mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 15:50:09 +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:
@@ -48,7 +48,7 @@ function image_hwstring($width, $height) {
|
||||
}
|
||||
|
||||
// Scale an image to fit a particular size (such as 'thumb' or 'medium'), and return an image URL, height and width.
|
||||
// The URL might be the original image, or it might be a resized version.
|
||||
// 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.
|
||||
// returns an array($url, $width, $height)
|
||||
function image_downsize($id, $size = 'medium') {
|
||||
|
||||
@@ -60,15 +60,19 @@ function image_downsize($id, $size = 'medium') {
|
||||
if ( $out = apply_filters('image_downsize', false, $id, $size) )
|
||||
return $out;
|
||||
|
||||
if ( $size == 'thumb' ) {
|
||||
// thumbnail: use the thumb as the displayed image, and constrain based on its dimensions
|
||||
$thumb_path = wp_get_attachment_thumb_file($id);
|
||||
// the actual thumbnail size isn't stored so we'll have to calculate it
|
||||
if ( $thumb_path && ($info = getimagesize($thumb_path)) ) {
|
||||
list( $width, $height ) = image_constrain_size_for_editor( $info[0], $info[1], $size );
|
||||
$img_url = wp_get_attachment_thumb_url($id);
|
||||
// try for a new style intermediate size
|
||||
if ( $intermediate = image_get_intermediate_size($id, $size) ) {
|
||||
$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
|
||||
$width = $intermediate['width'];
|
||||
$height = $intermediate['height'];
|
||||
}
|
||||
elseif ( $size == 'thumbnail' ) {
|
||||
// fall back to the old thumbnail
|
||||
if ( $thumb_file = wp_get_attachment_thumb_file() && $info = getimagesize($thumb_file) ) {
|
||||
$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
|
||||
$width = $info[0];
|
||||
$height = $info[1];
|
||||
}
|
||||
// this could be improved to provide a default thumbnail if one doesn't exist
|
||||
}
|
||||
elseif ( isset($meta['width'], $meta['height']) ) {
|
||||
// any other type: use the real image and constrain it
|
||||
@@ -226,4 +230,31 @@ function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_p
|
||||
return $destfilename;
|
||||
}
|
||||
|
||||
?>
|
||||
// resize an image to make a thumbnail or intermediate size, and return metadata describing the new copy
|
||||
// returns false if no image was created
|
||||
function image_make_intermediate_size($file, $width, $height, $crop=false) {
|
||||
if ( $width || $height ) {
|
||||
$resized_file = image_resize($file, $width, $height, $crop);
|
||||
if ( $resized_file && $info = getimagesize($resized_file) ) {
|
||||
return array(
|
||||
'file' => basename( $resized_file ),
|
||||
'width' => $info[0],
|
||||
'height' => $info[1],
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function image_get_intermediate_size($post_id, $size='thumbnail') {
|
||||
if ( !$imagedata = wp_get_attachment_metadata( $post_id ) )
|
||||
return false;
|
||||
|
||||
if ( empty($imagedata['sizes'][$size]) )
|
||||
return false;
|
||||
|
||||
return $imagedata['sizes'][$size];
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
||||
@@ -389,11 +389,10 @@ function get_attachment_icon_src( $id = 0, $fullsize = false ) {
|
||||
|
||||
$file = get_attached_file( $post->ID );
|
||||
|
||||
if ( !$fullsize && $thumbfile = wp_get_attachment_thumb_file( $post->ID ) ) {
|
||||
if ( !$fullsize && $src = wp_get_attachment_thumb_url( $post->ID ) ) {
|
||||
// We have a thumbnail desired, specified and existing
|
||||
|
||||
$src = wp_get_attachment_thumb_url( $post->ID );
|
||||
$src_file = $thumbfile;
|
||||
$src_file = basename($src);
|
||||
$class = 'attachmentthumb';
|
||||
} elseif ( wp_attachment_is_image( $post->ID ) ) {
|
||||
// We have an image without a thumbnail
|
||||
|
||||
@@ -2128,7 +2128,7 @@ function wp_insert_attachment($object, $file = false, $parent = 0) {
|
||||
|
||||
if ( $file )
|
||||
update_attached_file( $post_ID, $file );
|
||||
|
||||
|
||||
clean_post_cache($post_ID);
|
||||
|
||||
if ( $update) {
|
||||
@@ -2316,6 +2316,10 @@ function wp_get_attachment_thumb_url( $post_id = 0 ) {
|
||||
return false;
|
||||
if ( !$url = wp_get_attachment_url( $post->ID ) )
|
||||
return false;
|
||||
|
||||
$sized = image_downsize( $post_id, 'thumbnail' );
|
||||
if ( $sized )
|
||||
return $sized[0];
|
||||
|
||||
if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) )
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user