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.
Последние статьи
- 03.04.24ИТ / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24ИТ / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24ИТ / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23ИТ / Базы данных Ошибки при переходе с MySQL 5.6 на 5.7 и как их исправить - импорт дампа БД завершился ошибкой или не работает INSERT. Отключение строгого режима STRICT_TRANS_TABLES или использование IGNORE
- 08.07.22ИТ / Разное Конвертация офисных файлов DOC, DOCX, DOCM, RTF в форматы DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT без потерь и изменения разметки