PHP writes and reads image user-defined information _ exif or something?

Source: Internet
Author: User
PHP writes and reads image user-defined information _ exif or something? PHP image

Requirement description:

PHP is used to optimize image files, but this optimization process can only be performed once. secondary operations will cause irreparable damage to images. It is expected that after an image is processed, a custom string will be written into the image file. it can be used only to determine whether the image has been processed.


We look forward to using PHP for solutions. Currently, if you want to use the JPEG format, you can use the EXIF information to write a custom string in the EXIF information. But how can we solve common network Image formats like GIF, PNG, and BMP?

Google is here
Imagick: setImageProperty

According to document http://www.php.net/manual/zh/imagick.setimageproperty.php

It seems to have met the requirements, but I am not sure if I am using this function to save the image and read it again.

$image = new Imagick($file);$image->setImageProperty('Exif:Make', 'Imagick');$image->writeImage($file2);$image2 = new Imagick($file2);echo $image->getImageProperty('Exif:Make');



This is an idea, but it cannot be implemented. I really hope there is a problem with my usage, not that this function cannot be implemented.

Try APP13 again

$size = getimagesize('1.jpg', $info);var_dump($size);if(isset($info['APP13'])){    $iptc = iptcparse($info['APP13']);    var_dump($iptc);}


But it does not.

Have I been on a detour? Is there any good way? There are not many requirements, that is, to write a string in the image data on the basis of the fast image, for the next time to read this string.

Let's show up and give some advice to solve the problem.


Reply to discussion (solution)

For images supported by GD, you can append custom data after the image data ends without affecting the image content.
Sample code

$ Url = 'http: // response = imagecreatefromjpeg ($ url); imagepng ($ im, 'test.png '); // Generate a png image file $ s = 'abcefg '; // file_put_contents('test.png ', sprintf (' % sInfo % s', $ s, pack ('N', strlen ($ s), FILE_APPEND ); // append an image file in a custom format // read back $ s = file_get_contents('test.png '); $ t = unpack ('a4t/noffs', substr ($ s, -6); // The length of the retrieved custom information $ v = substr ($ s,-6-$ t ['offs'],-6 ); // retrieve custom information echo $ v; // abcdef

Verify that the image data is not damaged.

In fact, there is a general concept: streaming data can be read and displayed while reading. the header file shows the mimetype and length (or the ending mark ), the "extra" data after the length (or ending sign) does not affect the display of the subject Data. in this case, even rar and zip data can be used.

Thank you for your answers. The idea is the same, that is, to insert a character segment out of the data format of the file.

I also thought of this method at first. I edited several files manually, and the GIF dynamic image was damaged after it was inserted. At that time, I thought this method was not feasible and I did not continue to explore it. I used the damaged GIF with manual modification and tested it with xuzuning code. it seems that manual modification does not work.

This solution seems to be basically feasible, so the problem is that I am afraid I cannot test all the image formats. is this method deterministic? (As long as GD is supported, there will be no problem ?)

PS: I will first test several formats of images.

The Imagick class can be used for implementation. the imagemagick extension module needs to be installed in PHP.

I already mentioned it when I posted a post. google has an Imagick function, but it cannot be written. I don't know how to implement it with Imagick mentioned above?

Yes, GD supports all images (gif, jpg, and png)
Because these images are compressed and saved, there is a data area length table in the file header.
When the image is displayed, the software uses this identifier to read and extract the data.

However, GD files, such as bmp files, are not so lucky.
Bmp data is arranged from the end of the file to the end of the file. if you add content at the end of the file, the image data will be damaged.

In fact, the simplest test method is to prepare your binary data and the original file, and then use the doscommand

Copy/B original file + new binary data file

Check whether the new file is correct.

Use this solution first. there is a PHP class or something that can be compatible with various image formats. I think this requirement is quite common. for example, you can add an attribute to the image to determine whether the image has been watermark.

Close the post. thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.