Setting Image Properties

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
Mitron

Setting Image Properties

Post by Mitron »

I was checking out Mikko's blog entry "Analyzing Image Properties" and read the documentation on the getImageProperties() and setImageProperties() function. I successfully read the Exif data on one of my JPG images and noticed there was no Artist, Copywrite, Image Description or User Comment listed because my camera doesn't add those fields.

Using the setImageProperties() function, I attempted to write those properties to my image but for some reason it won't save the additions when I writeImage() to another JPG file. However, if I change it to a PNG file, the properties I set are saved. If I load the PNG file with the changes in tact and simply change it to a JPG, somehow the properties of the original JPG appear.

Here's the code I was trying which basically loads a jpg, displays the properties, adds a few properties, saves it to a png file then loads it and reads the properties:

Code: Select all

$im = new Imagick( 'images/file.jpg' );

$impng = $im->clone();
$impng_properties = $impng->getImageProperties();
foreach( $impng_properties as $key => $value ) {
	echo $key . '=>' . $value . '<br />';
}
echo '<br />';
$impng->setImageProperty( "exif:Artist", "My Name" ); 
$impng->setImageProperty( "exif:Copyright", "Copyright, My Name, 2008. All rights reserved." ); 
$impng->setImageProperty( "exif:DateTime", date('Y:m:d H:i:s') );
$impng->setImageProperty( "exif:ImageDescription", "My PhotoSet" ); 
$impng->setImageProperty( "exif:UserComment", "Comment goes here" ); 
$impng->setImageFormat( 'png' );
$impng->writeImage( 'images/file.png' );
$impng->clear();
$impng->destroy();

$im2 = new Imagick( 'images/file.png' );
$imjpg = $im2->clone();

$imjpg_properties = $imjpg->getImageProperties();
foreach( $imjpg_properties as $key => $value ) {
	echo $key . '=>' . $value . '<br />';
}
echo '<br />';
On my setup it displays the original properties of the JPG and the new properties of the PNG file. However, if you then write the second file (PNG) as a JPG, somehow the properties of the original JPG file appear without the changes you made to the PNG file?

Code: Select all

$im = new Imagick( 'images/file.jpg' );

$impng = $im->clone();
$impng_properties = $impng->getImageProperties();
foreach( $impng_properties as $key => $value ) {
	echo $key . '=>' . $value . '<br />';
}
echo '<br />';
$impng->setImageProperty( "exif:Artist", "My Name" ); 
$impng->setImageProperty( "exif:Copyright", "Copyright, My Name, 2008. All rights reserved." ); 
$impng->setImageProperty( "exif:DateTime", date('Y:m:d H:i:s') );
$impng->setImageProperty( "exif:ImageDescription", "My PhotoSet" ); 
$impng->setImageProperty( "exif:UserComment", "Comment goes here" ); 
$impng->setImageFormat( 'png' );
$impng->writeImage( 'images/file.png' );
$impng->clear();
$impng->destroy();

$im2 = new Imagick( 'images/file.png' );
$imjpg = $im2->clone();

$imjpg_properties = $imjpg->getImageProperties();
foreach( $imjpg_properties as $key => $value ) {
	echo $key . '=>' . $value . '<br />';
}
echo '<br />';
$imjpg->setImageFormat( 'jpg' );
$imjpg->writeImage( 'images/file2.jpg' );
$imjpg->clear();
$imjpg->destroy();

$im3 = new Imagick( 'images/file2.jpg' );

$im2jpg = $im->clone();
$im2jpg_properties = $im2jpg->getImageProperties();
foreach( $im2jpg_properties as $key => $value ) {
	echo $key . '=>' . $value . '<br />';
}
echo '<br />';
I'm thinking that maybe the setImageProperties() function isn't quite working as it should? It would be nice to be able to set these properties for those cameras that do not.
Post Reply