简体   繁体   中英

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. But I don't know why my watermark function is not working for png files. however, it works for jpeg files perfectly. this is my PHP function.

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.

If you pass a jpeg through the function it will work. If you pass a png it will not.

I recommend using imagecreatefromstring() to create all your resources as it is not dependent on the file type. 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.

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.

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. However on PHP.net, someone did write a createimagefromBMP() that works really well. Also I think that on the latest version of PHP the GD library does now actually have a createimagefromBMP() function.

I also see that you are using unlink() to delete the image from your directory. This is not necessary for two reasons. The imageJpeg() will just overwrite the original. 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($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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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