0

我的服务器上存储了一组黑白 JPEG。这些图像是基于符号的,其中符号是白色背景上的黑色线条的集合。

我正在尝试使用 GD 根据传递的变量动态地将黑色替换为另一种颜色。目前,我是:

获取 JPEG 为:$image = imagecreatefromjpeg($imgURL),通过 PHP 将 HEX 代码(例如#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;
}

由于某种原因,该功能根本不起作用(它不会覆盖新颜色)。

谁能告诉我哪里出错了?

非常感谢。

4

2 回答 2

0

您可以使用该imageistruecolor功能来确定您刚刚加载的 JPEG 是真彩色还是基于调色板的。如果它不是真彩色,您可以创建一个相同宽度和高度的新真彩色图像,并将旧图像复制过来:

$width = imagesx($jpeg);
$height = imagesy($jpeg);
$image = imagecreatetruecolor($width, $height);
imagecopy($jpeg, $image, 0, 0, 0, 0, $width, $height);

然后,您应该能够应用新颜色。

于 2009-05-04T21:59:00.137 回答
0

如果颜色是唯一的问题,那么你可以试试这个:

      <php>
      //剪辑
      $color = preg_replace('/^#/','',$color); //如果有“#”就去掉
      $r = hexdec("0x{$color[0]}{$color[1]}");
      $g = hexdec("0x{$color[2]}{$color[3]}");
$b = hexdec("0x{$color[4]}{$color[5]}"); //剪辑 </php>

于 2009-05-04T19:15:59.310 回答