You are at great risk when you publish your images and videos on the Internet, as your materials can easily be copied to hundreds of other resources. It will not be very cool to find your picture for the news, for example, on which you worked hard, on another site without specifying the source, that is, your site, isn't it? You, to put it mildly, will be upset, and if it was not a simple picture for the news, but a difficult job in Photoshop, to say that you will be angry is to say nothing! So what can you do to protect your artwork?

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

First, let's 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 CEH define ("WATERMARK_OUTPUT_QUALITY", 100); // The quality of the resulting image from the CEH. 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 a superimposed digital watermark

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

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

For example, if the uploaded image was:

Then after downloading and overlaying the watermark, you get the following image:

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

(178.4 KiB, 989 hits)

If you want to add a watermark to a photo without bothering with graphic 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 the jpg image header ("content-type: image / jpeg");

Then we render a png image and get its dimensions:

// create a png watermark $ 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 a common occurrence for photos that are uploaded via a form. We act as follows:

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

Now we need to place a watermark on the image:

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

Then we'll 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 memory imagedestroy ($ image); imagedestroy ($ watermark);

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

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

Header ("content-type: image / jpeg"); // get the name of the image 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 memory imagedestroy ($ image); imagedestroy ($ watermark);

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

Site owners (forums, message boards, etc.) often face the problem of creating watermarks on all large site images.

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

The solution to this problem can be the dynamic imposition of a watermark on the image before transferring 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.
The 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 an alpha channel // The last function parameter is the 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 controlled 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 is as follows, the ".htaccess" file is placed in the directory with the image files. In addition to it, a folder "watermark" is created on the server, which contains the script file "_watermark.php" and the actual watermark file "watermark.png".

At the same time, I made minor changes to both text files in comparison with the original version I met on the Internet.

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

The script "_watermark.php" has been redesigned to place a 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 (comments in the body of the script will help you set this parameter yourself).

You should also pay attention to the fact that by placing the "watermark" folder in the folder with images, as the original sources advise, we will not achieve the desired result. in this case we will have to have our own ".htaccess" file and "watermark" folder in each folder. This is because the ".htaccess" file specifies the absolute paths from the site root directory to the "_watermark.php" file. Thus, having a separate "watermark" subfolder in each folder with images, if it is necessary to change the watermark (or a script that imposes 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 putting the ".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 only in 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 in order to overlay watermarks on 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.png" watermark file in it with your own own, and place the ".htaccess" file in those site directories, images from which should be marked with a watermark.

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