📜 ⬆️ ⬇️

Adding a watermark to all images of the site

A request came from the client, on the old site, to update all the images, namely to add a watermark with a logo on them. The problem was that the pictures had more than 10,000 pieces and they were in different folders and under folders.


We decided to write a universal script that can be run from the console or directly in the browser and update all the images on the site.


And so the task:


  1. Pictures are in the img folder in the root of the site;
  2. Add a watermark in the center;
  3. Move all the pictures in the folder img2;

In the process of solving the problem, it turned out that all the pictures are also of different sizes from 200 to 7000 pixels in width, and the watermark in the form of a logo should be for everyone. How we solved this problem:


Stage 1. Bypass all files


First you need to know what we will work with, and for this we have displayed all the files and folders that are in the img folder on the site.


$path = $_SERVER['DOCUMENT_ROOT']; //  $root = $path."/img"; //  

And the function to bypass all the items in the folder:


 function find_new($dir) { $new_dir = null; $dir_files = opendir($dir); while(false !== ($file = readdir($dir_files))) { if($file != '.' && $file != '..') $new_dir[] = $dir."/".$file; } if($new_dir) foreach($new_dir as $check ) { if(is_file($check)) { echo $check . "<br>"; } elseif(is_dir($check)) find_new($check); } } find_new($root); 

As can be seen from the listing function is recursive, as a result of the work of this script, many lines appear on the screen with absolute paths to the files on the site.


The only thing that is not implemented here, check on the pictures, but it was not useful to us.


Stage 2. Recreate the folder structure


Since we have a lot of folders with subfolders, and so on up to level 10, for the successful operation of all the functions of copying and moving files, we need a ready structure.


To do this, we define the name of the folder, of course, and if not, then create:


 $fileName = basename($check); //   $new = str_replace("img","img2",$check); //   $put = substr($new,0,-strlen($fileName)); //    if (!file_exists($put)) { mkdir($put, 0777, true); //  ,   } 

This code is inserted after: echo $ check; and when executed, it generates a new folder structure, on your server, while you can run it infinitely many times, it does not damage the structure, but creates a neighboring img2 folder.


Stage 3. Add a logo to the images


For this, we will use four standard functions: imagecreatefrompng , imagecreatefromjpeg , imagecopy , imagejpeg and a couple of additional types: imagedestroy , imagesx .


All this is the GD library for PHP, connected by default by everyone and like this:


 $stamp = imagecreatefrompng('stamp.png'); //,   $sx = imagesx($stamp); //  $sy = imagesy($stamp); // $im = imagecreatefromjpeg($check); //  imagecopy($im, $stamp, imagesx($im) - $sx - 10, imagesy($im) - $sy - 10, 0, 0, imagesx($stamp), imagesy($stamp)); //    imagejpeg($im, $new, 100); //  imagedestroy($im); //   

Thus, after 3-5 minutes of the script, depending on the number of files, we get a copy of all the images in the img2 folder, but the logo is in the lower right corner, and all the pictures have a different size. You can play with the numbers in imagecopy but the effect of this will not be. The pictures are different, so the watermark should be different, so we go to stage 4.


4 stage. Adaptation and alignment of the logo


To do this, we need to convert the original logo stamp.png to the size of the image on which we will place it, and paste it exactly in the middle.
Let's start:


 $stamp = imagecreatefrompng('stamp.png'); //,    2000*1500 $sx = imagesx($stamp); //  $sy = imagesy($stamp); // $im = imagecreatefromjpeg($check); //  ///    $w = imagesx($im) - 20; //    $koe=$sx/$w; //   $h=ceil($sy/$koe); //   //echo $sx."-".$sy." ".$w."-".$h." ".$koe."<BR>"; //   $sim = imagecreatetruecolor($w, $h); //     $transparent = imagecolorallocatealpha($sim, 0, 0, 0, 127); //    imagefill($sim, 0, 0, $transparent); //    imagesavealpha($sim, true); //    imagecopyresampled($sim,$stamp,0,0,0,0,$w,$h,$sx,$sy); //    $sim $cn = ceil((imagesy($im) - $h)/2); //     imagecopy($im, $sim, imagesx($im) - $w - 10, $cn, 0, 0, imagesx($sim), imagesy($sim)); //    

If at stage 3, in imagecopy, imagesx ($ stamp) were used as the dimensions of the inserted image, here we are already using the dimensions of the new imagesx logo ($ sim).


The logo contains indents of 10 pixels on the left and on the right, and is set respectively by numbers 20 and 10 in the code.


Step 5. Add the conversion function to our cycle:


Since the script was going on the knee, of course it can still be simplified and improved, your suggestions in the comments. But here is a working version:


 path = $_SERVER['DOCUMENT_ROOT']; $root = $path."/img"; $stamp = imagecreatefrompng('stamp.png'); $sx = imagesx($stamp); $sy = imagesy($stamp); function find_new($dir) { global $stamp; global $sx; global $sy; $new_dir = null; $dir_files = opendir($dir); while(false !== ($file = readdir($dir_files))) { if($file != '.' && $file != '..') $new_dir[] = $dir."/".$file; } if($new_dir) foreach($new_dir as $check ) { if(is_file($check)) { $w='';$h='';$koe='';$sim=''; //echo $check . "<br>"; $im = imagecreatefromjpeg($check); ///    $w = imagesx($im) - 20; //    $koe=$sx/$w; $h=ceil($sy/$koe); //  //echo $sx."-".$sy." ".$w."-".$h." ".$koe."<BR>"; $sim = imagecreatetruecolor($w, $h); $transparent = imagecolorallocatealpha($sim, 0, 0, 0, 127); //    imagefill($sim, 0, 0, $transparent); //    imagesavealpha($sim, true); //    imagecopyresampled($sim,$stamp,0,0,0,0,$w,$h,$sx,$sy); $cn = ceil((imagesy($im) - $h)/2); //     //    imagecopy($im, $sim, imagesx($im) - $w - 10, $cn, 0, 0, imagesx($sim), imagesy($sim)); $fileName = basename($check); $put = substr($check,0,-strlen($fileName)); $put = str_replace("img","cache",$put); if (!file_exists($put)) { mkdir($put, 0777, true); } $new = str_replace("img","cache",$check); imagejpeg($im, $new, 100); imagedestroy($im); } elseif(is_dir($check)) find_new($check); } } find_new($root); 

It is enough to put it in the root folder of the site, set the source and destination folder with pictures and run if there are a lot of pictures. Add first:


 ignore_user_abort(); set_time_limit(0); 

And run from the console to see the stages of work.


Experiments with the insertion of the logo, and the selection of the degree of transparency is best done in the destination folder, for this in the line $ root = $ path. ”/ Add / your / path / to / folder” or put the script in the destination folder and run from there.


We used the standard company logo with 60% transparency.


Conclusion


When the folder with the new files is ready, you just need to rename it from img2 to img. As a result, you will have a folder with source files on your site that can be archived or deleted, and a folder with photos marked with the logo.


This script is relevant to use on projects where images of products or articles are already loaded, and there is no possibility to install a script to add a watermark, or not at all.


The next task is to replace the exif data on all the pictures of the same site. What it is and how it is implemented, we will tell in the next article.


')

Source: https://habr.com/ru/post/424693/


All Articles