【问题标题】:Error Using HALD CLUT with ImageMagick (Black lines on output image)使用带有 ImageMagick 的 HALD CLUT 时出错(输出图像上有黑线)
【发布时间】:2020-08-21 15:35:21
【问题描述】:

我是一个预建网站的新开发人员,试图弄清楚如何使用设置 ImageMagick 工具。我们使用它的方式是使用 HALD CLUT 滤镜自动更改产品的颜色。

我昨天让它运行了,但输出图像上到处都是黑线。我在产品图像和 HALD CLUT 过滤器上测试了更改文件类型/格式,但没有运气,所以我假设它与 ImageMagick 有关。

Screen capture of outputted image

有人知道这是什么原因吗?

这是 ImageMagick 颜色生成器的代码:

class SwatchGenerator {
    public $mask;
    public $swatches;
    public $types;
    public $th;
    public $base_url;
    public $results;
    public $type;
    public $color;
    public $output;

    function __construct ($overwrite = false) {
        $this->th = \Loader::helper('text');
        $this->base_url = \View::url('/');
        $this->overwrite = $overwrite;
        $this->delimiter = '~';
        $this->results = [];

        $this->loadSwatches();
        $this->loadTypes();
    }

    function loadSwatches () {
        $swatches = \Express::getObjectByHandle('swatch')->getEntries();

        foreach ($swatches as $swatch) {
            $handle = $this->th->urlify($swatch->getSwatchName());
            $filter = $swatch->getSwatchFilter();
            $this->swatches[$handle] = (is_object($filter)) ? $this->getImagickInstance($filter) : false;
        }
    }

    function loadTypes () {
        $types = \Concrete\Core\File\Image\Thumbnail\Type\Type::getVersionList();

        foreach ($types as $type) {
            $type_handle = $type->getHandle();

            if (strpos($type_handle, '_2x') === false && strpos($type_handle, 'product_') !== false) {
                $this->types[$type_handle] = $type;
            }
        }
    }

    function setType ($type) {
        $this->type = $type;
    }

    function setColor ($color) {
        $this->color = $color;
    }

    function setOutput ($output) {
        $this->output = $output;
    }

    function log ($message) {
        $this->addResult($message);

        // \Log::addEntry($message);
    }

    function addResult ($message) {
        $this->results[] = $message;
    }

    function getResults () {
        return $this->results;
    }

    function getResultLog ($delimiter = "\n\r") {
        return implode("\n\r", $this->getResults());
    }

    function generate ($base_file, $allowed_type = false) {
        $mask_file = $base_file->getAttribute('color_mask');

        if (!is_object($mask_file)) {
            $this->log('Mask not found.');
            return;
        }

        if ($base_file->getAttribute('width') != 2560) {
            $this->log('Invalid image size found. Must be 2560px wide.');
            return;
        }

        // these are our core images that should not change for any swatch
        $base = $this->getImagickInstance($base_file);

        // we do this to remove any trace of transparency and flatten the file
        // resolves issues with odd masking we were seeing
        $base->setImageBackgroundColor('white');
        // $base->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
        $base->setImageAlphaChannel(11);
        $base->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);

        // jpg output
        $base->setImageFormat('jpg');
        $base->setImageCompressionQuality(85);

        $mask = $this->getImagickInstance($mask_file);
        $overlay = clone ($base);
        $overlay->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);

        // base path to write the swatches to
        $base_file = $this->parsePath($base_file->getRelativePath());
        $pathinfo = pathinfo($base_file);

        // stores our composited swatch images
        $swatches = [];

        // now create an image for each swatch we have
        foreach ($this->swatches as $swatch_handle => $filter) {
            // if we are limited to a color, then abort if we are not on it
            if ($this->color && $this->color != $swatch_handle) {
                continue;
            }

            $swatch = clone ($base);

            if ($filter) {
                $swatch->haldClutImage($filter);
            }

            $swatch->compositeImage($overlay, imagick::COMPOSITE_DEFAULT, 0, 0);

            $swatches[$swatch_handle] = $swatch;

            foreach ($this->types as $type_handle => $type) {
                // filter non-product types
                if ($allowed_type && $allowed_type !== $type_handle) {
                    continue;
                }

                // if we are limited to a type, then abort if we are not on it
                if ($this->type && $this->type != $type_handle) {
                    continue;
                }

                $output_path = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . $this->delimiter . $type_handle . $this->delimiter . $swatch_handle . '.jpg';

                if (file_exists($output_path) && !$this->overwrite) {
                    $this->log('Skipping existing file at ' . $output_path);
                } else {
                    $this->log('Rendering ' . $this->th->unhandle($swatch_handle) . ' at ' . $type->getWidth() . 'px (' . $type_handle . ')');

                    $output = clone ($swatch);
                    $output->resizeImage($type->getWidth(), 0, Imagick::FILTER_LANCZOS, 0);
                    $output->writeImage('../' . $output_path);

                    if ($this->output) {
                        $this->addResult("<img src='/{$output_path}' />");
                    }
                }

            }
        }
    }

    function getImagickInstance ($file, $type = false) {
        if (is_object($file)) {
            $path = ($type) ? $file->getThumbnailURL($type) : $file->getRelativePath();

            $image = new Imagick($this->parsePath('../' . $path));
            $image->setImageFormat('png');
            $image->setFormat('png');

            return $image;
        }
    }

    function parsePath ($path) {
        // strip leading slash
        $path = preg_replace('/^\//', '', $path);

        return str_replace($base_url, '', $path);
    }
}

我使用的是 PHP 7.0.3.3、Imagick 3.4.3 和 ImageMagick 6.9.6-2。

【问题讨论】:

  • 你能发布你的输入图像和你的HALD图像,这样我们就可以在命令行中测试了吗?您的 ImageMagick 版本非常旧。我建议你尝试升级。
  • 这里是图片:imgur.com/udcGu9F%20 imgur.com/QdZMMc7 我会考虑更新。我们也使用旧版本的 PHP,所以我不确定哪个是支持的最高版本。感谢您的回复!
  • 我似乎无法下载您的第一个链接图片

标签: php imagemagick imagick concrete5


【解决方案1】:

我无法访问您的原始图片。但我确实下载了你的 Hald 图像。所以我在命令行中将它应用到 ImageMagick 6.9.11.28 Q16 Mac OSX 中的以下图像,它工作正常 - 至少没有行。所以我怀疑你的 ImageMagick 版本或 Imagick 版本或你的代码有问题。您可以尝试在 PHP exec() 中运行我的命令,看看是否可行。它会告诉您 ImageMagick 是否正常工作。这也可能是您的输入图像或 libpng 委托版本的问题。

输入:

静止图像:

convert lena.jpg hald.png -hald-clut result.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多