Hate My Blog

php watermark and resize image

So I have this friend that likes to take photos, well yea hes a photographer, and apparently one of the lamest parts of his job is the prep work after the show. Basically he spends a lot of time resizing and adding his logo to some 100s of photos each next day after parties. And as I’m currently building an online gallery for him, I wanted to make his life simpler in this aspect.

This is code ripped out of the upload script, then i added some comments to explain whats going on, and then starting writing this post. I haven’t tested it, but it should work.

UPDATE: well wordpress butchers code, i put it on [pastebin.ca] http://pastebin.ca/1557910


$srcimg = "thebigphoto.jpg";    //the source file
$tmimg = "sm_".$srcimg;            //were to place the thumb
$wmimg = "watermark.pbr";        //the watermark files
$target  = 150;                    //thumbnail target size
$waterlevel = 40;                //the level of opacity

//load the jpeg and get its sizes
$image = imagecreatefromjpeg($srcimg);
$size = getimagesize($srcimg);

//this if just figures out what side is dominant
if ($size[0] > $size[1]) {
$percentage = ($target / $size[0]);
} else {
$percentage = ($target / $size[1]);
}
//calc the thumb sizes
$newwidth = round($size[0] * $percentage);
$newheight = round($size[1] * $percentage);

//make a new img | copy the smaller | save as jpeg | clean up
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1]);
imagejpeg($thumb,$tmimg);
imagedestroy($thumb);

//make watermage img | and get its sizes;
$watermark = imagecreatefrompng($wmimg);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

this will place it Bottom-Left, 5px from the border
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;

//copy it over | save it over the original | and clean up
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $waterlevel);
imagejpeg($image,$srcimg );
imagedestroy($watermark);
imagedestroy($image);

September 8, 2009 - Posted by | Code, PHP | , , , , , , ,

1 Comment »

  1. This site rocks!

    Comment by Bill Bartmann_- | September 10, 2009 | Reply


Leave a reply to Bill Bartmann_- Cancel reply