【问题标题】:Adding watermark with PHP使用 PHP 添加水印
【发布时间】:2012-09-21 12:24:20
【问题描述】:

这是我的 PHP 水印功能:

function img_watermark($image) {
    $stamp = imagecreatefrompng('images/wm.png');
    $im = imagecreatefromjpeg($image); 
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);

    // Copy the stamp image onto our photo using the margin offsets and the photo 
    // width to calculate positioning of the stamp. 
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

    // Output and free memory
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
}

这是我的 HTML 代码:

<img src="<?php img_watermark('images/ornek.jpeg');?>" />

但它只是给了我这样的东西:

.......���Nw��o�#¨�8����J����wz�V�W����>�{��� #������z�^����/?��7VWo?��������CRVS3ӷ������行��?�������}��Ϳ�� ��������O=q��~�?���IY� ?MvN�Y�����k�7[�hwg���������6�/|���~ᫍ(�� ��?p(�B����_?�sY���G>|�ŗ���V)%�\Z��� J���7/....... .

我希望它显示带水印的图像。我该如何解决这个问题?

【问题讨论】:

    标签: php image gd php-gd


    【解决方案1】:

    你有错误的设置。您的 HTML 应如下所示:

    <img src="image.php?src=images/ornek.jpeg" />
    

    image.php应该是这样的:

    $stamp = imagecreatefromjpeg('images/wm.png');
    $im = imagecreatefromjpeg($_GET['src']); 
    $marge_right = 10;
    $marge_bottom = 10;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);
    
    // Copy the stamp image onto our photo using the margin offsets and the photo 
    // width to calculate positioning of the stamp. 
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
    
    // Output and free memory
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
    

    【讨论】:

      【解决方案2】:

      您不能将二进制数据放入图像标签的src 属性中。

      src 属性通常需要一个图像的 URL。

      不过,您可以使用 base64 编码:

      $file = base64_encode(img_watermark('images/ornek.jpeg'));
      echo "<img src='data:image/jpeg;base64,".$file."' alt='watermarked image'>";
      

      并删除函数中的header 行,除非您的PHP 文件应该发送图像数据而不是HTML。最好不要把这些东西混在一起:(

      【讨论】:

      • &lt;img src="&lt;?php img_watermark('images/ornek.jpeg');?&gt;" /&gt; 在几个层面上显然是错误的,我的解决方案至少提供了一种解决它的可能性。我不知道为什么我的答案被否决而标题被投了赞成票。 image.php 解决方案需要将 GD 代码完全放在另一个文件中,我的回答可以在一个 PHP 脚本中完成所有操作。当然,由 OP 决定解决他的问题的方法。
      • 确实,您走在正确的轨道上。我不一定会推荐data: URIs 作为解决方案,但问题的根源肯定是正确的,并且解决方案是有效的。
      • 尽管我仍然不喜欢将 base64 放入 img 标签的声音,但我会投票赞成。这听起来对 SEO、初始页面大小、缓存和速度都不利。
      • 我不认为 src="data: ... 好。它对图标等小图像有优势,但如果是带水印的 JPEG 照片(我认为这是 OP 的情况),它会使 HTML 更大,加载速度更慢。
      • 我认为它既不好,也不会像那样实现它。我的和@Baba 的解决方案在每次显示时都会生成图像,从而占用服务器的大量 CPU 资源。还应考虑缓存/持久性机制 - 但显然不是这个问题的范围。
      【解决方案3】:

      将此设置为您的标题:

      header("Content-type:image/jpeg");
      

      代替:

      header("Content-type:image/png");
      

      【讨论】:

      • 这是错误的..我想知道你为什么要投票他使用header('Content-type: image/png');imagepng($im);为什么要把标题​​改成header("Content-type:image/jpeg");???
      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 2016-11-15
      • 2011-01-15
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多