mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
Creating intermediate sizes, better thumbnails, and other image improvements. Hat tip: tellyworth.
git-svn-id: https://develop.svn.wordpress.org/trunk@7135 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
+35
-66
@@ -17,63 +17,8 @@
|
||||
* If PHP does not have the functionality to save in a file of the same format, the thumbnail will be created as a jpeg.
|
||||
*/
|
||||
function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
|
||||
if ( ctype_digit( $file ) ) // Handle int as attachment ID
|
||||
$file = get_attached_file( $file );
|
||||
|
||||
$image = wp_load_image( $file );
|
||||
|
||||
if ( !is_resource( $image ) )
|
||||
return $image;
|
||||
|
||||
list($sourceImageWidth, $sourceImageHeight, $sourceImageType) = getimagesize( $file );
|
||||
|
||||
if ( function_exists( 'imageantialias' ))
|
||||
imageantialias( $image, true );
|
||||
|
||||
list($image_new_width, $image_new_height) = wp_shrink_dimensions( $sourceImageWidth, $sourceImageHeight, $max_side, $max_side);
|
||||
|
||||
$thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
|
||||
|
||||
// preserve PNG transparency
|
||||
if ( IMAGETYPE_PNG == $sourceImageType && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
|
||||
imagealphablending( $thumbnail, false);
|
||||
imagesavealpha( $thumbnail, true);
|
||||
}
|
||||
|
||||
imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $sourceImageWidth, $sourceImageHeight );
|
||||
|
||||
imagedestroy( $image ); // Free up memory
|
||||
|
||||
// If no filters change the filename, we'll do a default transformation.
|
||||
if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
|
||||
$thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail$1', basename( $file ), 1 );
|
||||
|
||||
$thumbpath = str_replace( basename( $file ), $thumb, $file );
|
||||
|
||||
switch( $sourceImageType ){
|
||||
default: // We'll create a Jpeg if we cant use its native file format
|
||||
$thumb = preg_replace( '/\\.[^\\.]+$/', '.jpg', $thumb ); //Change file extension to Jpg
|
||||
case IMAGETYPE_JPEG:
|
||||
if (!imagejpeg( $thumbnail, $thumbpath ) )
|
||||
return __( 'Thumbnail path invalid' );
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
if (!imagegif( $thumbnail, $thumbpath ) )
|
||||
return __( 'Thumbnail path invalid' );
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
if (!imagepng( $thumbnail, $thumbpath ) )
|
||||
return __( 'Thumbnail path invalid' );
|
||||
break;
|
||||
}
|
||||
|
||||
imagedestroy( $thumbnail ); // Free up memory
|
||||
|
||||
// Set correct file permissions
|
||||
$stat = stat( dirname( $thumbpath ));
|
||||
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
|
||||
@ chmod( $thumbpath, $perms );
|
||||
|
||||
|
||||
$thumbpath = image_resize( $file, $max_side, $max_side );
|
||||
return apply_filters( 'wp_create_thumbnail', $thumbpath );
|
||||
}
|
||||
|
||||
@@ -142,7 +87,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
|
||||
$attachment = get_post( $attachment_id );
|
||||
|
||||
$metadata = array();
|
||||
if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
|
||||
if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
|
||||
$imagesize = getimagesize( $file );
|
||||
$metadata['width'] = $imagesize[0];
|
||||
$metadata['height'] = $imagesize[1];
|
||||
@@ -150,15 +95,16 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
|
||||
$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
|
||||
$metadata['file'] = $file;
|
||||
|
||||
$max = apply_filters( 'wp_thumbnail_creation_size_limit', absint( WP_MEMORY_LIMIT ) * 1024 * 1024, $attachment_id, $file );
|
||||
|
||||
if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
|
||||
$max_side = apply_filters( 'wp_thumbnail_max_side_length', 140, $attachment_id, $file );
|
||||
$thumb = wp_create_thumbnail( $file, $max_side );
|
||||
if ( @file_exists($thumb) )
|
||||
$metadata['thumb'] = basename($thumb);
|
||||
// make thumbnails and other intermediate sizes
|
||||
$sizes = array('thumbnail', 'medium');
|
||||
$sizes = apply_filters('intermediate_image_sizes', $sizes);
|
||||
|
||||
foreach ($sizes as $size) {
|
||||
$resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
|
||||
if ( $resized )
|
||||
$metadata['sizes'][$size] = $resized;
|
||||
}
|
||||
|
||||
|
||||
// fetch additional metadata from exif/iptc
|
||||
$image_meta = wp_read_image_metadata( $file );
|
||||
if ($image_meta)
|
||||
@@ -309,5 +255,28 @@ function wp_read_image_metadata( $file ) {
|
||||
|
||||
}
|
||||
|
||||
// is the file a real image file?
|
||||
function file_is_valid_image($path) {
|
||||
$size = @getimagesize($path);
|
||||
return !empty($size);
|
||||
}
|
||||
|
||||
// is the file an image suitable for displaying within a web page?
|
||||
function file_is_displayable_image($path) {
|
||||
$info = @getimagesize($path);
|
||||
if ( empty($info) )
|
||||
$result = false;
|
||||
elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) )
|
||||
// only gif, jpeg and png images can reliably be displayed
|
||||
$result = false;
|
||||
elseif ( $info['channels'] > 0 && $info['channels'] != 3 ) {
|
||||
// some web browsers can't display cmyk or grayscale jpegs
|
||||
$result = false;
|
||||
}
|
||||
else
|
||||
$result = true;
|
||||
|
||||
return apply_filters('file_is_displayable_image', $result, $path);
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -55,7 +55,7 @@ function get_image_send_to_editor($id, $alt, $title, $align, $url='', $rel = fal
|
||||
$rel = $rel ? ' rel="attachment wp-att-'.attribute_escape($id).'"' : '';
|
||||
if ( $url )
|
||||
$html = "<a href='".attribute_escape($url)."'$rel>$html</a>";
|
||||
elseif ( $size == 'thumb' || $size == 'medium' )
|
||||
elseif ( $size == 'thumbnail' || $size == 'medium' )
|
||||
$html = '<a href="'.get_attachment_link($id).'"'.$rel.'>'.$html.'</a>';
|
||||
|
||||
$html = apply_filters( 'image_send_to_editor', $html, $id, $alt, $title, $align, $url );
|
||||
@@ -467,7 +467,7 @@ function image_attachment_fields_to_edit($form_fields, $post) {
|
||||
'label' => __('Size'),
|
||||
'input' => 'html',
|
||||
'html' => "
|
||||
" . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumb' />
|
||||
" . ( $thumb ? "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-thumb-$post->ID' value='thumbnail' />
|
||||
<label for='image-size-thumb-$post->ID'>" . __('Thumbnail') . "</label>
|
||||
" : '' ) . "<input type='radio' name='attachments[$post->ID][image-size]' id='image-size-medium-$post->ID' value='medium' checked='checked' />
|
||||
<label for='image-size-medium-$post->ID'>" . __('Medium') . "</label>
|
||||
|
||||
@@ -244,6 +244,11 @@ function populate_options() {
|
||||
add_option('show_avatars', '1');
|
||||
add_option('avatar_rating', 'G');
|
||||
add_option('upload_url_path', '');
|
||||
add_option('thumbnail_size_w', 150);
|
||||
add_option('thumbnail_size_h', 150);
|
||||
add_option('thumbnail_crop', 1);
|
||||
add_option('medium_size_w', '');
|
||||
add_option('medium_size_h', '');
|
||||
|
||||
// Delete unused options
|
||||
$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing');
|
||||
|
||||
@@ -68,6 +68,8 @@ endforeach;
|
||||
<input name="thumbnail_size_w" type="text" id="thumbnail_size_w" value="<?php form_option('thumbnail_size_w'); ?>" size="6" />
|
||||
<label for="thumbnail_size_h"><?php _e('Height'); ?></label>
|
||||
<input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" size="6" />
|
||||
<input name="thumbnail_crop" type="checkbox" id="thumbnail_crop" value="1" <?php checked('1', get_option('thumbnail_crop')); ?>/>
|
||||
<label for="thumbnail_crop"><?php _e('Crop to size'); ?></label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr valign="top">
|
||||
@@ -134,7 +136,7 @@ endforeach;
|
||||
|
||||
<p class="submit">
|
||||
<input type="hidden" name="action" value="update" />
|
||||
<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" />
|
||||
<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" />
|
||||
<input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" />
|
||||
</p>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user