【问题标题】:Add 'Watermark' to images with php [closed]使用 php 向图像添加“水印”[关闭]
【发布时间】:2011-01-15 03:54:43
【问题描述】:

我有一个用户可以上传图片的网站...

上传图片后,我需要在图片中添加我的徽标(水印)。

我该怎么做?

重要的是水印位于可见的角落,例如,我见过一些网站会即时生成水印,并将标记放置在主图像背景为“相同颜色”的任何位置"所以如果你明白我的意思,水印就会突出。

谁有这方面的好教程或文章? 或者知道我需要找到水印位置的php中的任何函数?

【问题讨论】:

标签: php image watermark


【解决方案1】:

使用此功能
水印图片类型必须为“png”

 function watermark_image($target, $wtrmrk_file, $newcopy) {
    $watermark = imagecreatefrompng($wtrmrk_file);
    imagealphablending($watermark, false);
    imagesavealpha($watermark, true);
    $img = imagecreatefromjpeg($target);
    $img_w = imagesx($img);
    $img_h = imagesy($img);
    $wtrmrk_w = imagesx($watermark);
    $wtrmrk_h = imagesy($watermark);
    $dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
    $dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
    imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
    imagejpeg($img, $newcopy, 100);
    imagedestroy($img);
    imagedestroy($watermark);
}

watermark_image('image_name.jpg','watermark.png', 'new_image_name.jpg');

【讨论】:

    【解决方案2】:
    // Load the stamp and the photo to apply the watermark to
    
    $stamp = imagecreatefrompng('stamp.png');
    $im = imagecreatefromjpeg('photo.jpg');
    $save_watermark_photo_address = 'watermark_photo.jpg';
    
    // Set the margins for the stamp and get the height/width of the stamp 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');
    
    imagejpeg($im, $save_watermark_photo_address, 80); 
    imagedestroy($im);
    

    【讨论】:

      【解决方案3】:

      水印图片的好例子,位于中心

      <?php
      // Load the stamp and the photo to apply the watermark to
      $stamp = imagecreatefrompng('stampimg.png');
      $im = imagecreatefrompng('mainimage.png');
      
      // Set the margins for the stamp and get the height/width of the stamp image
      $marge_right = 10;
      $marge_bottom = 10;
      $sx = imagesx($stamp);
      $sy = imagesy($stamp);
      
      $imgx = imagesx($im);
      $imgy = imagesy($im);
      $centerX=round($imgx/2);
      $centerY=round($imgy/2);
      
      // Copy the stamp image onto our photo using the margin offsets and the photo 
      // width to calculate positioning of the stamp. 
      imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
      
      // Output and free memory
      header('Content-type: image/png');
      imagepng($im);
      imagedestroy($im);
      ?>
      

      【讨论】:

        【解决方案4】:

        我找到了一个更好的解决方案,通过 .htaccess 动态添加水印,您可以在此处找到教程:

        Add watermark to images through htaccess

        上传自定义 .htaccess 文件、watermark.php scrypt 和您的 watermark.png 图像后,该文件夹及其子文件夹中的所有图像都会显示水印,但是您仍将原始文件保留在服务器中.

        希望对别人的帮助和对我的帮助一样。

        【讨论】:

        • 很好的解决方案,因为它很简单,但它需要更多的服务器资源,因为它会一遍又一遍地(在每个显示器上)处理图像,而不是一劳永逸。它可以在流量大的网站上产生很大的影响。
        • 这不是一个好的解决方案,每次用户重新加载页面时,都会加载这个脚本......更多的服务器资源......
        • 这是一种临时解决方案。好处是您可以在不接触模块代码的情况下实现它。但是,在流量大的网站上将需要更多的努力
        【解决方案5】:

        PHP 手册中的good example

        // Load the stamp and the photo to apply the watermark to
        $stamp = imagecreatefrompng('stamp.png');
        $im = imagecreatefromjpeg('photo.jpeg');
        
        // Set the margins for the stamp and get the height/width of the stamp 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);
        

        【讨论】:

        【解决方案6】:

        ImageMagick 非常适合。我以前做过。不过,整个业务有点痛苦。特别是如果您想要花哨的混合模式等。

        【讨论】:

          【解决方案7】:

          这可以使用图像处理库来完成,例如GDImageMagick。这是一个教程,解释了使用 GD 的方法:

          http://articles.sitepoint.com/article/watermark-images-php

          【讨论】:

          • 您好,我也有问题,它给了我资源 ID,我无法上传它,这意味着 move_uploaded_file 不起作用,请帮我解决它。
          猜你喜欢
          • 2021-12-31
          • 1970-01-01
          • 2016-11-15
          • 1970-01-01
          • 1970-01-01
          • 2011-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多