2011-11-11 10:37
浏览 65


First, I resize the image while saving transparency:

all the classic routine etcetera: 
$canvas = imagecreatefrom[png|gif|jpeg]();
$resize = imagecreatetruecolor();

    $transparentIndex = imagecolortransparent($canvas);
    if($transparentIndex >= 0){
        imagepalettecopy($canvas, $resize);
        imagefill($resize, 0, 0, $transparentIndex);
        imagecolortransparent($resize, $transparentIndex);
        imagetruecolortopalette($resize, true, 256);
        imagealphablending($resize, false);
        imagesavealpha($resize, true);                          
        $transparent = imagecolorallocatealpha($resize, 255, 255, 255, 127); 
        imagefill($resize, 0, 0, $transparent); 

imagecopyresampled($resize, $canvas, 0, 0, 0, 0, $nx, $ny, $x, $y);

// image[png|gif|jpeg]... (image gets saved)

Then, I want to apply grayscale filter to that previously saved image (within a new function):

classic routine again: 
$canvas = imagecreatefrom[png|gif|jpeg]()

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

imagefilter($canvas, IMG_FILTER_GRAYSCALE);

This fully filters PNG's to Grayscale while saving transparency,
but for GIF, black background is added to my grayscaled picture,
plus, the picture isn't fully grayscale (more like gets high-contrasted with acidic colors).

// image[png|gif|jpeg]

What would be the fix, to preserve transparency when applying IMG_FILTER_GRAYSCALE to gif?

What would be the fix, in order to transform gif to grayscale while saving the transparency? (Question revised due to answer provided by @Pierre)

Thanks in advance!

图片转代码服务由CSDN问答提供 功能建议


   / * 
 $ canvas = imagecreatefrom [png | gif | jpeg](); 
 $ resize = imagecreatetruecolor(); 
 * / 
if($ blending){
 $  transparentIndex = imagecolortransparent($ canvas); 
 if($ transparentIndex> = 0){
 imagepalettecopy($ canvas,$ resize); 
 imagefill($ resize,0,0,$ transparentIndex);  
 imagecolortransparent($ resize,$ transparentIndex); 
 imagetruecolortopalette($ resize,true,256); 
} else {
 imagealphablending($ resize,false); 
 imagesavealpha($ resize,true  );  
 $ transparent = imagecolorallocatealpha($ resize,255,255,255,127);  
 imagefill($ resize,0,0,$ transparent);  
imagecopyresampled($ resize,$ canvas,0,0,0,0,$ nx,$ ny,$ x,$ y); 
 // image [png | gif |  jpeg] ...(图像保存)


  / * 
 $ canvas = imagecreatefrom [png | gif | jpeg]()
 * / 
if($ blend){
 imagealphablending(  $ canvas,false); 
 imagesavealpha($ canvas,true); 
imagefilter($ canvas,IMG_FILTER_GRAYSCALE); 
 / * 
但是 GIF,黑色背景添加到我的灰度图片中,
 * / 
 // image [png | gif | jpeg  ] 

将IMG_FILTER_GRAYSCALE应用于 gif 时保留透明度的修复方法是什么?

为了在保存透明度的同时将 gif 转换为灰度,将会有什么修复 CY? (由于@Pierre提供的答案修改了问题)


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpizd08264 2011-11-11 10:47

    imagefilter used with the gray scale filter does take care of the alpha channel.

    However, gif does not support alpha. So you won't be able to store it using the GIF format.

    It is also important to note that the background color (one single color or color index being used as background) has nothing to do with the alpha (level of transparency of a given color or pixel). That means you are responsible to set the background color to the desired single color.


    It is not directly possible as the color used as transparent will be modified. That could be considered as a bug as the transparent color may or should be ignored by the filter. But that's another topic.

    A workaround could be:

    $logo = imagecreatefromgif('php.gif');
    $newimg = imagecreatetruecolor(imagesx($logo), imagesy($logo));
    /* copy ignore the transparent color
     * so that we can use black (0,0,0) as transparent, which is what 
     * the image is filled with when created.
    $transparent = imagecolorallocate($newimg, 0,0,0);
    imagecolortransparent($newimg, $transparent);
    imagecopy($newimg, $logo, 0,0, 0, 0,imagesx($logo), imagesx($logo));
    imagefilter($newimg, IMG_FILTER_GRAYSCALE);
    imagegif($newimg, 'a.gif');

    this code simply fetch the value of the existing transparent color, convert it to gray and set it back to the color index. This code will only work for palette image like gif but that's the idea.

    点赞 打赏 评论

相关推荐 更多相似问题