spikeyjames
asked on
Adding a watermark with PHP
Below is the code I'm trying to use to firstly resize my image and then add a watermark to it..but i just can't get it to work. Any help would be greatly appreciated.
<?
$cfg_fullsizepics_path = "../images/magazine/$editi on/article s/$id/main ";
$cfg_thumb_path = "../images/magazine/$editi on/article s/$id/main ";
$filepath = "../images/magazine/$editi on/article s/$id/main ";
$dir = dir($filepath);
while($entry=$dir->read()) {
if($entry == "." || $entry == "..") {
continue;
}
$fp = @fopen("$filepath/$entry", "r");
$new_w = 800;
$image_stats = GetImageSize($cfg_fullsize pics_path. "/".$entry );
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
$filename = $entry;
$lst=GetImageSize($filenam e);
$image_width=$lst[0];
$image_height=$lst[1];
$image_format=$lst[2];
Define('WATERMARK_IMAGE', './wt.png'); // path to watermark image
Define('WATERMARK_PERCENT' , '70'); // Intensity of the transition (in percent)
Define('WATERMARK_ALIGN_H' , 'right'); // left / right / center
Define('WATERMARK_ALIGN_V' , 'bottom'); // top / bottom / center
Define('WATERMARK_MARGIN', '10'); // margin
if ($img_type=="2") {
$lst2=getimagesize(WATERMA RK_IMAGE);
$image2_width=$lst2[0];
$image2_height=$lst2[1];
$image2_format=$lst2[2];
$wt_image=imagecreatefromp ng(WATERMA RK_IMAGE);
if ($wt_image) {
$wt_y=WATERMARK_MARGIN;
if (WATERMARK_ALIGN_V=='top') {
$wt_y=WATERMARK_MARGIN;
} elseif (WATERMARK_ALIGN_V=='botto m') {
$wt_y=$image_height-$image 2_height-W ATERMARK_M ARGIN;
} elseif (WATERMARK_ALIGN_V=='cente r') {
$wt_y=(int)($image_height/ 2-$image2_ height/2);
}
$wt_x=WATERMARK_MARGIN;
if (WATERMARK_ALIGN_H=='left' ) {
$wt_x=WATERMARK_MARGIN;
} elseif (WATERMARK_ALIGN_H=='right ') {
$wt_x=$image_width-$image2 _width-WAT ERMARK_MAR GIN;
} elseif (WATERMARK_ALIGN_H=='cente r') {
$wt_x=(int)($image_width/2 -$image2_w idth/2);
}
$src_img = imagecreatefromjpeg($cfg_f ullsizepic s_path."/" .$entry);
$dst_img = imagecreatetruecolor($new_ w,$new_h);
$tmp = imagecopyresampled($dst_im g,$src_img ,0,0,0,0,$ new_w,$new _h,imagesx ($src_img) ,imagesy($ src_img));
imagecopymerge($tmp, $wt_image, $wt_x, $wt_y, 0, 0, $image2_width, $image2_height, WATERMARK_PERCENT);
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry ");
}
<?
$cfg_fullsizepics_path = "../images/magazine/$editi
$cfg_thumb_path = "../images/magazine/$editi
$filepath = "../images/magazine/$editi
$dir = dir($filepath);
while($entry=$dir->read())
if($entry == "." || $entry == "..") {
continue;
}
$fp = @fopen("$filepath/$entry",
$new_w = 800;
$image_stats = GetImageSize($cfg_fullsize
$imagewidth = $image_stats[0];
$imageheight = $image_stats[1];
$img_type = $image_stats[2];
$ratio = ($imagewidth / $new_w);
$new_h = round($imageheight / $ratio);
$filename = $entry;
$lst=GetImageSize($filenam
$image_width=$lst[0];
$image_height=$lst[1];
$image_format=$lst[2];
Define('WATERMARK_IMAGE', './wt.png'); // path to watermark image
Define('WATERMARK_PERCENT'
Define('WATERMARK_ALIGN_H'
Define('WATERMARK_ALIGN_V'
Define('WATERMARK_MARGIN',
if ($img_type=="2") {
$lst2=getimagesize(WATERMA
$image2_width=$lst2[0];
$image2_height=$lst2[1];
$image2_format=$lst2[2];
$wt_image=imagecreatefromp
if ($wt_image) {
$wt_y=WATERMARK_MARGIN;
if (WATERMARK_ALIGN_V=='top')
$wt_y=WATERMARK_MARGIN;
} elseif (WATERMARK_ALIGN_V=='botto
$wt_y=$image_height-$image
} elseif (WATERMARK_ALIGN_V=='cente
$wt_y=(int)($image_height/
}
$wt_x=WATERMARK_MARGIN;
if (WATERMARK_ALIGN_H=='left'
$wt_x=WATERMARK_MARGIN;
} elseif (WATERMARK_ALIGN_H=='right
$wt_x=$image_width-$image2
} elseif (WATERMARK_ALIGN_H=='cente
$wt_x=(int)($image_width/2
}
$src_img = imagecreatefromjpeg($cfg_f
$dst_img = imagecreatetruecolor($new_
$tmp = imagecopyresampled($dst_im
imagecopymerge($tmp, $wt_image, $wt_x, $wt_y, 0, 0, $image2_width, $image2_height, WATERMARK_PERCENT);
imagejpeg($dst_img, "$cfg_thumb_path"."/$entry
}
ignore the comment saying:
//Resize the image to the most common 2 sizes, plus a 'full' size for later copying and using
it should say:
//Resize the image to a standard 800x600 size
//Resize the image to the most common 2 sizes, plus a 'full' size for later copying and using
it should say:
//Resize the image to a standard 800x600 size
ASKER
Is there any chance you could just tell me what is wrong with my code? because I'm not that good with PHP, and I can't see from that what I've done wrong. Also, what is imagemagick? That is not in my script at all...is it needed?
Thanks
Thanks
here is an example much closer to yours (found in a file i will call 'image.php'). you call it from another page like this: <img src="image.php">
Here's the code:
//Get the original image size
list($width_orig, $height_orig) = getimagesize($imagePath);
//Set the desired height
$width = $globalInfo->GetOneSizeIma geWidth($p ortrait);
$height = $globalInfo->GetOneSizeIma geHeight($ portrait);
//using the two sets of sizes from above, get the new photo size
if ($width && ($width_orig < $height_orig))
$width = ($height / $height_orig) * $width_orig;
else
$height = ($width / $width_orig) * $height_orig;
// Resample the image to the size wanted
$image_p = imagecreatetruecolor($widt h, $height);
$image = imagecreatefromjpeg($image Path);
imagecopyresampled($image_ p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
//Get the watermark file in a usable format
$watermark = imagecreatefromgif('images /copyright _portrait. gif');
//Get the size of the watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
//get the x and y locations for where you want to put the watermark (change this to whatever you'd like)
$dest_x = ($width - $watermark_width);
$dest_y = ($height - $watermark_height);
imagecopymerge($image_p, $watermark, 0, $dest_y, 0, 0, $watermark_width, $watermark_height, 25);
//display the image
imagejpeg($image_p);
//destroy the two temporary images
imagedestroy($image_p);
imagedestroy($watermark);
Here's the code:
//Get the original image size
list($width_orig, $height_orig) = getimagesize($imagePath);
//Set the desired height
$width = $globalInfo->GetOneSizeIma
$height = $globalInfo->GetOneSizeIma
//using the two sets of sizes from above, get the new photo size
if ($width && ($width_orig < $height_orig))
$width = ($height / $height_orig) * $width_orig;
else
$height = ($width / $width_orig) * $height_orig;
// Resample the image to the size wanted
$image_p = imagecreatetruecolor($widt
$image = imagecreatefromjpeg($image
imagecopyresampled($image_
//Get the watermark file in a usable format
$watermark = imagecreatefromgif('images
//Get the size of the watermark image
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
//get the x and y locations for where you want to put the watermark (change this to whatever you'd like)
$dest_x = ($width - $watermark_width);
$dest_y = ($height - $watermark_height);
imagecopymerge($image_p, $watermark, 0, $dest_y, 0, 0, $watermark_width, $watermark_height, 25);
//display the image
imagejpeg($image_p);
//destroy the two temporary images
imagedestroy($image_p);
imagedestroy($watermark);
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Caliguian
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
Huji
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Caliguian
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
Huji
EE Cleanup Volunteer
ASKER
No, I wanted to resize the image when it was uploaded to the server, not when it was downloaded from the server - that response didn't answer my question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with Caliguian, if this had of been followed up the question would have been answered.
My 2 cents :)
My 2 cents :)
ASKER
You're right. I moved on to more important matters and left this aside. Happy to offer what little points I have left, as you did eventially answer the question :P
JP
JP
/* ADD WATERMARK */
//The tmp file -- file that will have the composite of both images
$tmpFile = $tempSavePath;
//The image to use as the watermark
$watermarkFile = "/watermark/watermark.jpg"
//add the watermark to the image if there has been a watermark selected
if(file_exists($watermarkF
{
$waterDimen = getImageSize($watermarkFil
//The image that we want to add the watermark to
$destinationFile = $tempSavePath;
$rawDimen = getImageSize($destinationF
//Place the watermark 5 pixels away from the right hand side of image
$placement_x = $rawDimen[0]-$waterDimen[0
//Place the watermark 5 pixels away from the bottom of image
$placement_y = $rawDimen[1]-$waterDimen[1
//The path to the imagemagick composite program.
$composite = "{$imagemagickdir}/composi
//Create and execute the command to create the watermark file (saves to the $tmpFile)
$cmd = "$composite -watermark 13.0 -geometry +$placement_x+$placement_y
exec($cmd);
}
//Resize the image to the most common 2 sizes, plus a 'full' size for later copying and using
$command = "{$imagemagickdir}/convert
exec($command);
@unlink($tempSavePath);