2

Is there a way to add metadata (such as keywords) to several png images using php? I have stored the images in my database, and I want to do the same with their corresponding keywords, but I haven't found any helpful suggestions using php yet. Thanks

image.php

<!DOCTYPE html>
<html lang="en">
<head>
 <title>Upload Images</title>
</head>
 <body>
 <form action="tags.php" method="POST" enctype="multipart/form-data" >
  <p>Select Image (one or multiple):</p>
  <input type="hidden" name="MAX_FILE_SIZE" value="536870912"/>
  <input type="file" name="image[]" accept="image/png" accept="image/jpg" accept="image/jpeg" accept="image/gif" multiple="multiple" />
  <input type="submit" value="Upload file" name="submit" />

 </form>
 </body>
</html>

tags.php (Raptor's answer)

<?php
include('../config.php');

$image = new Imagick();
$image->readImage("C:/xampp/htdocs/cmanager/uploads");
$image->setImageProperty('keywords', 'Imagick');

print($image->getImageProperty('keywords'));
joasa
  • 946
  • 4
  • 15
  • 35

2 Answers2

2

You need ImageMagick's setImageProperty() help to achieve your goal.

<?php
$image = new Imagick();
$image->newImage(300, 200, "black"); // or load your PNG into Imagick

$image->setImageProperty('keywords', 'Imagick');
echo $image->getImageProperty('keywords');
?>

Require PHP to compile against ImageMagick 6.3.2+.

Alternatively, you can parse the PNG's metadata with the codes shown here. Try it out.

Last, if you intend to edit EXIF data instead, you can use the EXIF functions.

Community
  • 1
  • 1
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • Thanks for your suggestions Raptor. How can I compile PHP against ImageMagick 6.3.2+? – joasa Mar 10 '16 at 10:25
  • You can find the installation instructions [here](http://php.net/manual/en/imagick.setup.php). – Raptor Mar 10 '16 at 10:27
  • I followed the instructions for Windows installation, and every time I run the above code, I get a message " Class 'Imagick' not found". I also followed this [http://stackoverflow.com/a/20599861/5945369], but still the same problem. Do you have any ideas on how to fix this issue? – joasa Mar 10 '16 at 12:46
  • Did you restart your web server? You can always check whether `ImageMagick` is installed & enabled via `phpinfo()` – Raptor Mar 11 '16 at 02:30
  • Yes it works now, but I have 2 issues: instead of adding a new picture, I want it to choose a picture from the directory they are stored. So instead of `$image ->newImage(300, 200, "black");`, I tried `$image->readImage("C:/xampp/htdocs/cmanager/uploads");`, but no luck. It seems I have to choose a specific picture everytime I run the code, which makes no sense. Also, I am not sure how exactly Imagick could help and what it would print regarding the tags. – joasa Mar 11 '16 at 08:52
  • You can always use a `` to pick a file and form submit the script via multipart POST file upload. – Raptor Mar 11 '16 at 09:07
  • Yes, I have done this already, but I still get this error: Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `C:/xampp/htdocs/cmanager/uploads/' @ error/constitute.c/ReadImage/550' in C:\xampp\htdocs\cmanager\tags.php:22 Stack trace: #0 C:\xampp\htdocs\cmanager\tags.php(22): Imagick->readimage('C:/xampp/htdocs...') #1 {main} thrown in C:\xampp\htdocs\cmanager\tags.php on line 22 – joasa Mar 11 '16 at 09:12
  • From the exception, your script didn't read the file at all. You're reading the folder `uploads/` instead. Double check your code. – Raptor Mar 11 '16 at 09:24
  • I added the code in the question, so that it is more clear. :) – joasa Mar 11 '16 at 09:46
  • Your code does not make sense at all. You're not reading the `$_FILES`. Also, the HTML syntax is incorrect. – Raptor Mar 11 '16 at 10:50
  • Hi Raptor, I am also looking at solutions to this same issue as the OP, I have read your answer and am curious if you know the type of MetaData that ImageMagick can edit as I read there are various metadata formts: http://stackoverflow.com/a/18688718/3536236 – Martin Mar 12 '16 at 08:01
1

Also, you can set PNG metadata in pure PHP (example is about how to set a pHYs chunk).

soulmare
  • 73
  • 1
  • 7