I’m trying to partially color an image based on my donation percentages, so the way i found was to copy a subbsection of a colored image, into the equivalent black and white image, so i could achieve a litle press meter with any image i want.
<?php
function createPartialGraycaleCopy($post_id, &$image_src_path,&$image_src_filename,&$bw_image_filename,$valor_currente,$objectivo) {
$post_thumbnail_id = get_post_thumbnail_id($post_id);
// Find thumbnail locations
$image_src = wp_get_attachment_image_src($post_thumbnail_id, 'thumbnail');
$image_src_path = dirname($image_src[0]);
$image_src_filename =substr(basename($image_src[0]), 0, strpos(basename($image_src[0]), "?")) ;
// Create greyscale filename
$image_src_extention_loc = (strripos($image_src_filename, '.') - strlen($image_src_filename));
$bw_image_filename = substr($image_src_filename, 0, $image_src_extention_loc) . '_bw' . substr($image_src_filename, $image_src_extention_loc);
// Get the local path of the image
$wpcontent_image_path = dirname(get_attached_file($post_thumbnail_id));
// Create greyscale image
$bw_image = wp_load_image( $wpcontent_image_path . '/' . $image_src_filename);
// Apply greyscale filter
imagefilter($bw_image, IMG_FILTER_GRAYSCALE);
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image); // Save the image.
imagejpeg($bw_image,$wpcontent_image_path . '/' . $bw_image_filename, 100);
imagedestroy($bw_image);
partialGrayscale($image_src_path,$image_src_filename,$bw_image_filename,$valor_currente,$objectivo) ;
}
function partialGrayscale ($image_src_path,$image_src_filename,$bw_image_filename,$valor_currente,$objectivo){
// Create image instances
$color = imagecreatefromjpeg($image_src_path . '/' . $image_src_filename);
$saturated = imagecreatefromjpeg($image_src_path . '/' . $bw_image_filename);
$percentagemCompleto=round((100*$valor_currente)/(100-$valor_currente));
$comprimentoSaturado=imagesx($saturated)-(round(imagesx($saturated)*($percentagemCompleto/100)));
$alturaSaturada=imagesy($saturated)-(round(imagesy($saturated)*($percentagemCompleto/100)));
// Greyscale operation
echo imagecopyresampled ($saturated, $color, 0, 0, 0, 0, $comprimentoSaturado, $alturaSaturada,$comprimentoSaturado, $alturaSaturada);
// Save the greyscale image file
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
// Save the image.
imagejpeg($saturated,$wpcontent_image_path . '/' . $bw_image_filename, 100);
// Remove original images from memory.
imagedestroy($color);
imagedestroy($saturated);
}
?>
I can successfully create an black and white image but i can’t do the copy part.
What am i doing wrong?
Is there a better way to do this