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.

There are a large number of images on the Internet, which, as a rule, are uploaded by site owners or site users. Depending on the relevance, the image can migrate from site to site. Not to mention copyright, not every site owner may like the fact that images from his site are copied on other resources. As a means of combating banal copying of site images, it was invented to impose watermarks on images, indicating the belonging of the image to a particular resource. This can be especially true for sites that have a large number of unique images.

Let's look at a code example that demonstrates the imposition of a watermark on uploaded images.

So, the main settings are contained in the form of constants, and come first in the code:

// path to the watermark image define ("WATERMARK_OVERLAY_IMAGE", "/lab/watermark/watermark.png"); // Compression, range 0-100 (affects image quality) define ("WATERMARK_OUTPUT_QUALITY", 100); // folder with source images define ("UPLOADED_IMAGE_DESTINATION", "/ lab / watermark / upload / src /"); // folder with processed images define ("WATERMARK_IMAGE_DESTINATION", "/ lab / watermark / upload /");

On the page, we will place an image upload form, with which images will be sent to the server.
Form code:

Select image file:
Original image
"style =" max-width: 300px; "/>
Image with watermark
"style =" max-width: 300px; "/>

Well, now the most important thing is the functions for image processing. Place these functions before showing the form on the page.

Two key functions, with the help of one the image is uploaded to the server, with the help of the other the watermark is applied. Perhaps, the comments to the functions are described in more detail, and it will be superfluous to write the same thing here.

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.

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

There are many ways to watermark an image using PHP, but my client needed a way of applying that would allow changing the watermark to a new one at any time.

As it turned out, there is such a solution.

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

Choosing a Watermarking Method

The problem with implementing all of this mess is choosing a watermarking method between performance and flexibility. The perfect solution that would suit everyone simply does not exist. This is why there are many implementations.

In my case, the customer reserves the right to rebrand at any time and instead of "Horns and hooves" to write "Hooves and Horns"... The chosen method of watermarking must endure 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 you to dynamically apply a watermark, then this is exactly what you need to do.

If anyone has an easier way then you are welcome in the comments. It will be interesting to listen to.

Before bringing any code, I would like to describe the merits, as well as give a working example.

Pros:

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

Minuses:

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

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

Example

Implementing watermarking with PHP

As promised, for this you do not need to have any special knowledge, you must:

  1. the file that is in the archive and placed in the root directory of your site;
  2. the image, which will act as a watermark, is placed in the root directory of the site and name it (in my case, it is a white brushstroke, so following the link it may not be distinguishable against the background of your browser). The image must be exactly PNG, as it contains a transparent layer. If you want to use GIF, then you need to edit the file image.php;
  3. in the place where you want to display an image with a watermark, place the code:

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