How To Add Watermark To An Image with PHP and Imagepng
|
Hey!
Now I'm going to teach you how to add a watermark to an image. It is really easy. It does like this: First lets define our image where we want our watermark: $image = 'path/to/a/file.jpg'; Now we need to check what kind of image is it and set it up for processing: if(eregi('.jpg', $image)) { $i = imagecreatefromjpeg($image); }elseif(eregi('.gif', $image)) { $i = imagecreatefromgif($image); }elseif(eregi('.png', $image)) { $i = imagecreatefrompng($image); } Now let's take our watermark: Next is taking width and height of our image and our watermark: And now is the time for the most important step, putting those images together: imagecopymerge($i, $watermark, (($width - $watermark_width)), (($height - $watermark_height)), 0, 0, $watermark_width, $watermark_height, 50); If it's done, then we want to display our watermarked image: Now when everything is done, let's destroy our images: And we are done! ![]() Here's the full code: <?php /** * @author Jaan * @link http://webhostingtalkforums.com * @copyright 2011 */ $image = 'battlefield.jpg'; if(eregi('.jpg', $image)) { $i = imagecreatefromjpeg($image); }elseif(eregi('.gif', $image)) { $i = imagecreatefromgif($image); }elseif(eregi('.png', $image)) { $i = imagecreatefrompng($image); } $watermark = imagecreatefromgif('watermark.gif'); $width = imagesx($i); $height = imagesy($i); $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); imagecopymerge($i, $watermark, (($width - $watermark_width)), (($height - $watermark_height)), 0, 0, $watermark_width, $watermark_height, 50); if(eregi('.jpg', $image)) { header('Content-Type: image/jpeg'); imagejpeg($i); }elseif(eregi('.gif', $image)) { header('Content-Type: image/gif'); imagegif($i); }elseif(eregi('.png', $image)) { header('Content-Type: image/png'); imagepng($i); } imagedestroy($i); imagedestroy($watermark); ?> I hope it helped you. If you have questions feel free to post a comment below. |
Replies:
|
|
||
|
||
|
||
|























