Computer lessons

php watermark. Creating Watermarks Using PHP

You take a big risk when you publish your images and videos on the Internet, since your materials can easily be copied to hundreds of other resources. It wouldn’t be very cool to find your picture for a news story, for example, which you worked hard on, on another site without indicating the source, that is, your site, would it? To put it mildly, you will be upset, and if it was not a simple picture for the news, but a complex job in Photoshop, to say that you will be angry is to say nothing! So what can you do to protect your graphics?

To protect the copyright of images or videos on the Internet, a digital watermark or digital watermark is usually used for convenience. Attach a digital signature to each uploaded image in order to protect it. The CEZ can be the logo of your website or company, beautifully and aesthetically placed on uploaded images.

Let's first create a file containing the necessary settings in the form of constants - /config.php:

Define("WATERMARK_OVERLAY_IMAGE", "/develop/images/watermark.png"); // Path to your Central Exhibition Hall define("WATERMARK_OUTPUT_QUALITY", 100); // Quality of the resulting image from digital video recording. Remember that quality directly affects file size. define("UPLOADED_IMAGE_DESTINATION", "/develop/folder1/"); // Path to the location of the original loaded images define("WATERMARK_IMAGE_DESTINATION", "/develop/folder2/"); // Path to images with overlaid digital waveform

Let's collect the files created above in the file executing the download /upload.php

