用php给图片加水印

发布于 2025-01-08 11:06:43 字数 1754 浏览 4 评论 0原文

我的网站(全部用 html 和 php 编码)包含一个允许某些用户上传图片的功能。使用以下代码调整图片大小并加水印:

function watermarkpic($filename) {

ini_set('max_input_time', 300);

require 'config.php';  

$watermark = imagecreatefrompng('watermarknew.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

if(preg_match('/[.](jpg)$/', $filename)) {  
        $originalimage = imagecreatefromjpeg($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](gif)$/', $filename)) {  
        $originalimage = imagecreatefromgif($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](png)$/', $filename)) {  
        $originalimage = imagecreatefrompng($path_to_image_directory . $filename);  
    }  

$originalwidth = imagesx($originalimage);
$originalheight = imagesy($originalimage);

$maxsize = 800;
$imgratio = $originalwidth / $originalheight;

if($imgratio > 1) {
    $finalwidth = $maxsize;
    $finalheight = $maxsize / $imgratio;
}
else {
    $finalheight = $maxsize;
    $finalwidth = $maxsize * $imgratio;
}   

$finalimage = imagecreatetruecolor($finalwidth,$finalheight);

imagecopyresampled($finalimage, $originalimage, 0,0,0,0,$finalwidth,$finalheight,$originalwidth,$originalheight);

imagecopymerge($finalimage, $watermark, 0, 0, 0, 0, $watermarkwidth, $watermarkheight, 100);

//now move the file where it needs to go
if(!file_exists($path_to_medimage_directory)) {  
        if(!mkdir($path_to_medimage_directory)) {  
                die("There was a problem. Please try again!");  
        }  
     } 

 imagejpeg($finalimage, $path_to_medimage_directory . $filename);   
}

问题是水印具有透明背景,但在图像上显示为黑色背景。我看过有关阿尔法混合等的内容,但我真的不知道这是什么。我想了解我在做什么,并解决问题,使水印应有的透明度。真实的图片应该填满这个空间。

提前致谢。

My website (coded in html and php in entirety) includes a feature which allows certain users to upload pictures. The picture is resized and watermarked using this code:

function watermarkpic($filename) {

ini_set('max_input_time', 300);

require 'config.php';  

$watermark = imagecreatefrompng('watermarknew.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);

if(preg_match('/[.](jpg)$/', $filename)) {  
        $originalimage = imagecreatefromjpeg($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](gif)$/', $filename)) {  
        $originalimage = imagecreatefromgif($path_to_image_directory . $filename);  
    } else if (preg_match('/[.](png)$/', $filename)) {  
        $originalimage = imagecreatefrompng($path_to_image_directory . $filename);  
    }  

$originalwidth = imagesx($originalimage);
$originalheight = imagesy($originalimage);

$maxsize = 800;
$imgratio = $originalwidth / $originalheight;

if($imgratio > 1) {
    $finalwidth = $maxsize;
    $finalheight = $maxsize / $imgratio;
}
else {
    $finalheight = $maxsize;
    $finalwidth = $maxsize * $imgratio;
}   

$finalimage = imagecreatetruecolor($finalwidth,$finalheight);

imagecopyresampled($finalimage, $originalimage, 0,0,0,0,$finalwidth,$finalheight,$originalwidth,$originalheight);

imagecopymerge($finalimage, $watermark, 0, 0, 0, 0, $watermarkwidth, $watermarkheight, 100);

//now move the file where it needs to go
if(!file_exists($path_to_medimage_directory)) {  
        if(!mkdir($path_to_medimage_directory)) {  
                die("There was a problem. Please try again!");  
        }  
     } 

 imagejpeg($finalimage, $path_to_medimage_directory . $filename);   
}

The issue is that the watermark has a transparent background, but it shows up as having a black background on the image. I have seen stuff about alpha blending, etc. but I do not really know what this is. I want to understand what I am doing, as well as fix the problem so that the watermark is transparent as it should be. The real picture should fill the space.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2025-01-15 11:06:43

斯科特,这里可能会发生很多事情。

  1. 您需要确保使用未索引的 alpha 透明度保存 PNG。索引透明度基本上表示“这种颜色(可能是黑色)将在整个图像中显示为透明”。当由浏览器或图像编辑器读取时,它可能是透明的,但特别是如果您将其与 JPG 合并,则不会保留透明度。如果您想了解更多信息,请尝试 http ://www.idux.com/2011/02/27/what-are-index-and-alpha-transparency/

  2. 确保您获得两个图像的正确尺寸。请参阅 PHP 中的透明 PNG 优于 JPG 以确保您不会遇到同样的问题。

  3. 如果您仍然遇到问题,您可能需要检查此处:http://php.net/manual/en/image.examples.merged-watermark.php 因为它展示了如何更改图像的不透明度。它可能接近您想要完成的目标,或者可能会引发另一个想法。

Scott, there are a lot of things that can be going on here.

  1. You need to make sure you saved your PNG using alpha transparency not indexed. Indexed transparency basically says "This color(maybe black) will be displayed as transparent throughout the image." When read by a browser or image editor, it may be transparent, but especially if you're merging it with a JPG, the transparency won't be honored. If you want to understand more, try http://www.idux.com/2011/02/27/what-are-index-and-alpha-transparency/

  2. Make sure you are getting the correct dimensions for both images. See Transparent PNG over JPG in PHP to make sure you're not running into the same issue.

  3. If you are still running into issues, you may want to check here: http://php.net/manual/en/image.examples.merged-watermark.php as it shows how to change the opacity of an image. It may be close to what you are trying to accomplish or might jog another thought.

拥抱没勇气 2025-01-15 11:06:43

http://php.net/manual/en/function.imagecopymerge.php :

<?php
   $src = imagecreatefromstring(file_get_contents('watermark.png'));
   $dest = imagecreatefromstring(file_get_contents('Unmarked/indian_elephant_chasing_bird.jpg'));

   // Set the brush
   imagesetbrush($dest, $src);
   // Draw a couple of brushes, each overlaying each
   imageline($dest,
       imagesx($src)/2,
       imagesy($src)/2,
       imagesx($src)/2 ,  
       imagesy($src)/2,  
       IMG_COLOR_BRUSHED);

   header('Content-Type: image/png');
   imagepng($dest);
?>

http://php.net/manual/en/function.imagecopymerge.php:

<?php
   $src = imagecreatefromstring(file_get_contents('watermark.png'));
   $dest = imagecreatefromstring(file_get_contents('Unmarked/indian_elephant_chasing_bird.jpg'));

   // Set the brush
   imagesetbrush($dest, $src);
   // Draw a couple of brushes, each overlaying each
   imageline($dest,
       imagesx($src)/2,
       imagesy($src)/2,
       imagesx($src)/2 ,  
       imagesy($src)/2,  
       IMG_COLOR_BRUSHED);

   header('Content-Type: image/png');
   imagepng($dest);
?>
眉黛浅 2025-01-15 11:06:43

所以我想通了;我没有使用 imagecopymerge(),而是使用 imagecopy() 并且效果很好。

我在某处看到 imagecopymerge() 可能存在错误,导致 PHP5 中出现此问题。

So I figured it out; instead of using imagecopymerge() I used imagecopy() and it worked fine.

I saw somewhere that there may be a bug with imagecopymerge()that causes this in PHP5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文