I want to add watermark to my users images posts automatically.
I've done below code already, but the images become Blank with watermark.
Blank Image: https://drk3g4xn3fsso.cloudfront.net/1503319365_b6ffc731d32b4abc1503319361869.jpg
<?php
function resizeimage($img,$size){
$width=$size[0];
$height=$size[1];
$newwidth = 640;
$newheight = $height*($newwidth/$width);
$tci = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tci, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $tci;
}
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function compress($source, $destination, $quality,$add_watermark=FALSE) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
$image=resizeimage($image,$info);
if ($add_watermark){
$watermark=imagecreatefrompng("watermark.png");
$infoWatermark=getimagesize("watermark.png");
$imageWidth=$info[0];
$imageHeight=$info[1];
$watermarkWidth=$infoWatermark[0];
$watermarkHeight=$infoWatermark[1];
//imagecopyresampled($image,$watermark,20,20,0,0,$watermarkWidth,$watermarkHeight,$watermarkWidth,$watermarkHeight);
imagecopy($image,$watermark,20,20,0,0,$watermarkWidth,$watermarkHeight);
}
imagejpeg($image, $destination, $quality);
return $destination;
}
I just wonder what I've done wrong to make it not work with the uploaded image?
thank you.
Hello here is the demo to generate image with Watermark.
Folder Structure :
Demo
Note : watermark.jpg is the image which you use as watermark on sample.jpg image.
Here is the index.php code.