How to create watermark using gd library in php
Last updated:28th May, 2020 Show
How to watermark images using phpPHP GD library has very cool features to manipulate images. Images can be resized, cropped, rotated or watermarked. In several web applications, you need to add dynamic images or add a watermark to images. In a previous tutorial we explored how to manipulate images using gd library and watermark images with codeigniter. This tutorial explores how to watermark images using php. In this tutorial following tasks are performed. 1. Write dynamic text to image using php 2. Write dynamic text to image using php with true type fonts 3. Watermark images using php with another image as stamp 4. Watermark images using php with text as stamp Write dynamic text on image imagestring methodphp provides many functions to add dynamic text on images, but easy and simplest function is imagestring method. imagestring($image, $font, $x, $y, $string, $color); Method explanation:
imagecreate method. A palette based image is created using this method and image resource is returned. Height and width is passed as parameter to this method. imagecolorallocate method A color for an image is allocated using imagecolorallocate method. This method accepts an image resource and integer values for red, green, blue colors. White and black color is allocated in this example. imagestring method A string is drawn horizontally using this method at given co -ordinates. This method accepts 6 parameters as explained. Using header methods content type of image is passed to browser. imagepng method This method is used to output a PNG image to a file or to broswer. imagedestroy method This method destroys a given image resource and free the memory. Write dynamic text on image text using True Type FontsTrue type fonts were developed by apple. TTF or True Type Fonts provide much control to developers to display fonts. PHP provides imagettftext and imagefttext functions for true type fonts. This methods accetps 8 arguments. array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
$textImage = imagecreate(300, 100); $bg = imagecolorallocate($textImage, 102, 152, 210); $black = imagecolorallocate($textImage, 0, 0, 0); $text = 'Welcome to porgrammerblog.net'; $font = 'C:\\Windows\\Fonts\\arial.ttf'; imagefttext($textImage, 14, 0, 10, 50, $black, $font, $text); header("Content-type: image/png"); imagepng($textImage); imagedestroy($textImage); Watermark images using php alpha channelsTo add a watermark to a image using alpha channel, you can use imagecreatefrompng and imagecreatefromjpeg methods. To add watermark using php you need an image and a stamp image. Stamp image is applied as watermark to image. Suppose there is an image of a flower and you want to apply copyrights image as watermark or stamp. Remember to set path of these file correctly. In this case both image files are placed in same directory. Copyrights image Flower Image $stamp = imagecreatefrompng('copyrights.png'); $image = imagecreatefromjpeg('flower.jpeg'); // Set the margins for the stamp and get the height/width of the stamp image $right = 200; $bottom = 150; $sx = imagesx($stamp); $sy = imagesy($stamp); // Copy the stamp image onto our photo using the margin offsets and the photo // width to calculate positioning of the stamp. imagecopy($image, $stamp, imagesx($image) - $sx - $right, imagesy($image) - $sy - $bottom, 0, 0, imagesx($stamp), imagesy($stamp)); // Output and free memory header('Content-type: image/png'); imagepng($image); imagedestroy($image); First copyrights.png is loaded using imagecreatepng function. Secondly flower.jpeg file is loaded using imagecreatefromjpeg. Right and bottom margins for stamp image are set. Using imagesx and imagesy method, height and width is set for stamp image. Imagecopy method is used to copy stamp image to original image as watermark. imagecopy($image, $stamp, imagesx($image) - $sx - $right, imagesy($image) - $sy - $bottom, 0, 0, imagesx($stamp), imagesy($stamp)); In the function margins for stamp image are set and then imagepng method is used to create watermarked image. Finally image is destroyed using image destroy method. On flower image, copyright image is placed. Translucent watermark using php with imagecopymerge()In last example, an image was used as a stamp. In this example text is used asa stamp and opacity of text is also set. Stamp text is merged with original image using imagecopymerge function. $image = imagecreatefromjpeg('flower.jpeg'); // First we create our stamp image manually from GD $stamp = imagecreatetruecolor(200, 70); imagefilledrectangle($stamp, 0, 0, 199, 169, 0x0000FF); imagefilledrectangle($stamp, 9, 9, 190, 60, 0xFFFFFF); imagestring($stamp, 5, 20, 20, 'Programmer Blog', 0x0000FF); imagestring($stamp, 3, 20, 40, '(c) 2017', 0x0000FF); // Set the margins for the stamp and get the height/width of the stamp image $right = 10; $bottom = 10; $sx = imagesx($stamp); $sy = imagesy($stamp); // Merge the stamp onto our photo with an opacity of 50% imagecopymerge($image, $stamp, imagesx($image) - $sx - $right, imagesy($image) - $sy - $bottom, 0, 0, imagesx($stamp), imagesy($stamp), 70); // Save the image to file and free memory imagepng($image, 'flower_stamp.png'); imagedestroy($image); Flower.jpeg image is loaded using imagecreatefromjpeg method. A text stamp is created using imagecreatetruecolor with X and Y co-ordinates. Imagefillrectangle is used to create rectangles to display text inside. Imagestring function is used to create dynamic text that is Programmer Blog. Next line for copyright information is created. Right and bottom margins are specified for the stamp image. Height and width of stamp is assigned to $x and $y using imagesx and imagesy methods. imagecopymerge method merges stamp with original image. Margins with opacity is specified with value 70. New watermarked image is saved with name flower_stamp.png in the same directory where the php file is placed. Image is destroyed to free memory. You can view the generated image below. SummaryTo summarize, in examples above image is generated with text and true type fonts. Then learned to watermark images using php. First an image is used as watermark on an original image and second example used text as watermark. You can download source code from this link. Please follow us on twitter or subscribe to our newsletter to stay updated about latest articles. Please leave your feedback and comments below. Previous Article:
How to create watermark using PHP?Upload file to server using move_uploaded_file() function in PHP. Load and create a new stamp from the watermark image using imagecreatefrompng() function. Load and create a new image from the uploaded image based on the file type. Set the right and bottom margin for the watermark image.
How to add watermark text in PHP?We can use the GD extension in PHP to add a watermark to images:. Open the original image – $img = imagecreatefromjpeg("IMAGE. ... . Set the text color – $red = imagecolorallocatealpha($img, 255, 0, 0);. Add the text to the image – imagettftext($img, 18, 0, 0, 24, $red, "PATH\FONT.. How to watermark photos in PHP?Good Example of watermark image and positioned at the center
php // Load the stamp and the photo to apply the watermark to $stamp = imagecreatefrompng('stampimg. png'); $im = imagecreatefrompng('mainimage.
How do I add a watermark to my photos?Insert a picture watermark. On the Design tab, select Watermark.. Select Custom Watermark, and then choose Picture Watermark.. Click Select Picture.. Find a picture of your own, or search Bing images.. Choose the picture you want, and select Insert.. |