0

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?

jaclerigo
  • 557
  • 2
  • 6
  • 31

2 Answers2

0

Take a close look at what you're doing here:

$jpeg = new PelJpeg($new_image);
$jpeg->setExif($exif);
header("Content-Type: image/jpeg");
ImageJPEG($new_image);

You're creating a new image called $jpeg and applying the EXIF data to it, but then you're outputting $new_image with ImageJPEG(). You'll need to make a call to PelJpeg::saveFile() to save the changes you've made and then serve up that file.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • Hi, thank you for helping. But there is an error before: 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. Can you help me figure this one out? – jaclerigo Nov 17 '15 at 09:49
0

OK, figured it out, here is the final and working code:

$jpeg = new PelJpeg($original_image);
$exif = $jpeg->getExif();

/*
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");
echo $jpeg->getBytes();
jaclerigo
  • 557
  • 2
  • 6
  • 31