简体   繁体   English

我的php水印功能不适用于png图像

[英]My php Watermark function is not working for png image

I am using a PHP function to add my logo as the watermark on images uploaded on my website. 我正在使用PHP函数将徽标添加为在我网站上上传的图像上的水印。 But I don't know why my watermark function is not working for png files. 但是我不知道为什么我的水印功能不能用于png文件。 however, it works for jpeg files perfectly. 但是,它完美地适用于jpeg文件。 this is my PHP function. 这是我的PHP函数。

function watermark($img) {
   global $wm_file, $wm_right, $wm_bottom;

   // image values pulled from config.inc.php
   $logo = './images/' . $wm_file; // path to the watermark.png
   $sp = $wm_right; // spacing from right side
   $sq = $wm_bottom; // spacing from bottom

   $size = getImageSize($img);
   $sizel = getImageSize($logo);
   $imgA = imageCreateFromJpeg($img);
   imageAlphaBlending($imgA, TRUE);
   if($sizel[0] > $size[0] || $sizel[1] > $size[1]) 
   {
      // logo size > img size
      $sizelo[0] = $sizel[0];
      $sizelo[1] = $sizel[1];
      $sizel[0] = ($sizel[0]/2);
      $sizel[1] = ($sizel[1]/2);
   } 
   else 
   {
      $sizelo[0] = $sizel[0];
      $sizelo[1] = $sizel[1];
   }
   $imgBa = imageCreateFromPng($logo);
   $imgB = imageCreateTrueColor($sizel[0], $sizel[1]);
   imageAlphaBlending($imgB, TRUE);
   imageCopyResampled($imgB, $imgBa, 0, 0, 0, 0, $sizel[0], $sizel[1], $sizelo[0], $sizelo[1]);
   imageColorTransparent($imgB, ImageColorAllocate($imgB, 0, 0, 0));
   $perc = 100; 
   imageCopymerge($imgA, $imgB, ($size[0]-$sizel[0]-$sp), ($size[1]-$sizel[1]-$sq), 0, 0, $sizel[0], $sizel[1], $perc);
   unlink($img);
   if(imageJpeg($imgA, $img, 100)) 
   {
      imageDestroy($imgB);
      imageDestroy($imgA);
      return true;
   }
   chmod($img, 0777);
}

The problem I see is that you are using imageCreateFromJpeg() as the way to generate the resource for your $img that you are passing to the function. 我看到的问题是,您正在使用imageCreateFromJpeg()作为为传递给函数的$img生成资源的方式。

If you pass a jpeg through the function it will work. 如果通过该函数传递jpeg,它将起作用。 If you pass a png it will not. 如果您传递png,则不会。

I recommend using imagecreatefromstring() to create all your resources as it is not dependent on the file type. 我建议使用imagecreatefromstring()创建所有资源,因为它不依赖于文件类型。 Like so: 像这样:

$source = imagecreatefromstring(file_get_contents($filePath));

Another benefit of this is that it will return false if the function fails to create a resource from the file path that you supplied meaning that the file is not an image file. 这样做的另一个好处是,如果函数无法从您提供的文件路径创建资源,则返回false,这意味着该文件不是图像文件。

Now that you have a resource to use for the rest of your code, imageJpeg() will save the resource as a jpeg back to the file path. 现在,您已经有资源可用于其余的代码, imageJpeg()会将资源以jpeg格式保存回文件路径。

Hope that helps. 希望能有所帮助。

One other side note. 另一注。 If you intend on using bmp images, the GD library does not have a built in function for bmps. 如果打算使用bmp图像,则GD库没有bmp的内置函数。 However on PHP.net, someone did write a createimagefromBMP() that works really well. 但是在PHP.net上,有人确实编写了一个效果很好的createimagefromBMP() Also I think that on the latest version of PHP the GD library does now actually have a createimagefromBMP() function. 我还认为,在最新版本的PHP上,GD库实际上确实具有createimagefromBMP()函数。

I also see that you are using unlink() to delete the image from your directory. 我还看到您正在使用unlink()从目录中删除图像。 This is not necessary for two reasons. 出于两个原因,这是不必要的。 The imageJpeg() will just overwrite the original. imageJpeg()只会覆盖原始图像。 Also, if for some reason your script fails it may delete the image prematurely and you will loose the image without the new one being written. 另外,如果由于某种原因您的脚本失败,它可能会过早地删除该图像,并且您将丢失该图像而无需编写新的图像。

Please be careful when using chmod() , always make sure that you set permissions back to the original permissions when you are done. 使用chmod()时请小心,请务必在完成chmod()权限设置回原始权限。

chmod($img, 777);  //Give broad permissions.

//Do something.

chmod($img, 600(or whatever they were)); //Reset permission back to where they were before you changed them.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM