gpt4 book ai didi

php - imagecreatefromstring 更改图像颜色

转载 作者:可可西里 更新时间:2023-11-01 13:22:09 33 4
gpt4 key购买 nike

我在 PHP 中调整图像大小时遇到​​问题;似乎从我使用 imagecreatefromstringimagecreatefrompng 加载图像的那一刻起,颜色就会改变并褪色。

我知道我必须使用 imagecreatetruecolor 来创建目标图像,但我什至没有做到这一点。

这里有几个例子来解释我得到的结果:

// This results in a discolored / faded image
$image = imagecreatefrompng('/path/to/my/image.png');
header('Content-Type: image/png');
imagepng($image);
die();
// This also results in a discolored / faded image
$image = imagecreatefrompng('/path/to/my/image.png');
$info = getimagesize('/path/to/my/image.png');
$sourceWidth = $info[0];
$sourceHeight = $info[1];

$resizedImage = imagecreatetruecolor($sourceWidth, $sourceHeight);
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $sourceWidth, $sourceHeight, $sourceWidth, $sourceHeight);

header('Content-Type: image/png');
imagepng($resizedImage);
die();
// Obviously, this works flawlessly.
header('Content-Type: image/png');
echo file_get_contents('/path/to/my/image.png');
die();

这是前后对比的例子: Original on the left, faded on the right

显然,我一定遗漏了一些东西,但我已经查看了我能找到的每一个 SO 问题和答案,但没有找到解决我问题的方法。

你遇到过这个问题吗?我应该怎么做?

最佳答案

这个问题可能与颜色配置文件有关,因为 GD 似乎不支持颜色配置文件。例如,如果您的图像位于比 sRGB 具有更多颜色信息的 Adob​​e RGB 色彩空间中,则可能会发生这种情况。以下是有关该主题的更多信息:

https://devot-ee.com/add-ons/support/ce-image/viewthread/1085

一个可能的解决方案是使用 Photoshop 将图像转换为 sRGB(导出时有一个“转换为 sRGB”复选框)。

如果这不可能,您可以使用 ImageMagick而不是 GD,它(如上面的链接中所述)确实支持颜色配置文件。

关于php - imagecreatefromstring 更改图像颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56622658/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com