【问题标题】:add a watermark on images在图像上添加水印
【发布时间】:2013-09-02 14:50:58
【问题描述】:

我尝试在图像 (jpg/gif/png/jpeg) 上添加水印、透明背景的 png...我有 2 个文件...第一个是 watermark.php:

<?php
    require 'func/images.func.php';
    if (isset($_FILES['image'])){
         $file_name =       $_FILES['image']['name']; 
         $file_tmp =        $_FILES['image']['tmp_name'];   
         $name = explode('.', $file_name);
         if (allowed_image($file_name) === true){
            $file_name = $img_name .'.png';
            watermark_image($file_tmp, 'images-watermark/uploads/' . $file_name);       
         } else{
          echo '<p>no image, or image type not accepted.</p>';
         }
     }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
Choose an image:
<input type="file" name="image" />
<input type="submit" value="watermark"/>
</form>
</body>
</html>

...第二个是images.func.php:

<?php
    function allowed_image($file_name){
    $allow_set = array('jpg', 'jpeg', 'gif', 'png');
    $file_ext =   (explode('.', $file_name));
    $end = end($file_ext);  
    return (in_array($end , $allow_set) === true) ? true : false;   
}

function watermark_image($file, $destination){
    $watermark = imagecreatefrompng('images-watermark/watermarkf.png'); 
    $source = getimagesize($file);  
    $source_mime = $source['mime']; 
    if ($source_mime === 'image/png'){
        $image = imagecreatefrompng($file);     
    }else if($source_mime === 'image/jpeg'){
        $image = imagecreatefromjpeg($file);    
    }else if($source_mime === 'image/gif'){
        $image = imagecreatefromgif($file);         
    }   
    imagecopy($image, $watermark, 70, 160, 0, 0, imagesx($watermark),imagesy($watermark));  
    imagepng($image, $destination); 
    imagedestroy($image); 
        imagedestroy($watermark);

}
?>

...我的代码适用于 jpg/png/jpeg 格式(图像具有透明背景的水印)...在 gif 图像中,水印没有透明背景...任何提示? 在此先感谢

【问题讨论】:

  • 哇哦.. 你这里似乎缺少很多的缩进。请清理它,以便我们有合理的机会查看您的问题。
  • 另外,OP 使用 imagick 吗?我觉得可能GD不支持
  • 对不起...但是什么是 OP?谢谢
  • 你为什么使用 XHTML 1.0 而不是 html?

标签: php image watermark


【解决方案1】:

GIF 图像需要解压缩为全 RGB,然后混合,然后重新索引为 256 色。在图像顶部添加透明水印是使数据无法以 GIF 格式捕获的好方法,因为您引入了 256 种新的叠加层。

【讨论】:

  • 我可以使用 Photoshop 将 gif 图像解压缩为完整的 RGB...但是...“然后混合”和“然后重新索引为 256 色”是什么意思?真的非常感谢你
  • 您的叠加图像混合到原始图像的像素中。原来的GIF有256色,然后你混入你的透明水印,现在图像已经超过256色,调色板需要重新制作,过程中丢失了信息。真正的主要教训是不要使用 GIF。这是计算机屏幕只有几种颜色的时代的一种图像格式。是的,它们很小,插值的 PNG 文件也是如此,通过 pngcrush 运行
【解决方案2】:

如果您不出于实验目的这样做,我建议您为此使用库。 我个人最喜欢的是PHP Image Workshop 或者你也可以使用Imagine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多