Created
February 3, 2016 14:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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