PHP Imagick:如何將自定義圖像屬性保存到文件 (PHP Imagick: How to save custom image property to file)


問題描述

PHP Imagick:如何將自定義圖像屬性保存到文件 (PHP Imagick: How to save custom image property to file)

我有一個 PHP 應用程序,它正在使用 Image Magick (Imagick()) 處理大量圖像,並且我正在嘗試將一段計算數據保存到圖像中以供讀取(如果可用)節省處理時間或使用 Imagick::setImageProperty()Imagick::getImageProperty() 計算並保存以備下次使用。我在 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'));
    }
}

前2次成功讀取該屬性,但是一旦根據保存的圖像實例化一個新的Imagick對象,該屬性就無法讀取並返回false

string(4) "rawr" string(4) "rawr"

在此處輸入圖片描述

bool(false )

無法將自定義圖像屬性保存到文件中,還是我濫用了該類?

PS...我故意不使用 CodeIgniter 圖像庫,但是如果這對這種情況有幫助,我願意接受這個想法。


參考解法

方法 1:

This seems to be a limitation in the ImageMagick library and the JPEG image format. The short version is that only properties named 'comment' are persisted by the ImageMagick library for JPG images. 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. i.e. take the image file name, append ".json" and store the calculated data in there.

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'

(by Jon BDanack)

參考文件

  1. PHP Imagick: How to save custom image property to file (CC BY‑SA 2.5/3.0/4.0)

#image-processing #imagick #codeigniter #PHP






相關問題

在 matlab 中用 imread 讀取圖像文件會給出什麼樣的表示? (reading a image file with imread in matlab gives what kind of representation?)

使用 CRF 的圖像標記性能 (Image labeling performance using CRF)

Opencv:獲取圖像中的段大小並刪除小段 (Opencv: Get segments sizes in image and remove small segments)

將 PHP 頁面作為圖像返回 (Return a PHP page as an image)

我在哪裡可以找到有關雙三次插值和 Lanczos 重採樣的好讀物? (Where can I find a good read about bicubic interpolation and Lanczos resampling?)

從圖像中刪除白色背景 (Remove white backgrounds from images)

如何填充投影圖像的空白部分? (How to fill empty parts of a projected image?)

如何使圖像亮度均勻(使用 Python/PIL) (How to Make an Image Uniform Brightness (using Python/PIL))

圖像處理公式可生成類似通過 Mac 相機拍攝的照片的效果 (Image manipulation formula to generate effects like pictures taken via Mac's camera)

從照片生成漂亮的直方圖? (generating nice looking histogram from photo?)

使用 DjVu 工具進行背景/前景分離? (Using the DjVu tools to for background / foreground seperation?)

如何操縱跟踪器區域使其變成方形? (How can I manipulate the tracker area to make it into a square shape?)







留言討論