Changeset 7135
- Timestamp:
- 03/03/2008 04:17:37 AM (17 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/image.php
r7041 r7135 18 18 */ 19 19 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { 20 if ( ctype_digit( $file ) ) // Handle int as attachment ID 21 $file = get_attached_file( $file ); 22 23 $image = wp_load_image( $file ); 24 25 if ( !is_resource( $image ) ) 26 return $image; 27 28 list($sourceImageWidth, $sourceImageHeight, $sourceImageType) = getimagesize( $file ); 29 30 if ( function_exists( 'imageantialias' )) 31 imageantialias( $image, true ); 32 33 list($image_new_width, $image_new_height) = wp_shrink_dimensions( $sourceImageWidth, $sourceImageHeight, $max_side, $max_side); 34 35 $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height); 36 37 // preserve PNG transparency 38 if ( IMAGETYPE_PNG == $sourceImageType && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { 39 imagealphablending( $thumbnail, false); 40 imagesavealpha( $thumbnail, true); 41 } 42 43 imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $sourceImageWidth, $sourceImageHeight ); 44 45 imagedestroy( $image ); // Free up memory 46 47 // If no filters change the filename, we'll do a default transformation. 48 if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) ) 49 $thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail$1', basename( $file ), 1 ); 50 51 $thumbpath = str_replace( basename( $file ), $thumb, $file ); 52 53 switch( $sourceImageType ){ 54 default: // We'll create a Jpeg if we cant use its native file format 55 $thumb = preg_replace( '/\\.[^\\.]+$/', '.jpg', $thumb ); //Change file extension to Jpg 56 case IMAGETYPE_JPEG: 57 if (!imagejpeg( $thumbnail, $thumbpath ) ) 58 return __( 'Thumbnail path invalid' ); 59 break; 60 case IMAGETYPE_GIF: 61 if (!imagegif( $thumbnail, $thumbpath ) ) 62 return __( 'Thumbnail path invalid' ); 63 break; 64 case IMAGETYPE_PNG: 65 if (!imagepng( $thumbnail, $thumbpath ) ) 66 return __( 'Thumbnail path invalid' ); 67 break; 68 } 69 70 imagedestroy( $thumbnail ); // Free up memory 71 72 // Set correct file permissions 73 $stat = stat( dirname( $thumbpath )); 74 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits 75 @ chmod( $thumbpath, $perms ); 76 20 21 $thumbpath = image_resize( $file, $max_side, $max_side ); 77 22 return apply_filters( 'wp_create_thumbnail', $thumbpath ); 78 23 } … … 143 88 144 89 $metadata = array(); 145 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {90 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { 146 91 $imagesize = getimagesize( $file ); 147 92 $metadata['width'] = $imagesize[0]; … … 151 96 $metadata['file'] = $file; 152 97 153 $max = apply_filters( 'wp_thumbnail_creation_size_limit', absint( WP_MEMORY_LIMIT ) * 1024 * 1024, $attachment_id, $file ); 154 155 if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) { 156 $max_side = apply_filters( 'wp_thumbnail_max_side_length', 140, $attachment_id, $file ); 157 $thumb = wp_create_thumbnail( $file, $max_side ); 158 if ( @file_exists($thumb) ) 159 $metadata['thumb'] = basename($thumb); 98 // make thumbnails and other intermediate sizes 99 $sizes = array('thumbnail', 'medium'); 100 $sizes = apply_filters('intermediate_image_sizes', $sizes); 101 102 foreach ($sizes as $size) { 103 $resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") ); 104 if ( $resized ) 105 $metadata['sizes'][$size] = $resized; 160 106 } 161 107 162 108 // fetch additional metadata from exif/iptc 163 109 $image_meta = wp_read_image_metadata( $file ); … … 310 256 } 311 257 258 // is the file a real image file? 259 function file_is_valid_image($path) { 260 $size = @getimagesize($path); 261 return !empty($size); 262 } 263 264 // is the file an image suitable for displaying within a web page? 265 function file_is_displayable_image($path) { 266 $info = @getimagesize($path); 267 if ( empty($info) ) 268 $result = false; 269 elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) 270 // only gif, jpeg and png images can reliably be displayed 271 $result = false; 272 elseif ( $info['channels'] > 0 && $info['channels'] != 3 ) { 273 // some web browsers can't display cmyk or grayscale jpegs 274 $result = false; 275 } 276 else 277 $result = true; 278 279 return apply_filters('file_is_displayable_image', $result, $path); 280 } 312 281 313 282 ?> -
trunk/wp-admin/includes/media.php
r7130 r7135 56 56 if ( $url ) 57 57 $html = "<a href='".attribute_escape($url)."'$rel>$html</a>"; 58 elseif ( $size == 'thumb ' || $size == 'medium' )58 elseif ( $size == 'thumbnail' || $size == 'medium' ) 59 59 $html = '<a href="'.get_attachment_link($id).'"'.$rel.'>'.$html.'</a>'; 60 60 … … 468 468 'input' => 'html', 469 469 'html' => " 470 " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumb ' />470 " . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' /> 471 471 <label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label> 472 472 " : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' /> -
trunk/wp-admin/includes/schema.php
r6916 r7135 245 245 add_option('avatar_rating', 'G'); 246 246 add_option('upload_url_path', ''); 247 add_option('thumbnail_size_w', 150); 248 add_option('thumbnail_size_h', 150); 249 add_option('thumbnail_crop', 1); 250 add_option('medium_size_w', ''); 251 add_option('medium_size_h', ''); 247 252 248 253 // Delete unused options -
trunk/wp-admin/options-writing.php
r7000 r7135 69 69 <label for="thumbnail_size_h"><?php _e('Height'); ?></label> 70 70 <input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" size="6" /> 71 <input name="thumbnail_crop" type="checkbox" id="thumbnail_crop" value="1" <?php checked('1', get_option('thumbnail_crop')); ?>/> 72 <label for="thumbnail_crop"><?php _e('Crop to size'); ?></label> 71 73 </td> 72 74 </tr> … … 135 137 <p class="submit"> 136 138 <input type="hidden" name="action" value="update" /> 137 <input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category " />139 <input type="hidden" name="page_options" value="default_post_edit_rows,use_smilies,ping_sites,mailserver_url,mailserver_port,mailserver_login,mailserver_pass,default_category,default_email_category,use_balanceTags,default_link_category,thumbnail_size_w,thumbnail_size_h,thumbnail_crop,medium_size_w,medium_size_h" /> 138 140 <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /> 139 141 </p> -
trunk/wp-includes/media.php
r7130 r7135 49 49 50 50 // Scale an image to fit a particular size (such as 'thumb' or 'medium'), and return an image URL, height and width. 51 // The URL might be the original image, or it might be a resized version. 51 // The URL might be the original image, or it might be a resized version. This function won't create a new resized copy, it will just return an already resized one if it exists. 52 52 // returns an array($url, $width, $height) 53 53 function image_downsize($id, $size = 'medium') { … … 61 61 return $out; 62 62 63 if ( $size == 'thumb' ) { 64 // thumbnail: use the thumb as the displayed image, and constrain based on its dimensions 65 $thumb_path = wp_get_attachment_thumb_file($id); 66 // the actual thumbnail size isn't stored so we'll have to calculate it 67 if ( $thumb_path && ($info = getimagesize($thumb_path)) ) { 68 list( $width, $height ) = image_constrain_size_for_editor( $info[0], $info[1], $size ); 69 $img_url = wp_get_attachment_thumb_url($id); 70 } 71 // this could be improved to provide a default thumbnail if one doesn't exist 63 // try for a new style intermediate size 64 if ( $intermediate = image_get_intermediate_size($id, $size) ) { 65 $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url); 66 $width = $intermediate['width']; 67 $height = $intermediate['height']; 68 } 69 elseif ( $size == 'thumbnail' ) { 70 // fall back to the old thumbnail 71 if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) { 72 $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url); 73 $width = $info[0]; 74 $height = $info[1]; 75 } 72 76 } 73 77 elseif ( isset($meta['width'], $meta['height']) ) { … … 227 231 } 228 232 233 // resize an image to make a thumbnail or intermediate size, and return metadata describing the new copy 234 // returns false if no image was created 235 function image_make_intermediate_size($file, $width, $height, $crop=false) { 236 if ( $width || $height ) { 237 $resized_file = image_resize($file, $width, $height, $crop); 238 if ( $resized_file && $info = getimagesize($resized_file) ) { 239 return array( 240 'file' => basename( $resized_file ), 241 'width' => $info[0], 242 'height' => $info[1], 243 ); 244 } 245 } 246 return false; 247 } 248 249 function image_get_intermediate_size($post_id, $size='thumbnail') { 250 if ( !$imagedata = wp_get_attachment_metadata( $post_id ) ) 251 return false; 252 253 if ( empty($imagedata['sizes'][$size]) ) 254 return false; 255 256 return $imagedata['sizes'][$size]; 257 } 258 259 229 260 ?> -
trunk/wp-includes/post-template.php
r7073 r7135 390 390 $file = get_attached_file( $post->ID ); 391 391 392 if ( !$fullsize && $ thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {392 if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) { 393 393 // We have a thumbnail desired, specified and existing 394 394 395 $src = wp_get_attachment_thumb_url( $post->ID ); 396 $src_file = $thumbfile; 395 $src_file = basename($src); 397 396 $class = 'attachmentthumb'; 398 397 } elseif ( wp_attachment_is_image( $post->ID ) ) { -
trunk/wp-includes/post.php
r7130 r7135 2129 2129 if ( $file ) 2130 2130 update_attached_file( $post_ID, $file ); 2131 2131 2132 2132 clean_post_cache($post_ID); 2133 2133 … … 2317 2317 if ( !$url = wp_get_attachment_url( $post->ID ) ) 2318 2318 return false; 2319 2320 $sized = image_downsize( $post_id, 'thumbnail' ); 2321 if ( $sized ) 2322 return $sized[0]; 2319 2323 2320 2324 if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
Note: See TracChangeset
for help on using the changeset viewer.