Changeset 4628

Show
Ignore:
Timestamp:
12/07/06 22:42:22 (2 years ago)
Author:
ryan
Message:

Create attachment metadata for xmlrpc uploads. Props mdawaffe. fixes #3452

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/admin-functions.php

    r4612 r4628  
    852852    } else { 
    853853        return false; 
    854     } 
    855 } 
    856  
    857 function wp_create_thumbnail( $file, $max_side, $effect = '' ) { 
    858  
    859         // 1 = GIF, 2 = JPEG, 3 = PNG 
    860  
    861     if ( file_exists( $file ) ) { 
    862         $type = getimagesize( $file ); 
    863  
    864         // if the associated function doesn't exist - then it's not 
    865         // handle. duh. i hope. 
    866  
    867         if (!function_exists( 'imagegif' ) && $type[2] == 1 ) { 
    868             $error = __( 'Filetype not supported. Thumbnail not created.' ); 
    869         } 
    870         elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) { 
    871             $error = __( 'Filetype not supported. Thumbnail not created.' ); 
    872         } 
    873         elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) { 
    874             $error = __( 'Filetype not supported. Thumbnail not created.' ); 
    875         } else { 
    876  
    877             // create the initial copy from the original file 
    878             if ( $type[2] == 1 ) { 
    879                 $image = imagecreatefromgif( $file ); 
    880             } 
    881             elseif ( $type[2] == 2 ) { 
    882                 $image = imagecreatefromjpeg( $file ); 
    883             } 
    884             elseif ( $type[2] == 3 ) { 
    885                 $image = imagecreatefrompng( $file ); 
    886             } 
    887  
    888             if ( function_exists( 'imageantialias' )) 
    889                 imageantialias( $image, TRUE ); 
    890  
    891             $image_attr = getimagesize( $file ); 
    892  
    893             // figure out the longest side 
    894  
    895             if ( $image_attr[0] > $image_attr[1] ) { 
    896                 $image_width = $image_attr[0]; 
    897                 $image_height = $image_attr[1]; 
    898                 $image_new_width = $max_side; 
    899  
    900                 $image_ratio = $image_width / $image_new_width; 
    901                 $image_new_height = $image_height / $image_ratio; 
    902                 //width is > height 
    903             } else { 
    904                 $image_width = $image_attr[0]; 
    905                 $image_height = $image_attr[1]; 
    906                 $image_new_height = $max_side; 
    907  
    908                 $image_ratio = $image_height / $image_new_height; 
    909                 $image_new_width = $image_width / $image_ratio; 
    910                 //height > width 
    911             } 
    912  
    913             $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height); 
    914             @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] ); 
    915  
    916             // If no filters change the filename, we'll do a default transformation. 
    917             if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) ) 
    918                 $thumb = preg_replace( '!(\.[^.]+)?$!', __( '.thumbnail' ).'$1', basename( $file ), 1 ); 
    919  
    920             $thumbpath = str_replace( basename( $file ), $thumb, $file ); 
    921  
    922             // move the thumbnail to it's final destination 
    923             if ( $type[2] == 1 ) { 
    924                 if (!imagegif( $thumbnail, $thumbpath ) ) { 
    925                     $error = __( "Thumbnail path invalid" ); 
    926                 } 
    927             } 
    928             elseif ( $type[2] == 2 ) { 
    929                 if (!imagejpeg( $thumbnail, $thumbpath ) ) { 
    930                     $error = __( "Thumbnail path invalid" ); 
    931                 } 
    932             } 
    933             elseif ( $type[2] == 3 ) { 
    934                 if (!imagepng( $thumbnail, $thumbpath ) ) { 
    935                     $error = __( "Thumbnail path invalid" ); 
    936                 } 
    937             } 
    938  
    939         } 
    940     } else { 
    941         $error = __( 'File not found' ); 
    942     } 
    943  
    944     if (!empty ( $error ) ) { 
    945         return $error; 
    946     } else { 
    947         apply_filters( 'wp_create_thumbnail', $thumbpath ); 
    948         return $thumbpath; 
    949854    } 
    950855} 
  • trunk/wp-admin/upload-functions.php

    r4612 r4628  
    221221        $id = wp_insert_attachment($attachment, $file, $post_id); 
    222222 
    223         if ( preg_match('!^image/!', $attachment['post_mime_type']) ) { 
    224             // Generate the attachment's postmeta. 
    225             $imagesize = getimagesize($file); 
    226             $imagedata['width'] = $imagesize['0']; 
    227             $imagedata['height'] = $imagesize['1']; 
    228             list($uwidth, $uheight) = get_udims($imagedata['width'], $imagedata['height']); 
    229             $imagedata['hwstring_small'] = "height='$uheight' width='$uwidth'"; 
    230             $imagedata['file'] = $file; 
    231  
    232             wp_update_attachment_metadata( $id, $imagedata ); 
    233  
    234             if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) { 
    235                 if ( $imagedata['width'] > 128 && $imagedata['width'] >= $imagedata['height'] * 4 / 3 ) 
    236                     $thumb = wp_create_thumbnail($file, 128); 
    237                 elseif ( $imagedata['height'] > 96 ) 
    238                     $thumb = wp_create_thumbnail($file, 96); 
    239  
    240                 if ( @file_exists($thumb) ) { 
    241                     $newdata = $imagedata; 
    242                     $newdata['thumb'] = basename($thumb); 
    243                     wp_update_attachment_metadata( $id, $newdata ); 
    244                 } else { 
    245                     $error = $thumb; 
    246                 } 
    247             } 
    248         } else { 
    249             wp_update_attachment_metadata( $id, array() ); 
    250         } 
     223        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); 
    251224 
    252225        wp_redirect( get_option('siteurl') . "/wp-admin/upload.php?style=$style&tab=browse&action=view&ID=$id&post_id=$post_id"); 
  • trunk/wp-includes/functions.php

    r4524 r4628  
    10951095} 
    10961096 
     1097function wp_generate_attachment_metadata( $attachment_id, $file ) { 
     1098    $attachment = get_post( $attachment_id ); 
     1099 
     1100    $metadata = array(); 
     1101    if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) { 
     1102        $imagesize = getimagesize($file); 
     1103        $metadata['width'] = $imagesize['0']; 
     1104        $metadata['height'] = $imagesize['1']; 
     1105        list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']); 
     1106        $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; 
     1107        $metadata['file'] = $file; 
     1108 
     1109        if ( $metadata['width'] * $metadata['height'] < 3 * 1024 * 1024 ) { 
     1110            if ( $metadata['width'] > 128 && $metadata['width'] >= $metadata['height'] * 4 / 3 ) 
     1111                $thumb = wp_create_thumbnail($file, 128); 
     1112            elseif ( $metadata['height'] > 96 ) 
     1113                $thumb = wp_create_thumbnail($file, 96); 
     1114 
     1115            if ( @file_exists($thumb) ) 
     1116                $metadata['thumb'] = basename($thumb); 
     1117        } 
     1118    } 
     1119    return apply_filters( 'wp_generate_attachment_metadata', $metadata ); 
     1120} 
     1121 
     1122function wp_create_thumbnail( $file, $max_side, $effect = '' ) { 
     1123 
     1124        // 1 = GIF, 2 = JPEG, 3 = PNG 
     1125 
     1126    if ( file_exists( $file ) ) { 
     1127        $type = getimagesize( $file ); 
     1128 
     1129        // if the associated function doesn't exist - then it's not 
     1130        // handle. duh. i hope. 
     1131 
     1132        if (!function_exists( 'imagegif' ) && $type[2] == 1 ) { 
     1133            $error = __( 'Filetype not supported. Thumbnail not created.' ); 
     1134        } 
     1135        elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) { 
     1136            $error = __( 'Filetype not supported. Thumbnail not created.' ); 
     1137        } 
     1138        elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) { 
     1139            $error = __( 'Filetype not supported. Thumbnail not created.' ); 
     1140        } else { 
     1141 
     1142            // create the initial copy from the original file 
     1143            if ( $type[2] == 1 ) { 
     1144                $image = imagecreatefromgif( $file ); 
     1145            } 
     1146            elseif ( $type[2] == 2 ) { 
     1147                $image = imagecreatefromjpeg( $file ); 
     1148            } 
     1149            elseif ( $type[2] == 3 ) { 
     1150                $image = imagecreatefrompng( $file ); 
     1151            } 
     1152 
     1153            if ( function_exists( 'imageantialias' )) 
     1154                imageantialias( $image, TRUE ); 
     1155 
     1156            $image_attr = getimagesize( $file ); 
     1157 
     1158            // figure out the longest side 
     1159 
     1160            if ( $image_attr[0] > $image_attr[1] ) { 
     1161                $image_width = $image_attr[0]; 
     1162                $image_height = $image_attr[1]; 
     1163                $image_new_width = $max_side; 
     1164 
     1165                $image_ratio = $image_width / $image_new_width; 
     1166                $image_new_height = $image_height / $image_ratio; 
     1167                //width is > height 
     1168            } else { 
     1169                $image_width = $image_attr[0]; 
     1170                $image_height = $image_attr[1]; 
     1171                $image_new_height = $max_side; 
     1172 
     1173                $image_ratio = $image_height / $image_new_height; 
     1174                $image_new_width = $image_width / $image_ratio; 
     1175                //height > width 
     1176            } 
     1177 
     1178            $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height); 
     1179            @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] ); 
     1180 
     1181            // If no filters change the filename, we'll do a default transformation. 
     1182            if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) ) 
     1183                $thumb = preg_replace( '!(\.[^.]+)?$!', __( '.thumbnail' ).'$1', basename( $file ), 1 ); 
     1184 
     1185            $thumbpath = str_replace( basename( $file ), $thumb, $file ); 
     1186 
     1187            // move the thumbnail to it's final destination 
     1188            if ( $type[2] == 1 ) { 
     1189                if (!imagegif( $thumbnail, $thumbpath ) ) { 
     1190                    $error = __( "Thumbnail path invalid" ); 
     1191                } 
     1192            } 
     1193            elseif ( $type[2] == 2 ) { 
     1194                if (!imagejpeg( $thumbnail, $thumbpath ) ) { 
     1195                    $error = __( "Thumbnail path invalid" ); 
     1196                } 
     1197            } 
     1198            elseif ( $type[2] == 3 ) { 
     1199                if (!imagepng( $thumbnail, $thumbpath ) ) { 
     1200                    $error = __( "Thumbnail path invalid" ); 
     1201                } 
     1202            } 
     1203 
     1204        } 
     1205    } else { 
     1206        $error = __( 'File not found' ); 
     1207    } 
     1208 
     1209    if (!empty ( $error ) ) { 
     1210        return $error; 
     1211    } else { 
     1212        apply_filters( 'wp_create_thumbnail', $thumbpath ); 
     1213        return $thumbpath; 
     1214    } 
     1215} 
     1216 
    10971217function wp_explain_nonce($action) { 
    10981218    if ( $action !== -1 && preg_match('/([a-z]+)-([a-z]+)(_(.+))?/', $action, $matches) ) { 
  • trunk/xmlrpc.php

    r4612 r4628  
    873873        // Save the data 
    874874        $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $post_id ); 
    875         wp_update_attachment_metadata( $id, array() ); 
     875        wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); 
    876876 
    877877        return apply_filters( 'wp_handle_upload', array( 'file' => $upload[ 'file' ], 'url' => $upload[ 'url' ], 'type' => $type ) );