Include("config.php"); include("functions.php"); $result = ImageUpload($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"]); if ($result === false)( echo "Download failed!"; )

For example, if the image being loaded was:

Then after downloading and applying a watermark, you will get the following image:

In this example, the downloaded image is saved in one folder, and the image on which the digital watermark was superimposed in another, so that you always have access to the original images, but, of course, it is worth posting images from the digital exhibition on the site.

(178.4 KiB, 989 hits)

Owners of websites (forums, message boards, etc.) are often faced with the problem of creating watermarks on all large images of the site.

Of course, you can solve this problem by manually creating a watermark on each image, however, firstly, this takes a lot of time, and secondly, it becomes necessary to store two versions of the image, with and without a watermark.

A solution to this problem can be to dynamically apply a watermark to the image before transmitting it to the site visitor.

There is a solution to this problem on the Internet in the form of two files, the contents of which are given below.
Source code of the ".htaccess" file

DirectoryIndex index.php RewriteEngine On RewriteCond %(REQUEST_FILENAME) -f RewriteRule ^(.*)$ /watermark/_watermark.php

Source code of the file "_watermark.php"

250) && ($info_o > 250)) ( // For images without alpha channel // The last parameter of the function is the degree of opacity of the watermark imageCopyMerge($out, $watermark, ($info_o-$info_w)/2, ($info_o -$info_w)/2, 0, 0, $info_w, $info_w, 25); // For images with an alpha channel // In this case, the transparency is adjusted by the alpha channel of the image itself // imageCopy($out, $watermark, ($info_o-$info_w)/2, ($info_o-$info_w)/2, 0, 0, $info_w, $info_w); ) switch ($info_o) ( case 1: imageGIF($out); break; case 2: imageJPEG($out); break; case 3: imagePNG($out); break; default: return false; ) imageDestroy($out); imageDestroy($original); imageDestroy($watermark); return true; ) ?>

The solution boils down to the following: the “.htaccess” file is placed in the directory with the image files. In addition to it, a “watermark” folder is created on the server, in which there is a script file “_watermark.php” and the actual watermark file “watermark.png”.

At the same time, compared to the original version found on the Internet, I made minor changes to both text files.

In the ".htaccess" file, the "jpeg" extension has been added to the regular expression for searching for image files, as it is also frequently encountered.

The "_watermark.php" script has been reworked in such a way as to place the watermark in the center of the image (this was required by the specifics of the problem being solved) and the ability to adjust the transparency of the created watermark has been added (the comments in the body of the script will help you configure this parameter yourself).

You should also pay attention to the fact that placing the "watermark" folder in the folder with images, as the original sources advise, we will not achieve the desired result, because in this case we will have to have its own ".htaccess" file and "watermark" folder in each folder. This is due to the fact that the ".htaccess" file specifies absolute paths from the site's root directory to the "_watermark.php" file. Thus, having a separate “watermark” subfolder in each folder with images, if we need to change the watermark (or the script that overlays it on the image), we will have to make changes in all folders.

To avoid this problem, I recommend creating a "watermark" folder in the root directory of the site, and placing a ".htaccess" file in the image directories without having to change it every time. In this case, to change the watermark or script, we will need to make changes in only one place on the site. In this case, you can create different watermarks for different folders with images by referring from different “.htaccess” files to different scripts, for example “_watermark-1.php”, “_watermark-2.php”, etc.

Thus, to summarize, we can say that to apply watermarks to all images of the site, you need to download the archive attached below, unpack it, place the “watermark” folder in the root directory of the site, replace the watermark file “watermark.png” in it with your own own, and place the ".htaccess" file in those site directories, images from which should be watermarked.

You can download the archive containing all the necessary files at this

If you want to add a watermark to a photo without bothering with image editors or add it while uploading photos to the server, then this tutorial is for you.

In this tutorial, I'll show you how to add a watermark to an image on the fly without actually changing the original image. First of all, you will need an image to use as your watermark.

Then we form the file header:

// this line will tell the browser that we are passing a jpg image header("content-type: image/jpeg");

Then we generate a png image and get its dimensions:

// create a watermark in png format $watermark = imagecreatefrompng("watermark.png"); // get the width and height $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark);

We will do the same with the original image, but only in jpg format. This is common for photos that are uploaded through a form. We proceed as follows:

// creating a jpg image $image_path = "original.jpg"; $image = imagecreatefromjpeg($image_path); // get the image size $size = getimagesize($image_path);

Now we need to place a watermark on the image:

// place a watermark at the bottom right. Indent 5px $dest_x = $size - $watermark_width - 5; $dest_y = $size - $watermark_height - 5;

Next, let's set up the blending options for both images:

Imagealphablending($image, true); imagealphablending($watermark, true);

Finally we create a new image using the parameters:

// create a new image imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); imagejpeg($image);

It is important to clean up after yourself:

// free up memory imagedestroy($image); imagedestroy($watermark);

You can use Photoshop to adjust the transparency of the watermark.

That's all with theory. Now let's apply our knowledge to a real project. All this must be saved to a file. For example, called watermark.php

Header("content-type: image/jpeg"); // get the image name via GET $image = $_GET["image"]; // create a watermark $watermark = imagecreatefrompng("watermark.png"); // get the height and width of the watermark $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); // create a jpg from the original image $image_path = "/path/to/image/folder/" . $image; $image = imagecreatefromjpeg($image_path); //if something goes wrong if ($image === false) ( return false; ) $size = getimagesize($image_path); // place a watermark on the image $dest_x = $size - $watermark_width - 5; $dest_y = $size - $watermark_height - 5; imagealphablending($image, true); imagealphablending($watermark, true); // create a new image imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); imagejpeg($image); // free up memory imagedestroy($image); imagedestroy($watermark);

Now, in order to show a photo with a watermark without changing the original image, use the following code.

In some cases, adding watermarks to the images you post on your site is the only way to somehow protect them from theft. In addition, they say that such watermarks attract additional traffic.

There are many ways to create a watermark on an image using PHP, but my customer needed an application method that would allow him to change the watermark to a new one at any time.

As it turns out, there is such a solution.

This is usually used by people who develop websites with mostly graphic content.

Selecting a watermark method

The problem with implementing all this mess is choosing how to apply the watermark between performance and flexibility. There is simply no ideal solution that would suit everyone. This is why there are many implementations.

In my case, the customer reserves the right to rebrand at any time and instead "Horns and hooves" write “Hooves and Horns”. The watermarking method you choose should tolerate this too.

The essence of the watermarking technology described here is to add this very mark every time an image is loaded. Yes, this method carries a number of limitations that can significantly affect performance, but as they say, the customer is always right and therefore if those. the task requires dynamically applying a watermark, then this is exactly what needs to be done.

If anyone has an easier way, then you are welcome to comment. It will be interesting to listen.

Before presenting some code, I would like to describe the advantages, as well as give a working example.

Pros:

  • you can change the watermark at least 500 times a day;
  • can be deployed in any CMS (it is not tied to it in any way).

Minuses:

  • depends on the performance of the hosting (if you have a lot of images or they are of high resolution, this can put a significant load on the server);
  • an inquisitive mind will still be able to remove your watermark.

Conclusions: for posting a small number of images with watermarks, this method is perfect, but if you are going to open a photo gallery, then it would be better to look for something less heavily loaded.

Example

Implementing a watermark using PHP

As promised, you don’t need to have any special knowledge for this, you need to:

  1. file, which is in the archive, and place it in the root directory of your site;
  2. We place the image that will act as a watermark in the root directory of the site and name it (in my case it’s a white brush stroke, so the link may not be visible against the background of your browser). The image must be a PNG, since it has a transparent layer. If you want to use GIF, you need to edit the file image.php;
  3. in the place where you want to display the image with the watermark, place the code:

That's all. Everyone is happy, both you and the customer.