How to Watermark your Images - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: PHP The Hypertext Preprocessor (https://www.supportforums.net/forumdisplay.php?fid=21) +---- Thread: How to Watermark your Images (/showthread.php?tid=834) Pages:
1
2
|
How to Watermark your Images - Gaijin - 10-08-2009 Welcome and Enjoy! Intro: In this tutorial, I will show you how to use PHP and GD to watermark your images. Preview: Now the watermark text doesn't look nice as you can see but that is my mistake in creating process. Image copyright http://artgerm.deviantart.com Wrote: Requirements:
Functions:
Let's Start: We start with the following code: PHP Code: if(!isset($_GET['img'])) { Frist we check if the url is like "watermark.php?img=path_to_image", if it's not we let the script end running with the die() function. If all goes well we define with header() that the page is an image. In the next step we define $path, wich holds the full path to the image. "watermark.php?img=path_to_image" PHP Code: $path = $_GET['img']; Now we use imagecreatefrompng() to create new image resource for use within our scritp. And then imagesx to get the width of the watermark image, and imagesy to get the height of the watermark image. NOTE: Since we are using imagecreatefrompng(), the watermark image needs to be an PNG image. PHP Code: $watermark = imagecreatefrompng('watermark.png'); So far so good, we use imagecreatetruecolor() to create black image with the size of our watermark image. PHP Code: $image = imagecreatetruecolor($width, $height); In the next step we get the info of the original image, and create right type of image. PHP Code: $img = getimagesize($path); getimagesize() will get us the size and file type of the original image. We use the "mime" type returned by "getimagesize()" to create right type of resource. Now let us define the position of our watermark. This will place the watermark at the bottom right corner. PHP Code: $pos_x = $img[0] - $width; All we need to do now is to merge the two created resources with the function imagecopymerge(). PHP Code: imagecopymerge($image, $watermark, $pos_x, $pos_y, 0, 0, $width, $height, 50); And finally we just need to output the image with the imagejpeg() function. PHP Code: imagejpeg($image); With imagedestroy() we clear any memory association. To use this script you just need to load it as following: Code: <img src="watermark.php?img=tut.jpg"> Source (Click to View) Thank you for reading! RE: How to Watermark your Images - Headshot - 10-08-2009 Great, man, you are god. RE: How to Watermark your Images - Gaijin - 10-08-2009 (10-08-2009, 09:00 PM)Headshot Wrote: Great, man, you are god. Thank you man. Just trying to help. RE: How to Watermark your Images - Grizzly - 10-10-2009 Nice tutorial. Well written and easy to follow. RE: How to Watermark your Images - Gaijin - 10-10-2009 (10-10-2009, 03:20 PM)Grizzly Wrote: Nice tutorial. Well written and easy to follow. Thanks man! RE: How to Watermark your Images - Bartdevil - 10-11-2009 Nice tutorial, written in a good format, thanks =) RE: How to Watermark your Images - Gaijin - 10-11-2009 (10-11-2009, 02:53 AM)Bartdevil Wrote: Nice tutorial, written in a good format, thanks =) Thank you, I'm glad you like it. RE: How to Watermark your Images - HuNt3R - 10-11-2009 Great TUT!!!!!!! RE: How to Watermark your Images - Gaijin - 10-13-2009 (10-11-2009, 09:24 AM)HuNt3R Wrote: Great TUT!!!!!!! Thank you very much! edit: crap 110mb closed my account and I muss find another host for the files. RE: How to Watermark your Images - Akshay* - 10-13-2009 Thanks ninja geek your posts are always amazing |