phpexifiptc

How to add keywords in image file with PHP?


I try to add keywords with this method

//echoing the img to check the path
echo "<img src='uploads/Background597x1050px.jpg'>";

//creating the object and use of setImageProperty method to add keywords
$imgi = new Imagick('uploads/Background597x1050px.jpg');
$imgi->setImageProperty('keywords','test');

When I download the image, nothing appears in the keyword part (screenshot in french) enter image description here

Edit: I also tried with the iptcembed method, but then the tag returned by iptcparse are only those and if I had tag added from windows property, they aren't return but with exif_read_data only and not the iptcembed ones


Solution

  • So, you cannot use ImageMagick to do what you are trying to do and that is a bummer -- but that does not mean you are out of options. You have two that I can think of off the top of my head for dealing with EXIF and ITPC data in JPEG images:

    1. Use a 3rd party PHP library designed to work with EXIF data. The one I am specifically thinking of is called Pel and supports JPEG and TIFF images. From what I have read, it can be tricky to use and the documentation is sparse, but may be worth a try.

    2. Another option is called ExifTool, a handy command line utility that actually supports, EXIF, ITPC, XMP, among others (don't let the name fool you). If you add this command line tool to your server, you can then call it via PHP shell type commands and process the image that way. Just be aware that if the image is user input, you must be very careful to validate the image is actually an image, that the exif data doesn't have a payload injected, and do your absolute best to try to write as secure code as possible. One bit of code I found online calls ExifTool using exec(), but also calls escapeshellcommand() to clean the arguments when building the call. I suspect if you dig further up in that code, the images themselves are also being validated with things like getimagesize() and other techniques.