I have script that adds a watermark to some pictures in my photographic blog website. The watermarked file is served to the browser and leaves the original untouched. This part is working OK.
Recently I've found out that using simply GD for the watermark is stripping important information from the original file, the EXIF data. I've found that the solution is using PEL, so I need some help using it.
I was able to figure out in how to install PEL, but was unable to get it to copy EXIF data from $original_image
to $new_image
.
$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();
$tiff = $exif->getTiff();
$ifd0 = $tiff->getIfd();
$exif = $ifd0->getSubIfd(PelIfd::EXIF);
$ifd1 = $ifd0->getNextIfd();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
This also produces an error like this:
Catchable fatal error: Argument 1 passed to lsolesen\pel\PelJpeg::setExif() must be an instance of lsolesen\pel\PelExif, instance of lsolesen\pel\PelIfd given, called in /var/www/html/clerigo/exif.php on line 71 and defined in /var/www/html/clerigo/pel/src/PelJpeg.php on line 304.
[EDIT] Ok, managed to make this work like this:
$jpeg = new PelJpeg($original);
$exif = $jpeg->getExif();
/*
creates copy of $original_image to $new_image, adds watermark to $new_image
*/
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
ImageJPEG($new_image, "new_image.jpg");
$jpeg->saveFile("/var/www/html/clerigo/new_image.jpg");
The thing is, this is saving an image to a file, and the purpose is NOT to save any image, but serving it only to the browser on request, like this:
$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
$jpeg->saveFile($new_image);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);
imagedestroy($new_image);
But, this results in an error:
Warning: file_put_contents() expects parameter 1 to be a valid path, resource given in /var/www/html/clerigo/pel/src/PelJpeg.php on line 600
Any ideas how to solve this?