WordPress thumbs – how to generate Black&white transparent png

I’m trying to generate a b&w png thumb in wordpres. The problem is that the pngs I am uploading have transparency. They are different logos from partners I need in grayscale to match the design of the Template.

So far I got this:

Read More
// add thumb size
add_image_size('pvz-partnerbw-image', 9999, 64, false);

// Generate BW thumbnail
add_filter('wp_generate_attachment_metadata','pvz_bw_filter');
function pvz_bw_filter($meta) {
    $file = wp_upload_dir();
    $file = trailingslashit($file['path']).$meta['sizes']['pvz-partnerbw-image']['file'];
    list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
    $image = wp_load_image($file);
    imagefilter($image, IMG_FILTER_GRAYSCALE);
    imagesavealpha($image,true);

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

It almost works but the problem is that on the edges between the artwork and transparent background the colors stay. So I get a b&w logo with 1px red (or some other color). Anyone got a better solution for this – The kind that doesn’t include Photoshoping the images ;). Thank you in advance!

Related posts

Leave a Reply

1 comment

  1. For starters, wp_load_image has been depreciated. You should be using wp_image_editor. I suggest rewriting your function using wp_image_editor to see if it creates the same problems whn applying the GRAYSCALE filter.