imagecreatefromstring 更改图像颜色
imagecreatefromstring changes image color
我在 PHP 中遇到图像大小调整问题;似乎从我使用 imagecreatefromstring
或 imagecreatefrompng
加载图像的那一刻起,颜色就会改变并变得褪色。
我知道我必须使用 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();
以下是之前和之后的示例:
显然,我一定遗漏了一些东西,但我已经查看了我能找到的每一个 SO 问题和答案,但没有找到解决我问题的方法。
你遇到过这个问题吗?
我应该怎么做?
这个问题可能与颜色配置文件有关,因为 GD 似乎不支持颜色配置文件。例如,如果您的图像位于比 sRGB 具有更多颜色信息的 Adobe RGB 色彩空间中,则可能会发生这种情况。以下是有关该主题的更多信息:
https://devot-ee.com/add-ons/support/ce-image/viewthread/1085
一个可能的解决方案是使用 Photoshop 将图像转换为 sRGB(导出时有一个 "convert to sRGB" 复选框)。
如果这不可能,您可以使用 ImageMagick 代替 GD,后者(如上文 link 所述)支持颜色配置文件。
我在 PHP 中遇到图像大小调整问题;似乎从我使用 imagecreatefromstring
或 imagecreatefrompng
加载图像的那一刻起,颜色就会改变并变得褪色。
我知道我必须使用 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();
以下是之前和之后的示例:
显然,我一定遗漏了一些东西,但我已经查看了我能找到的每一个 SO 问题和答案,但没有找到解决我问题的方法。
你遇到过这个问题吗? 我应该怎么做?
这个问题可能与颜色配置文件有关,因为 GD 似乎不支持颜色配置文件。例如,如果您的图像位于比 sRGB 具有更多颜色信息的 Adobe RGB 色彩空间中,则可能会发生这种情况。以下是有关该主题的更多信息:
https://devot-ee.com/add-ons/support/ce-image/viewthread/1085
一个可能的解决方案是使用 Photoshop 将图像转换为 sRGB(导出时有一个 "convert to sRGB" 复选框)。
如果这不可能,您可以使用 ImageMagick 代替 GD,后者(如上文 link 所述)支持颜色配置文件。