【发布时间】:2012-07-30 08:24:35
【问题描述】:
我正在尝试将我的 PNG 转换为灰度,并且它几乎可以在以下代码中正常工作:
$image = imagecreatefromstring(file_get_contents($this->image_dest."".$this->file_name));
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image, $this->image_dest."".$this->file_name);
问题是,当图像具有一定透明度时,透明像素被渲染为黑色。我see there are others 在他们的部分问题中也有同样的问题,但从未针对这个问题专门回答过。
希望有人能帮忙解决这个问题!
如果有帮助,我以前使用这个 sn-p 转换为灰度,但它与 png 中的透明像素转换为黑色有同样的问题,我不确定 如何检测透明度并使用 imagecolorat 函数进行转换。
//Creates the 256 color palette
for ($c=0;$c<256;$c++){
$palette[$c] = imagecolorallocate($new,$c,$c,$c);
}
//Creates yiq function
function yiq($r,$g,$b){
return (($r*0.299)+($g*0.587)+($b*0.114));
}
//Reads the origonal colors pixel by pixel
for ($y=0;$y<$h;$y++) {
for ($x=0;$x<$w;$x++) {
$rgb = imagecolorat($new,$x,$y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
//This is where we actually use yiq to modify our rbg values, and then convert them to our grayscale palette
$gs = yiq($r,$g,$b);
imagesetpixel($new,$x,$y,$palette[$gs]);
}
}
【问题讨论】:
-
嘿,我的朋友,我也遇到了这个问题。当我得到它时,我会发布一个解决方案。
标签: php image grayscale imagefilter