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.