使用 PHP/GD 在 JPEG 上叠加颜色?
我的服务器上存储了一组黑白 JPEG。 这些图像是基于符号的,其中符号是白色背景上的黑线的集合。
我正在尝试根据传递的变量使用 GD 将黑色替换为另一种颜色。 目前,我正在:
将 JPEG 获取为:$image = imagecreatefromjpeg($imgURL), 通过 PHP 将十六进制代码(例如#FF0000)转换为 RGB,
然后将这些变量提供给:
private function colourize_image($image, $colour, $contrast = 0) {
if (!$image) { return false; }
imagealphablending($image, true);
imagesavealpha($image, true);
# Convert hex colour into RGB values
$r = hexdec('0x' . $colour{0} . $colour{1});
$g = hexdec('0x' . $colour{2} . $colour{3});
$b = hexdec('0x' . $colour{4} . $colour{5});
imagefilter($image, IMG_FILTER_COLORIZE, $r, $g, $b);
imagefilter($image, IMG_FILTER_CONTRAST, $contrast);
# Return the GD image object
return $image;
}
由于某种原因,该函数根本不起作用(它不会覆盖新颜色)。
谁能告诉我哪里出了问题?
非常感谢。
I have a collection of black and white JPEG's stored on my server. These images are symbol based, where the symbol is a collection of black lines on a white background.
I am trying to use GD to replace the black colour with another colour on the fly based on a variable passed. Currently, I am:
Getting the JPEG as: $image = imagecreatefromjpeg($imgURL),
Converting a HEX code (#FF0000, say) to RGB through PHP,
And then feeding these variables to:
private function colourize_image($image, $colour, $contrast = 0) {
if (!$image) { return false; }
imagealphablending($image, true);
imagesavealpha($image, true);
# Convert hex colour into RGB values
$r = hexdec('0x' . $colour{0} . $colour{1});
$g = hexdec('0x' . $colour{2} . $colour{3});
$b = hexdec('0x' . $colour{4} . $colour{5});
imagefilter($image, IMG_FILTER_COLORIZE, $r, $g, $b);
imagefilter($image, IMG_FILTER_CONTRAST, $contrast);
# Return the GD image object
return $image;
}
For some reason, the function doesn't work at all (it won't overlay a new colour).
Can anyone advise as to where I am going wrong?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果颜色是唯一的问题,那么你可以尝试以下方法:
If the color is the only problem, then you could try this:
您可以使用
imageistruecolor
函数来确定您刚刚加载的JPEG是真彩色还是基于调色板的。 如果它不是真彩色,您可以创建一个具有相同宽度和高度的新真彩色图像,然后复制旧图像:然后您应该能够应用新颜色。
You can use the
imageistruecolor
function to find out whether the JPEG you've just loaded is true colour or palette-based. If it's not true colour, you can create a new true color image of the same width and height, and copy the old image over:You should then be able to apply the new colours.