kaput
kaput

Reputation: 53

Upsizing Wordpress featured image thumbnail / Always convert GIF to JPG

This one's a bit of a two-parter, as either could solve my problem.

Upsizing featured image when selected image smaller than specified thumbnail size

If a file is uploaded that's smaller than the smallest set thumbnail size - such as a 100x100 image when the thumbnail size is 150x150, Wordpress doesn't force upsize. Is there any way that I can force an upsize, other than using an external source like Imagemagick?

Convert uploaded GIF to JPG in every instance

An alternative to forcing an upsize would be to create a JPG alternative to any uploaded GIF file. Right now, GIFs larger than the thumbnail size - such as a 200x200 GIF when the thumbnail size is 150x150 - automatically have a static JPG thumbnail generated.

I'm generating thumbnails programmatically with:

// Inserts metadata and creates thumbnails for uploaded file
$attach_id = wp_insert_attachment($attachmentMeta, $newFile, $post_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $newFile);
wp_update_attachment_metadata($attach_id, $attach_data);

// Adds the thumbnail to the post
update_post_meta($post_id, '_thumbnail_id', $attach_id);

While I could do all of this with Imagemagick, I'd prefer to use Wordpress's own capabilities to get it done. Any help would be greatly appreciated!

Upvotes: 0

Views: 843

Answers (1)

msun
msun

Reputation: 831

As of Wordpress 3.6, I don't believe there is a built-in method to "upsize" an input image so that it fits/fills the requested thumbnail size in the case where the source image is too small.

Downsizing is simple, however. Anyone seeking a solution to this issue may know by now that many users seem to suggest/confuse "add_image_size" as the appropriate solution for this type of issue. It is not the entire solution.

It's important to note that if either the width or height of the source image is less than either predefined thumbnail/image-size dimension (i.e., dimensions specified via add_image_size or via the settings), then WP will return original source image instead of a thumbnail. Obviously this can be frustrating.

Many other users have suggested just using CSS to achieve the desired results; e.g., to force a minimum width or height. Although CSS will work in many situations, it won't work in every situation, especially in cases where you're dealing with fluid or responsive layouts.

Below is a rough, untested filter used to force a thumbnail to always fit the desired dimensions. Drop this in your functions.php and it will also require timthumb.php be available in your theme. You'll likely need to modify it to fit your specific situation, and perhaps it could be improved in several ways.

function my_image_downsize($src_data, $thumbnail_id, $size) {
    static $omit = false;
    global $_wp_additional_image_sizes;

    if($omit) {
        return $src_data;
    }

    $omit = true;

    $thumbnail_src_data = wp_get_attachment_image_src($thumbnail_id, $size);

    $img_url = $thumbnail_src_data[0];
    $width = $thumbnail_src_data[1];
    $height = $thumbnail_src_data[2];

    if(is_string($size) && isset($_wp_additional_image_sizes[$size]) && ($width < $_wp_additional_image_sizes[$size]['width'] || $height < $_wp_additional_image_sizes[$size]['height'])) {
        // "Upsize" the image as needed, to meet the minimum dimension of the thumbnail.
        $width = $_wp_additional_image_sizes[$size]['width'];
        $height = $_wp_additional_image_sizes[$size]['height'];
        $img_url = get_stylesheet_directory_uri() .'/timthumb.php?src='.$img_url.'&w='.$width.'&h='.$height.'&zc=2&cc=000000'; // TODO: replace timthumb with WP's native media class
    } else {
        $omit = false;
        return $src_data;
    }

    $omit = false;

    return array($img_url, $width, $height);
}
add_filter('image_downsize', 'my_image_downsize', 10, 3);

Upvotes: 0

Related Questions