简体   繁体   English

PHP Imagick:如何将自定义图像属性保存到文件

[英]PHP Imagick: How to save custom image property to file

I have a PHP app that is processing a large collection of images using Image Magick ( Imagick() ) and am attempting to save a piece of calculated data to the images to be read if available to save processing time or to be calculated and saved for use next time using Imagick::setImageProperty() and Imagick::getImageProperty() .我有一个 PHP 应用程序,它正在使用 Image Magick ( Imagick() ) 处理大量图像,并尝试将一段计算数据保存到要读取的图像中,如果可用以节省处理时间或计算并保存下次使用Imagick::setImageProperty()Imagick::getImageProperty() I have a test controller in CodeIgniter 2 as follows:我在 CodeIgniter 2 中有一个测试控制器,如下所示:

class Example extends CI_Controller
{
    public function __construct()
    {
         parent::__construct();
    }

    public function property()
    {
        $im = new Imagick();
        $im->newimage(50, 50, 'blue');
        $im->setimageformat('jpg');
        $im->setimageproperty('My-App:My-Prop', 'rawr');
        var_dump($im->getimageproperty('My-App:My-Prop'));
        $im->writeimage(getcwd().'/images/test/output.jpg');
        echo '<br />'.var_dump($im->getimageproperty('My-App:My-Prop'));
        echo '<img src="/images/test/output.jpg" /><br />';

        $this->read();
    }

    public function read()
    {
        $im = new Imagick(getcwd().'/images/test/output.jpg');
        var_dump($im->getimageproperty('My-App:My-Prop'));
    }
}

The property is successfully read the first 2 times, but once a new Imagick object is instantiated based off the saved image, the property cannot be read and returns false :该属性在前 2 次成功读取,但是一旦基于保存的图像实例化新的Imagick对象,则无法读取该属性并返回false

string(4) "rawr" string(4) "rawr"字符串(4) "rawr" 字符串(4) "rawr"

在此处输入图片说明

bool(false)布尔(假)

Is saving a custom image property to a file not possible, or am I misusing the class?无法将自定义图像属性保存到文件中,还是我滥用了该类?

PS...I'm purposefully not using the CodeIgniter image library, but if that can help in this situation, I am open to that idea. PS...我故意不使用 CodeIgniter 图像库,但如果在这种情况下可以提供帮助,我对这个想法持开放态度。

This seems to be a limitation in the ImageMagick library and the JPEG image format.这似乎是 ImageMagick 库和 JPEG 图像格式的限制。 The short version is that only properties named 'comment' are persisted by the ImageMagick library for JPG images.简短的版本是 ImageMagick 库为 JPG 图像仅保留名为“评论”的属性。 See the code below for an example.有关示例,请参阅下面的代码。

TBH I think the sensible thing to do would be to store your meta information separately from the image file. TBH 我认为明智的做法是将您的元信息与图像文件分开存储。 ie take the image file name, append ".json" and store the calculated data in there.即取图像文件名,附加“.json”并将计算出的数据存储在那里。

It will work, will avoid re-saving the images if they haven't been modified and generally be a more robust solution than storing it in the image file.它将起作用,如果图像未被修改,将避免重新保存图像,并且通常是比将其存储在图像文件中更强大的解决方案。

$propertyNames = [
    "comment",
    "anything_else"
];

$formats = [
    'jpg',
    'png'
];

foreach ($formats as $format) {
    foreach ($propertyNames as $propertyName) {
        $imagick = new Imagick('./LittleRobin.jpg');
        $imagick->setImageProperty($propertyName, "Modified value");
        $imagick->setFormat($format);
        $imagick->writeImage("./testModified.".$format);
        $imagick2 = new Imagick("./testModified.".$format);
        printf(
            "After reloading '%s' property '%s' is:%s\n",
            $format,
            $propertyName,
            var_export($imagick2->getImageProperty($propertyName), true)
        );
    }
}

Output is:输出是:

After reloading 'jpg' property 'comment' is:'Modified value'
After reloading 'jpg' property 'anything_else' is:false
After reloading 'png' property 'comment' is:'Modified value'
After reloading 'png' property 'anything_else' is:'Modified value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM