2013-11-13 01:26



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));

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;

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:


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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongpan2788 dongpan2788 7年前

    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.

    点赞 评论 复制链接分享