Adding watermarks to images or copyright to photos using PHP
When your site starts to fill up with content, you may soon find that images from your site are being used on third-party sites without permission. How to protect against image theft from the site? This must be fought, for this, watermarks are applied to the images.
What is a watermark ? This is a sign overlaid on the picture. Anything can be such a sign - an ordinary text phrase or your own logo. Oftentimes, images on their site are labeled with the site's address or a name.
How do I add a watermark to my image ? There are several ways, for example, you can use graphic editors, special programs, etc. But for images on the site, the easiest way is to use PHP script to automatically add copyright to images.
Such a script for adding watermarks to images is very easy to develop, so there is no need to install third-party modules - you can quickly write your own, ideally suited to your specific needs.
Ready script for adding copyright to pictures :
function add_watermark () {
// Set watermark and image folder
$ watermark = 'Watermark';
$ directory = getcwd (). '/ images /';
// Search for jpg and png images in the specified folder
$ images = glob ($ directory. '/ *. {jpg, png}', GLOB_BRACE);
// Process each image
foreach ($ images as $ image) {
// Get information about the image
$ data = getimagesize ($ image);
// Coordinates of the applied text - approximately in the center of the image
$ width = $ data [0];
$ height = $ data [1];
$ x = ($ width / 2) - 30;
$ y = ($ height / 2) + 30;
// Create an image from the original based on its type
$ mime = $ data ['mime'];
if ($ mime == 'image / jpeg') {
$ type = 'jpeg';
$ i = imagecreatefromjpeg ($ image);
} else if ($ mime == 'image / png') {
$ type = 'png';
$ i = imagecreatefrompng ($ image);
imagesavealpha ($ i, true);
}
// Set the color and font for the text (the font file can be copied, for example, from Windows)
$ gray = imagecolorallocate ($ i, 230, 230, 230);
$ font = getcwd (). '/ fonts / verdana.ttf';
// Add a watermark to the image - text
imagettftext ($ i, 15, 45, $ x, $ y, $ gray, $ font, $ watermark);
// Overwrite the original image with a new signed image
// Save depending on the type - jpg or png
$ type == 'png'? imagepng ($ i, $ image): imagejpeg ($ i, $ image);
// Destroy the temporary image in RAM
imagedestroy ($ i);
}
}
Such a function can be placed in any convenient place, or it is better to create a small module for applying watermarks on images based on it. Such a module can automatically watermark images when adding or changing materials on the site. The functionality can be significantly expanded and integrated with the interface your CMS .
For example, you can do:
- select a folder with images via the interface;
- setting a watermark phrase using a text field;
- checkbox toggle - recreate all images or only images without a watermark;
- original images can be stored in a separate protected folder, signed images in another folder, etc.
If you need a similar functionality on your site to automatically add copyright to images - leave a request for development.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes