How to add multiple image sizes of the same size with add_image_size

Gee, I keep solving my own problems all the time. Here’s how I solved it in the end. I discovered that add_image_size does not ignore the identical dimensions of the image size, but instead points the file name towards the same file in the uploads dir. Once I knew that, I could just save the grayscale image under a different name, return that name to the $meta array in my hook, and WP takes that as information to store in the database. All get_thumbnail() functions are still working as they should and I can just request the -grayscale ID.

add_filter('wp_generate_attachment_metadata','gholumns_grayscale_filter');
function gholumns_grayscale_filter($meta)
{
$file = $meta['sizes']['gho-large-grayscale']['file'];
$meta['sizes']['gho-large-grayscale']['file'] = do_grayscale_filter($file);
//do_resize($file, -1, -1); # No longer necessary!
$file = $meta['sizes']['gho-medium-grayscale']['file'];
$meta['sizes']['gho-medium-grayscale']['file'] = do_grayscale_filter($file);
//do_resize($file, -1, -1); # No longer necessary!
$file = $meta['sizes']['gho-small-grayscale']['file'];
$meta['sizes']['gho-small-grayscale']['file'] = do_grayscale_filter($file);
//do_resize($file, -1, -1); # No longer necessary!
return $meta;
}


function do_grayscale_filter($file)
{
$dir = wp_upload_dir();
$image = wp_load_image(trailingslashit($dir['path']).$file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
return save_modified_image($image, $file, '-grayscale');
}

function save_modified_image($image, $filename, $suffix)
{
$dir = wp_upload_dir();
$dest = trailingslashit($dir['path']).$filename;

list($orig_w, $orig_h, $orig_type) = @getimagesize($dest);

$filename = str_ireplace(array('.jpg', '.jpeg', '.gif', '.png'), array($suffix.'.jpg', $suffix.'.jpeg', $suffix.'.gif', $suffix.'.png'), $filename);
$dest = trailingslashit($dir['path']).$filename;

switch ($orig_type)
{
    case IMAGETYPE_GIF:
        imagegif( $image, $dest );
        break;
    case IMAGETYPE_PNG:
        imagepng( $image, $dest );
        break;
    case IMAGETYPE_JPEG:
        imagejpeg( $image, $dest );
        break;
}

return $filename;
}

Leave a Comment