繁体   English   中英

产生的图像颜色与我通过的不完全相同

[英]Resulting image color is not exactly what i passed

我有一个彩色图像,我想用GD库更改该颜色。 图片是PNG,我也想保持透明度。 我编写了下面的代码,这些代码可以保持透明度并更改颜色,但是生成的颜色不是我在imagefilter函数中使用的颜色。 例如,如果我传递0,0,255,则结果图像颜色将为194,194,255。

请帮助我解决此问题。 这是我的代码。

<?php
// first we will create a transparent image. an image that has no color.
$width = 294; $height=333;
$image = imagecreatetruecolor($width,$height); //black image of the specified width x height. 

imagealphablending($image, false);  // set blend mode to false.

$col=imagecolorallocatealpha($image,255,255,255,127); // fill color

imagefilledrectangle($image,0,0,$width,$height,$col); 

imagealphablending($image,true);


$shirt = imagecreatefrompng("primary_shirts/shirt.png");
imagesavealpha($shirt, true);
imagefilter($shirt, IMG_FILTER_GRAYSCALE);
imagefilter($shirt, IMG_FILTER_COLORIZE, 0,0,255);

imagecopy($image, $shirt, 0, 0, 0, 0, $width, $height);

imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);

if(imagepng($image, "primary_shirts/hello.png", 1)){
    echo "http://localhost/site/primary_shirts/hello.png";
}

imagedestroy($image);
imagedestroy($shirt);
?>

编辑:我试图将图像着色为蓝色(0,0,255)。 该脚本为图像着色,但结果图像不是(0,0,255),而是(76,76,255)。 为什么这样?

if(imagepng($image, "primary_shirts/hello.png", 1)){

应该

if(imagepng($image, "primary_shirts/shirt.png", 1)){

header("Content-type: image/png");在哪里header("Content-type: image/png");

我不知道hello.png和shirt.png的内容是什么。 但是,请尝试坚持最简单的示例 (除非您要提供更多信息)

$red = 0; $green = 204; $blue = 204;
$shirt = "primary_shirts/shirt.png";
$im = imagecreatefrompng ($shirt);
imagesavealpha($im, true);
imagefilter($im,IMG_FILTER_GRAYSCALE);
if($im && imagefilter($im,IMG_FILTER_COLORIZE, $red-195, $green-195, $blue-195  ) )
{
    imagepng($im, 'primary_shirts/hello.png');
    echo "http://localhost/site/primary_shirts/hello.png";
}

看来IMG_FILTER_COLORIZE并没有真正做到php.net所说的。 实际上,它从您传递的图像中减去图像颜色。 到目前为止,上面的代码正是在做我想要做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM