Question

I currently use the following code to watermark images on-the-fly and display them in a web page:

header('Content-type: image/jpeg');
$stamp = imagecreatefrompng(watermark.png');
$im = imagecreatefromjpeg($filename);
imagecopy($im, $stamp, 10, imagesy($im) - imagesy($stamp) - 10, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im);
imagedestroy($im);

I also have a lot of images that are stored in zip archives. I currently display them with the following code.

  $zip = new ZipArchive();
  $opened = $zip->open($zipname, ZIPARCHIVE::CHECKCONS);
  if ( $opened === true ){
    $content = $zip->getFromName($filename);
      header('Content-type: image/jpeg');
      echo $content;
    };  
    $zip->close();

I want to watermark them as well but cannot seem to get it to work. As an initial test I tried changing to output of $content from an echo to:

imagejpeg($content);

But that did not work, meaning adding the other watermark code will not work either. Any suggestions as to how to modify the zip code to include the watermark would be greatly appreciated.

I basically do not understand the difference between what is created in $im with imagecreatefromjpeg and what is extracted to $output from the zip archive. I assume that $output contains the raw jpg file data, but have no idea what $im contains.

Was it helpful?

Solution

UPDATE: Answered my own question. All I needed was to add the following after getting $content from the zip:

$im = imagecreatefromstring($content);

I can then apply the watermark exactly as in the first example. Hope that helps others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top