PHP GD 库将合并图像从绿色变为橙色
我有两张 gif 图像,一张带有灰色渐变背景,一张带有徽标。例如,在下图中,绿色框是徽标。
我的问题是,当我使用 PHP GD 库合并两个 gif 文件时,绿色会变成橙色/棕色,并且不会返回原始颜色。请有人指出我做错了什么?
$base_image = 'img_in.gif';
$logo_image = 'logo.gif';
// Create images from files
$source = imagecreatefromgif($base_image);
$logo = imagecreatefromgif($logo_image);
//---------------------Logo-----------------------------------\\
imagecolortransparent($logo, imagecolorallocate($logo, 0, 0, 0));
imagecopymerge($source, $logo, 152, 33, 0, 0, 153, 26, 100);
// -------------------------------Output--------------------------\\
imagegif($source,$image_out);
imagedestroy($source);
绿色的就是它应该的样子 橙色的就是它最终的样子
上面是它应该看起来的样子 上面是它最终的样子
I have two gif images, one with a gray gradient background, and one image with a logo. So for example, in the images below, the green box is the logo.
My problem is that when I merge the two gif files using the PHP GD library, the green some how becomes orange/brown and won't return the original colour. Please can someone point out what I'm doing wrong?
$base_image = 'img_in.gif';
$logo_image = 'logo.gif';
// Create images from files
$source = imagecreatefromgif($base_image);
$logo = imagecreatefromgif($logo_image);
//---------------------Logo-----------------------------------\\
imagecolortransparent($logo, imagecolorallocate($logo, 0, 0, 0));
imagecopymerge($source, $logo, 152, 33, 0, 0, 153, 26, 100);
// -------------------------------Output--------------------------\\
imagegif($source,$image_out);
imagedestroy($source);
The green one is how it should look
The orange one is how it ends up looking
The above is how it should look
The above is how it ends up looking
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于 GIF 是基于调色板的,因此每个 GIF 只能使用 256 种颜色 - 在其调色板中分配的那些。
如果将一个 GIF 复制到另一个 GIF 中,库需要以某种方式使调色板同质化。
我建议将这些 GIF 转换为 PNG(或某种具有更多颜色的格式),进行处理,最后转换回 GIF。
一般来说,结果应该看起来更好。
Since GIFs are pallet-base, each GIF can use 256 colors only - those allocated in its palette.
If you copy one GIF into another one, the library needs to somehow homogenize palettes.
I'd propose to convert those GIFs to PNG (or some format with more colors), do the processing and finally convert back to GIF.
In general, results should look better.
谢谢你们的回复。我意识到我保存的 gif 文件只有 63 种颜色,而不是 256 种颜色!哎哟!无论如何,谢谢
Thanks for the replies guys. I realised that I was saving the gif with only 63 colours and not 256! Doh! Thanks anyway