Watermarking an image with php

My site (encoded in html and php in full) includes a feature that allows certain users to upload photos. Image 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 problem is that the watermark has a transparent background, but it appears as having a black background in the image. I saw stuff about alpha blending, etc., but I really don't know what that is. I want to understand what I'm doing and also fix the problem so that the watermark is transparent, as it should be. The real picture should fill the space.

Thanks in advance.

+4
source share
3 answers

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

I saw somewhere that there might be an error with imagecopymerge() that causes this in PHP5.

0
source
Scott, there are many things that can happen here.
  • You need to make sure that you saved your PNG using alpha transparency that is not indexed. Indexed transparency basically says: "This color (possibly black) will be displayed as transparent throughout the image." When read by a browser or graphical editor, it may be transparent, but especially if you combine it with JPG, transparency will not work. If you want to know more, try http://www.idux.com/2011/02/27/what-are-index-and-alpha-transparency/

  • Make sure you get the right sizes for both images. See Transparent PNG over JPG in PHP to make sure you are not using the same problem.

  • If you still run into problems, you can check here: http://php.net/manual/en/image.examples.merged-watermark.php as it shows how to change the opacity of the image. It may be close to what you are trying to accomplish, or it may shuffle another thought.

+2
source

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); ?> 
0
source

Source: https://habr.com/ru/post/1398251/


All Articles