1

我正在尝试根据我的捐赠百分比对图像进行部分着色,所以我发现的方法是将彩色图像的一个小部分复制到等效的黑白图像中,这样我就可以用任何图像实现一个小压力表我想。

<?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);
} 

?>

我可以成功创建黑白图像,但无法进行复制部分。我究竟做错了什么?有一个更好的方法吗

4

0 回答 0