dongwei9771 2013-11-13 01:26
浏览 17
已采纳

水印压缩图像

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.

  • 写回答

1条回答 默认 最新

  • dongpan2788 2014-04-11 22:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