Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active November 14, 2019 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/23fe78ebab7b4435fae74f5d5a37695a to your computer and use it in GitHub Desktop.
Save Jany-M/23fe78ebab7b4435fae74f5d5a37695a to your computer and use it in GitHub Desktop.
[WP] Colorize (after greyscale), depending on post type, a post image (either feat, parsed or external) and save it as new to uploads
<?php
// This script assumes we are in a Post Loop
// Get the image you need however you like, I use https://github.com/Jany-M/WP-Imager
$image_orig = wp_imager($img_w, $img_h, 3, '', true, '', true, null, null, true);
// Check for image headers to avoid Error 400 - if all images come always from same server/site you dont need this
$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';
$ch = curl_init ($image_orig);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_exec ($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
//echo $content_type;
curl_close($ch);
if(!empty($image_orig) && $content_type != 'text/html; charset=us-ascii') {
$date_img = get_the_time('Y', get_the_ID()).'/'.get_the_time('m', get_the_ID());
$name_img = basename($image_orig);
$strip_from_name = array(' ', '%20');
$new_image_path = wp_upload_dir($date_img)["path"].'/colorized_'.str_replace($strip_from_name, '', $name_img);
if(!is_file($new_image_path) || !file_exists($new_image_path)) {
// Create new img
if (exif_imagetype($image_orig) == IMAGETYPE_PNG) {
$image = imagecreatefrompng($image_orig);
} elseif($img_extension == 'jpg' || exif_imagetype($image_orig) == IMAGETYPE_JPEG) {
$image = imagecreatefromjpeg($image_orig);
} elseif($img_extension == 'gif' || exif_imagetype($image_orig) == IMAGETYPE_GIF) {
$image = imagecreatefromgif($image_orig);
}
// First greyscale it
imagefilter($image, IMG_FILTER_GRAYSCALE);
// Colorize depending on post type
if($post_type == 'evento') {
imagefilter($image, IMG_FILTER_COLORIZE, 134, 117, 77);
} elseif($post_type == 'post') {
imagefilter($image, IMG_FILTER_COLORIZE, 72, 101, 151);
} elseif($post_type == 'corso') {
imagefilter($image, IMG_FILTER_COLORIZE, 201, 79, 85);
}
// If PNG keep transparency
if (exif_imagetype($image_orig) == IMAGETYPE_PNG || exif_imagetype($image_orig) == IMAGETYPE_GIF) {
imagealphablending($image, false);
imagesavealpha($image, true);
}
// Save new Image
$name_img = str_replace($strip_from_name, '', $name_img);
if (exif_imagetype($image_orig) == IMAGETYPE_PNG) {
imagepng($image, wp_upload_dir($date_img)["path"].'/colorized_'.$name_img);
} elseif(exif_imagetype($image_orig) == IMAGETYPE_JPEG) {
imagejpeg($image, wp_upload_dir($date_img)["path"].'/colorized_'.$name_img, 100);
} elseif(exif_imagetype($image_orig) == IMAGETYPE_GIF) {
imagegif($image, wp_upload_dir($date_img)["path"].'/colorized_'.$name_img);
}
// End Process
imagedestroy($image);
}
$new_image_url = wp_upload_dir($date_img)["url"].'/colorized_'.str_replace($strip_from_name, '', $name_img);
echo wp_imager($img_w, $img_h, 3, 'img-responsive', true, $new_image_url, false, null, null, null);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment