How to watermark images using php
PHP 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 method
php provides many functions to add dynamic text on images, but easy and simplest function is imagestring method.
imagestring($image, $font, $x, $y, $string, $color);
- First parameter is an image resource that is returned by imagecreate method
- Second argument is a font, it can be 1 , 2, 3, 4, 5 for builtin latin fonts
- X is co-ordinate for upper left corner.
- Y is co-ordinate for upper left corner
- String – actual string to be written
- Color is a color identifier created with imagecolorallocate 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.
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.
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.
This method is used to output a PNG image to a file or to broswer.
<?php $image = imagecreate(300, 100); $whitecolor = imagecolorallocate($image, 25, 233, 12); $blackcolor = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 15, 30, 'Copyrights Programmerblog.net', $blackcolor); header("Content-type: image/png"); imagepng($image); imagedestroy($image); ?>
Write dynamic text on image text using True Type Fonts
True 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 )
- First argument is image resource created from imagecreate method
- Second is font size, third argument is angle to specify counter clockwise rotation
- X co-ordinates defines base point of first character
- Y co-ordinate defines position of font baseline
- Color is index of color
- Path to font file, In this example path to ttf specified from fonts directory in Windows
$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 channels
To 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.
$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()
$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.