【问题标题】:How to reduce image creation time of an image php GD如何减少图像php GD的图像创建时间
【发布时间】:2015-10-28 09:28:38
【问题描述】:

使用 GD 函数更改图像的颜色。由于多次迭代,创建需要大量时间。正如下面提到的脚本粉红色(255,0,255)和附近的颜色替换为其他动态颜色。脚本输出是正确的,但如前所述,创建输出图像需要大量时间。

是否可以减少彩色图像的创建时间 可更改的功能还是我们可以更改任何代码部分?

 function changeImageColor($oldColorTriplet, $newColorTriplet, $hueError = 0.4){
        if (!$this->isImageCreated())
            return false;

        $oldColorHSL = $this->RGBtoHSL($oldColorTriplet[0], $oldColorTriplet[1], $oldColorTriplet[2]);
        $newColorHSL = $this->RGBtoHSL($newColorTriplet[0], $newColorTriplet[1], $newColorTriplet[2]);
        $cx = $this->width();
        $cy = $this->height();
        for ($x = 0; $x < $cx; $x++) {
            for ($y = 0; $y < $cy; $y++) {
                $pixel = imagecolorsforindex($this->handle, imagecolorat($this->handle, $x, $y));

                $currentColorHSL = $this->RGBtoHSL($pixel['red'], $pixel['green'], $pixel['blue']);

                if (($currentColorHSL[0] >= $oldColorHSL[0] - $hueError)
                    && ($oldColorHSL[0] + $hueError >= $currentColorHSL[0])) {
                    //$color = $this->HSLtoRGB($newColorHSL[0], $newColorHSL[1], $currentColorHSL[2]);
                    $color = $newColorTriplet;
                    $color = imagecolorallocatealpha($this->handle, $color[0], $color[1], $color[2], $pixel['alpha']);
                    imagesetpixel($this->handle, $x, $y, $color);
                }
            }
        }

        return true;
    }

function RGBtoHSL( $r, $g, $b ){
        $r /= 255;
        $g /= 255;
        $b /= 255;
        $max = max( $r, $g, $b );
        $min = min( $r, $g, $b );
        $l = ( $max + $min ) / 2;
        $d = $max - $min;
        if( $d == 0 ){
            $h = $s = 0;
        } else {
            $s = $d / ( 1 - abs( 2 * $l - 1 ) );
            switch( $max ){
                case $r:
                    $h = 60 * fmod( ( ( $g - $b ) / $d ), 6 );
                    if ($b > $g) {
                        $h += 360;
                    }
                    break;
                case $g:
                    $h = 60 * ( ( $b - $r ) / $d + 2 );
                    break;
                case $b:
                    $h = 60 * ( ( $r - $g ) / $d + 4 );
                    break;
            }
        }
        return array( round( $h, 2 ), round( $s, 2 ), round( $l, 2 ) );
    }

    function HSLtoRGB( $h, $s, $l ){
        $c = ( 1 - abs( 2 * $l - 1 ) ) * $s;
        $x = $c * ( 1 - abs( fmod( ( $h / 60 ), 2 ) - 1 ) );
        $m = $l - ( $c / 2 );
        if ( $h < 60 ) {
            $r = $c;
            $g = $x;
            $b = 0;
        } else if ( $h < 120 ) {
            $r = $x;
            $g = $c;
            $b = 0;
        } else if ( $h < 180 ) {
            $r = 0;
            $g = $c;
            $b = $x;
        } else if ( $h < 240 ) {
            $r = 0;
            $g = $x;
            $b = $c;
        } else if ( $h < 300 ) {
            $r = $x;
            $g = 0;
            $b = $c;
        } else {
            $r = $c;
            $g = 0;
            $b = $x;
        }
        $r = ( $r + $m ) * 255;
        $g = ( $g + $m ) * 255;
        $b = ( $b + $m  ) * 255;
        return array( floor( $r ), floor( $g ), floor( $b ) );
    }

【问题讨论】:

标签: php image imagick


【解决方案1】:

我有其他方法可以减少图像创建时间:

  • 上传图片时,我会获取所有接近 rgb(255,0,255) 的像素并存储在 DB 中。
  • 比只调用那些像素 仅在这些位置替换颜色并实现上述代码。

  • 意思是假设一个图像,有 10,000 个像素 1000 个像素是 rgb(255,0,255) 比我保存这 1000 个像素位置并同时应用 颜色我只发送这 1000 个像素来改变颜色。

【讨论】:

    【解决方案2】:

    我不是 PHP+GD 方面的专家,但在我看来,您正在获取某个 HSL 颜色任一侧的带内的所有像素并用纯色替换它们 - 如果是这样,您可以使用 ImageMagick 快速完成在 PHP 中。我将尝试在命令行中展示这个概念,然后您可以对其进行基准测试,因为 ImageMagick 已安装在大多数 Linux 发行版上,并且可用于 OSX 和 Windows。

    首先让我们制作一个名为swatch.png 的渐变图像,用于测试,其中包含一些粉红色:

    convert -size 100x100 gradient:blue-pink gradient:pink-blue -rotate -90 +append swatch.png
    

    现在,让我们看看 ImageMagick 认为 pink 在 HSL 坐标中的样子:

    convert xc:pink -depth 8 -colorspace hsl txt:
    # ImageMagick pixel enumeration: 1,1,255,hsl
    0,0: (63628,65535,57440)  #F8FFE0  hsl(349.524,100%,87.6478%)
    

    因此,色相分量为 63628/65535、349.524/360 或 97%

    现在,如果我们取出样本并复制它,然后进入副本并转换为 HSL 色彩空间,分离出 H、S 和 L 通道,并删除饱和度和亮度,我们将只存在色调通道。如果我们现在让小于 96% 的所有内容变为黑色,然后使黑色透明,大于 98% 的所有内容变为白色,然后使白色透明,我们将在图像中只有色调介于 96-98% 之间的任何不透明度,即大约我们的粉红色。如果我们现在用石灰绿色填充图像并将其覆盖在原始图像之上,我们将看到我们已将粉红色调替换为石灰绿色。

    convert swatch.png                                 \
      \( +clone -colorspace hsl -separate -delete 1,2  \
         -black-threshold 96% -transparent black       \
         -white-threshold 98% -transparent white       \
         -fill lime -colorize 100%                     \
      \) -compose overlay -composite result.png
    

    如果我们将色相的下限公差扩大到 90%:

    convert swatch.png \( +clone -colorspace hsl -separate -delete 1,2 -black-threshold 90% -transparent black -white-threshold 98% -transparent white -fill lime -colorize 100% \) -compose overlay -composite result.png
    

    如果我们缩小色相的上限公差:

    convert swatch.png \( +clone -colorspace hsl -separate -delete 1,2 -black-threshold 96.0% -transparent black -white-threshold 97.0% -transparent white -fill red -colorize 100% \) -compose overlay -composite result.png
    

    使用 ImageMagick 还有其他方法可以做到这一点,但这应该会给您一个想法,您可以在实施任何 PHP 之前在命令行中对其进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2012-08-22
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多