PHP GD 库将合并图像从绿色变为橙色

发布于 2024-12-26 07:30:08 字数 854 浏览 4 评论 0原文

我有两张 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
This is how it should look

This is how it ends up looking

The above is how it should look
The above is how it ends up looking

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

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

发布评论

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

评论(2

殊姿 2025-01-02 07:30:08

由于 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.

撧情箌佬 2025-01-02 07:30:08

谢谢你们的回复。我意识到我保存的 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

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