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.
source share