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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度