douhui1630 2011-11-11 10: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 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.

    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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?