douhui1630 2011-11-11 02:37
浏览 73
已采纳

将透明的`gif`转换为灰度,同时节省透明度

First, I resize the image while saving transparency:

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

if($blending){
    $transparentIndex = imagecolortransparent($canvas);
    if($transparentIndex >= 0){
        #GIF
        imagepalettecopy($canvas, $resize);
        imagefill($resize, 0, 0, $transparentIndex);
        imagecolortransparent($resize, $transparentIndex);
        imagetruecolortopalette($resize, true, 256);
    }else{
        #PNG
        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]()
*/

if($blending){          
    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!

展开全部

  • 写回答

1条回答 默认 最新

  • dpizd08264 2011-11-11 02: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.

    Update

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥15 MATLAB解决问题
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部