Sign in Registration
ruen

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.

watermark-images-php

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.

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.