Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminsinger/03fe9167316971e211aa to your computer and use it in GitHub Desktop.
Save benjaminsinger/03fe9167316971e211aa to your computer and use it in GitHub Desktop.
WordPress Grayscale Thumbnail Generation - Makes duplicates of all uploaded images in grayscale without overwriting existing media
add_filter('wp_generate_attachment_metadata','rb_bw_filter');
function rb_bw_filter($meta) {
$path = wp_upload_dir(); // get upload directory
$file = $path['basedir'].'/'.$meta['file']; // Get full size image
$files[] = $file; // Set up an array of image size urls
foreach ($meta['sizes'] as $size) {
$files[] = $path['path'].'/'.$size['file'];
}
foreach ($files as $file) { // iterate through each image size
// Convert image to grayscale credit to http://ottopress.com/2011/customizing-wordpress-images/
list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
$image = wp_load_image($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
switch ($orig_type) {
case IMAGETYPE_GIF:
$file = str_replace(".gif", "-bw.gif", $file);
imagegif( $image, $file );
break;
case IMAGETYPE_PNG:
$file = str_replace(".png", "-bw.png", $file);
imagepng( $image, $file );
break;
case IMAGETYPE_JPEG:
$file = str_replace(".jpg", "-bw.jpg", $file);
imagejpeg( $image, $file );
break;
}
}
return $meta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment