将透明“gif”转换为灰度,同时保存透明度

发布于 2024-12-14 22:48:51 字数 1586 浏览 2 评论 0原文

首先,我在保存透明度的同时调整图像大小:

/*
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)

然后,我想将灰度滤镜应用到之前保存的图像(在新函数内):

/*
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]

修复什么,以在将 IMG_FILTER_GRAYSCALE 应用于 gif 时保留透明度

为了在保存透明度的同时将 gif 转换为灰度,需要解决什么问题? (由于@Pierre提供的答案而修改了问题)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2024-12-21 22:48:51

与灰度滤镜一起使用的 imagefilter 确实可以处理 Alpha 通道。

但是,gif 不支持 Alpha。所以你将无法使用 GIF 格式存储它。

还需要注意的是,背景颜色(用作背景的单一颜色或颜色索引)与 Alpha(给定颜色或像素的透明度级别)无关。这意味着您有责任将背景颜色设置为所需的单色。

更新

这不是直接可能的,因为用作透明的颜色将被修改。这可以被视为一个错误,因为透明颜色可能或应该被过滤器忽略。但这是另一个话题了。

解决方法可能是:


$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');

此代码只需获取现有透明颜色的值,将其转换为灰色并将其设置回颜色索引。此代码仅适用于像 gif 这样的调色板图像,但这就是想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文