So, I'm trying to add a watermark (png image) on jpg/png image. The thing is..
The code works if I upload a jpg, but when I upload a png the result is not the same as I want it to be.
If I upload a png the watermark will go in front of the image I uploaded, but then the background isn't transparant anymore (which is what I want).
if(isset($_GET['img'])) {
$image = $_GET['img'];
$stamp = imagecreatefrompng(DIR_PUBLIC . 'watermerk.png');
//$im = imagecreatefromjpeg(DIR_IMAGE . $image);
$fullPath = DIR_IMAGE . $image;
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
// Determine Content Type
switch ($ext) {
case "png":
$im = imagecreatefrompng($fullPath);
$ctype="image/png";
break;
case "jpeg":
case "jpg":
$ctype="image/jpg";
$im = imagecreatefromjpeg($fullPath);
break;
default:
$ctype="application/force-download";
}
// Get dimensions
$imageWidth = imagesx($im);
$imageHeight = imagesy($im);
$logoWidth = imagesx($stamp);
$logoHeight = imagesy($stamp);
// Paste the logo
imagecopy(
// destination
$im,
// source
$stamp,
// destination x and y
($imageWidth - $logoWidth) / 2, ($imageHeight - $logoHeight) / 2,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight
);
// Output and free memory
header("Content-Type: $ctype");
imagepng($im);
imagedestroy($im);
}
What I want: I want to be able to upload a png image and still have the watermark image over the image.