Hello everyone, welcome back at sahretech. On this occasion, we will learn how to make watermarks on images with text in PHP. Intrigued?, let's follow the tutorial below.
Previously, I made an article on how to make an image watermark with an image in PHP. Click on the following link to view. link. Watermarks are one way to let others know that this image is ours. With programming we can provide watermarks automatically and upload them to the internet. It's very easy and fun.
How to Make a Text Watermark in PHP
1. Create a new folder with the name watermark_text in the htdocs
folder. Then create an index.php file, enter the image you want to
watermark and also enter the font to be used.
You can download the fonts at this link
https://fonts.google.com/specimen/Roboto. don't forget to extract and fetch only the required fonts. You can see the
contents of the folder in the image below.
2. Open the index.php file then add the script in it with the script
below.
<?php
// Creates an image object from an existing file
$img = imagecreatefrompng('image.png');
// Specifies the font to be used
$font_path = realpath('Roboto-Bold.ttf');
// Specifies the text color
$text_color = imagecolorallocate($img, 255, 255, 255);
// Specifies the watermark text
$text = "sahretech.com";
// Specifies the text size
$text_size = 10;
// Specifies the position of the text
$x = 10;
$y = 20;
// Write text on image with imgettftext()
// you can rotate the text by changing the number 0
// $x is the position from the side
// $y is the position from above
imagettftext($img, $text_size, 0, $x, $y, $text_color, $font_path, $text);
// you can also write text on image with imagestring() function
// imagestring($img, 5, 10, 10, "sahretech.com", $text_color);
// Show pictures
header('Content-Type: image/png');
imagepng($img);
// Delete image object from memory
imagedestroy($img);
?>
Now try to open localhost/watermark_text in the browser. If true, then
the result looks like the image below.
How to Make a Watermark With Repetitive Text in PHP
You can also make the watermark text repeat to prevent the image from being
partially cropped by others. The script is like below.
<?php
// Creates an image object from an existing file
$img = imagecreatefrompng('image.png');
// Retrieve the original size of the image
$width = imagesx($img);
$height = imagesy($img);
// Specifies fonts
$font_path = realpath('Roboto-Bold.ttf');
// Specifies the text color
$text_color = imagecolorallocate($img, 255, 255, 255);
// Specifies the watermark text
$text = "sahretech";
// Specifies the text size
$text_size = 10;
// Specifies the position of the text
$x = 50;
$y = 50;
// Specifies the spacing between text
$distance = 100;
// Specifies the number of iterations
$loop_x = ceil($width / ($distance + $text_size));
$loop_y = ceil($height / ($distance + $text_size));
// Loop to add text on image
for ($i = 0; $i < $loop_x; $i++) {
for ($j = 0; $j < $loop_y; $j++) {
// 40 is the text rotation
imagettftext($img, $text_size, 40, $x + $i * $distance, $y + $j * $distance, $text_color, $font_path, $text);
}
}
// Show pictures
header('Content-Type: image/png');
imagepng($img);
// Delete image object from memory
imagedestroy($img);
When executed, the result will look like the image below.
Ok, that's all for our tutorial this time about how to make a watermark with
text in php. Hopefully useful and can be implemented. If you have questions,
please ask directly in the comments column below. That is all and thank you.
0 Comments
Come on ask us and let's discuss together
Emoji