While OP's question wasnt crystal clear on the exact method of watermarking, im showing here text and image watermarking capabilities using the GD library.
Adding text as watermark (using TTF font): Gist
function add_text_watermark($kep,$Text,$WatermarkNeeded = 1) {
list($img_type, $Image) = getImage($kep);
$sx = imagesx($Image) ;
$sy = imagesy($Image) ;
if ($WatermarkNeeded)
{
/* Set the font */
$Font="_arial.ttf";
$FontColor = ImageColorAllocate ($Image,204,204,204) ;
$FontShadow = ImageColorAllocate ($Image,100,100,100) ;
$Rotation = 0 ;
/* Make a copy image */
$OriginalImage = ImageCreateTrueColor($sx,$sy) ;
ImageCopy ($OriginalImage,$Image,0,0,0,0,$sx,$sy) ;
/* Iterate to get the size up */
$FontSize=1 ;
do
{
$FontSize *= 1.1 ;
$Box = @ImageTTFBBox($FontSize,0,$Font,$Text);
$TextWidth = abs($Box[4] - $Box[0]) ;
$TextHeight = abs($Box[5] - $Box[1]) ;
}
while ($TextWidth < $sx*0.9 && $FontSize < 30) ;
/* Awkward maths to get the origin of the text in the right place */
$x = $sx/2 - cos(deg2rad($Rotation))*$TextWidth/2 ;
$y = $sy/2 + sin(deg2rad($Rotation))*$TextWidth/2 + cos(deg2rad($Rotation))*$TextHeight/2 ;
/* Make shadow text first followed by solid text */
ImageTTFText ($Image,$FontSize,$Rotation,$x+1,$y+1,$FontShadow,$Font,$Text);
ImageTTFText ($Image,$FontSize,$Rotation,$x,$y,$FontColor,$Font,$Text);
/* merge original image into version with text to show image through text */
ImageCopyMerge ($Image,$OriginalImage,0,0,0,0,$sx,$sy,50) ;
imagejpeg($Image, $kep, 100);
}
}
function getImage($res) {
$img = "";
$type = "";
if (intval(@imagesx($res)) > 0) {
$img = $res;
} else {
$imginfo = getimagesize($res);
switch($imginfo[2]) { // Determine type
case 1:
$type = "GIF";
if (function_exists("imagecreatefromgif")) {
$img = imagecreatefromgif($res);
} else {
die("Unsupported image type: $type");
}
break;
case 2:
$type = "JPG";
if (function_exists("imagecreatefromjpeg")) {
$img = imagecreatefromjpeg($res);
} else {
die("Unsupported image type: $type");
}
break;
case 3:
$type = "PNG";
if (function_exists("imagecreatefrompng")) {
$img = imagecreatefrompng($res);
} else {
die("Unsupported image type: $type");
}
break;
}
}
return array($type, $img);
}
EDIT: make sure the ttf font file you are trying to add as watermark text are in sight of the path!
Adding Image as watermark: Gist
function generate_watermarked_image($originalFileContents, $originalWidth, $originalHeight, $paddingFromBottomRight = 0, $watermarkFileLocation = 'logo.png') {
$watermarkImage = imagecreatefrompng($watermarkFileLocation);
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
$originalImage = imagecreatefromstring($originalFileContents);
$destX = $originalWidth - $watermarkWidth - $paddingFromBottomRight;
$destY = $originalHeight - $watermarkHeight - $paddingFromBottomRight;
// creating a cut resource
$cut = imagecreatetruecolor($watermarkWidth, $watermarkHeight);
// copying that section of the background to the cut
imagecopy($cut, $originalImage, 0, 0, $destX, $destY, $watermarkWidth, $watermarkHeight);
// placing the watermark now
imagecopy($cut, $watermarkImage, 0, 0, 0, 0, $watermarkWidth, $watermarkHeight);
// merging both of the images
imagecopymerge($originalImage, $cut, $destX, $destY, 0, 0, $watermarkWidth, $watermarkHeight, 100);
return $originalImage;
}
You can call it as:
$Image = "$desired_dir/".$file_name;
imagejpeg(generate_watermarked_image(file_get_contents($Image), imagesx($Image), imagesy($Image), 10), $Image."-watermarked.jpg", 100);
EDIT: make sure the logo.png or whatever file you are trying to add as watermark are in sight of the path!