我可以使用GD将给定颜色(RGB)与图像资源(RGBA)的每个像素相乘吗?例如,如果给定的色调颜色为红色(255,0,0),则图像资源上的黑色像素应保持黑色(因为255 * 0 = 0),较亮的像素应受该红色色调因子的影响。 / p>
我试过
imagefilter($sprite, IMG_FILTER_COLORIZE, 255, 0, 0);
但这只会改变黑色像素。 NEGATE,IMG_FILTER_COLORIZE,NEGATE也没有工作。
答案 0 :(得分:0)
这似乎对我有用:
<?php
// function for creating images from any uploaded
function imageCreateFromAny($filepath) {
$type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize()
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
6 // [] bmp
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($filepath);
break;
case 2 :
$im = imageCreateFromJpeg($filepath);
break;
case 3 :
$im = imageCreateFromPng($filepath);
break;
case 6 :
$im = imageCreateFromBmp($filepath);
break;
}
return $im;
}
// set up variables
$filter_r=255;
$filter_g=0;
$filter_b=0;
$suffixe="-red";
$path="original-source/image.jpg";
if(is_file($path)){
$image=imageCreateFromAny($path);
// invert inteded color "red"
$filter_r_opp = 255 - $filter_r; // = 0
$filter_g_opp = 255 - $filter_g; // = 255
$filter_b_opp = 255 - $filter_b; // = 255
// color is now "aqua"
/* FAST METHOD */
imagefilter($image, IMG_FILTER_NEGATE);
imagefilter($image, IMG_FILTER_COLORIZE, $filter_r_opp, $filter_g_opp, $filter_b_opp);
imagefilter($image, IMG_FILTER_NEGATE);
$new_path=substr($path,0,strlen($path)-4).$suffixe.".jpg";
imagejpeg($image,$new_path);
imagedestroy($image);
echo 'Red shading success.';
}
else {
echo 'Red shading failed.';
}
?>