Home Links
Home Page
Blocks try... catch... finally... in JScript 5.
Processing of events in language JavaScript
Job with Cookies on JavaScript
Processing of mistakes in PHP
Installation of the password on page
Erroneous methods of promotion of sites
Erroneous methods of promotion of sites
Keywords - the theory
Do not cling to searches!
Adjustment Firewall under ICQ
Adjustment Firewall under ICQ
Safety PHP+MYSQL+Apache
We use base MySQL
Creation of watermarks with help PHP
Twenty six ways of reception of the qualitative traffic on your site.
Unjustified use OOP
Simple way keshirovanija pages
PHP an example of parsing URL for « User Friendly URLs
 

Creation of watermarks with help PHP

The introduction


At the given stage sveogo developments PHP are offered programmers with a wide set of functions for dynamic generation of the image and job with them. In this clause{article} I shall show a technique of creation of a class which will put watermarks on these images.


This class will work with two images: initial and a watermark. As addition, the third parameter is entered still - our class will contain alpha - variable. It will allow to use the alpha - channel for our watermark.


For the information{inquiry}



The alpha - channel (alpha-channel): the part of the image storing{keeping} the information on a transparency of separate sites of the image whereas color channels store{keep} the information on color of the image. In graphic editors it is used for masking (protection against editing) some area of the image. In some applications they are called as a transparent mask.


The information which is taking place in the alpha - channel more often represents the selected areas - some forms or an arrangement of color areas. Preservation of the alpha - channel in the image increases the size of a file on 1/3. RGB images can have up to 24 alphas - channels. Dot and indexed images cannot contain alphas - channels.


Part the first - bases


Before beginning a spelling of the class, we shall consider functions which in him will be used. Their list:



* Returns width and height of the image

  imagesx ()

  imagesy ()

* Creates the new image true-color

  imagecreatetruecolor


* Returns an associative file with keys red, green and blue (+ the alpha - channel),

* Containing corresponding values for the specified index of color

  imagecolorsforindex ()


* Returns an index of color piksela in the specified place in the image

  imagecolorat ()


* Draws single piksel the set color

  imagesetpixel ()


* Returns an index an index of color in a palette of the image,

* The identifier of color (made of RGB-components)

* And an index of color of a palette of the image,

* Being "nearest" to RGB-value accordingly

* (these data are necessary for function imagesetpixel ())

  imagecolorexact ()

  imagecolorallocate ()

  imagecolorclosest ()


As it is possible to see, at php there are enough functions for job with grafikoj. Let purpose  of some from them and is not absolutely understandable in the theory, but in practice all garazdo is easier. Therefore, to understand, as with them to work, we apply them in our class.


Production of a problem


Now, when we were already defined{determined} with the purpose of our "mini-project", we shall a little return back and we shall talk about ways of its{her} realization.


For the beginning, our application receives two images - initial images and a watermark. Further it is necessary for us to define{determine} the sizes of these images (width-width and height-height). These data are necessary for us for an arrangement of a watermark in the center of the image (proceeding from the assumption, that the size of a watermark will be less than the figure).


Then it will be necessary to impose our watermark on the initial image. For this purpose we will need to combine colors (matematicheski) nakladyvaemykh images for reception of the third.


And in a result, we will need to display the received image in a browser. In this case figure will open directly from a source specified in tege <img>


I think, the theory has enough - the key moments in her are opened in detail enough. Now we shall pass directly to a spelling of a script.


Part the second - we write a script


Let's start with the most simple - we shall write a class which creates a file with a watermark. We shall name it  "watermark" and we shall register his  code in a file "api.watermark.php". "Skeleton" of a class will be three functions:



<? php

class watermark

   {

      * Function which merges two initial images in one

      function create_watermark () {}

      * Function for "averaging" colors of images

      function _get_ave_color () {}

      * Function which finds the nearest RGB-colors for the new image

      function _get_image_color () {}

}

?>


The following stage will be a spelling of a code of functions of a class "watermark". We supplement a file "api.watermark.php" with the next lines of a code:



* Function which merges two initial images in one

function create_watermark ($main_img_obj, $watermark_img_obj, $alpha_level=100)

  {

     $alpha_level / = 100; * we translate value of a transparency of the alpha - channel from % in tens

      * Calculation of the sizes of the image (width and height)

      $main_img_obj_w = imagesx ($main_img_obj);

      $main_img_obj_h = imagesy ($main_img_obj);

      $watermark_img_obj_w = imagesx ($watermark_img_obj);

      $watermark_img_obj_h = imagesy ($watermark_img_obj);

      * Definition of coordinates of the center of the image

      $main_img_obj_min_x = floor (($main_img_obj_w/2) - ($watermark_img_obj_w/2));

      $main_img_obj_max_x = ceil (($main_img_obj_w/2) + ($watermark_img_obj_w/2));

      $main_img_obj_min_y = floor (($main_img_obj_h/2) - ($watermark_img_obj_h/2));

      $main_img_obj_max_y = ceil (($main_img_obj_h/2) + ($watermark_img_obj_h/2));

      * Creation of the new image

      $return_img = imagecreatetruecolor ($main_img_obj_w, $main_img_obj_h);

      * We shall walk under the initial image

      * " Some code "

      * We display the image with a watermark

      return $return_img;

} * the end of function create_watermark ()


Now we shall more in detail consider function create_watermark ().

First of all we pass her of three parameters:



$main_img_obj * the initial image on which it is necessary to put a watermark

$watermark_img_obj * the watermark, should contain the alpha - channel

$alpha_level * value of a transparency of the alpha - channel of a watermark, (0-100, on umolchniju = 100)


(it is important to note, that our function accepts images as objects, instead of is simple as a way to them - but about it will be said hardly later)


Further we it is necessary for us to receive the information on each of images. To us it is necessary to know coordinates X and Y for an arrangement of a watermark in the center of the initial image.


The following stage will be creation new, true-color images with the same sizes, as well as at an initial picture. This image (a variable $return_img) will be used for association of the information from initial pictures (figure and a watermark).


But before it still it is necessary "to pass" on each of two initial izborazhenij and "to merge" them in one. Only it still early to do{make} - we are not yet ready to this. Instead of it we shall place the comment " some code ", and then we shall add this place to a site of a code.


The ending will be displays of our modified image in web - page which will request it . Further we shall consider the staying two auxiliary functions.


Part the third - auxiliary functions


Besides function create_watermark in our class watermark there are two more functions. We shall continue an initial code of a class in the next lines:



* Averaging two colors in view of a transparency of the alpha - channel

function _get_ave_color ($color_a, $color_b, $alpha_level)

{

     return round ((($color_a * (1 - $alpha_level)) + ($color_b * $alpha_level)));

}

* We return values of the nearest RGB-making of new figure

function _get_image_color ($im, $r, $g, $b)

{

   $c=imagecolorexact ($im, $r, $g, $b);

   if ($c! =-1) return $c;

   $c=imagecolorallocate ($im, $r, $g, $b);

   if ($c! =-1) return $c;

   return imagecolorclosest ($im, $r, $g, $b);

}


And now is more detailed. Our first function "_get_ave_color" accepts numerical sizes of two colors and the alpha - channel. She returns their average size. This function is necessary for us for definition of color which will turn out at imposing pixels of two figures.


The second function "_get_image_color" breaks the image on red (red), green (green) and dark blue (blue) components (rgb-palette). With the help built - in in php functions for job with grafikoj (their description was in the beginning of clause{article}) it is received the nearest value of color for the new image.


In addition some moments are still checked. First, if it was possible to receive exact value (a variable $c) it and comes back from function (return $c). Otherwise dalaetsja attempt to pick up color with the help of function imagecolorallocate (). If also it will not help to reach{achieve} result, with the help of function imagecolorclosest () the nearest value of color (most inexact) simply comes back.


Well, our class also is almost ready. It are necessary to replace only in function "create_watermark" the comment " some code " the next lines:



* We shall walk under the image

for ($y = 0; $y <$main_img_obj_h; $y ++)

{

      for ($x = 0; $x <$main_img_obj_w; $x ++)

       {

          $return_color = NULL;

          * Definition of a true arrangement of a pixel within the limits of our watermark

          $watermark_x = $x - $main_img_obj_min_x;

          $watermark_y = $y - $main_img_obj_min_y;

          * A choice of the information on color for our images

          $main_rgb = imagecolorsforindex ($main_img_obj, imagecolorat ($main_img_obj, $x, $y));

          * If our pixel of a watermark opaque

if ($watermark_x> = 0 ** $watermark_x <$watermark_img_obj_w ** $watermark_y> = 0 ** $watermark_y <$watermark_img_obj_h)

             {

               $watermark_rbg = imagecolorsforindex ($watermark_img_obj, imagecolorat ($watermark_img_obj, $watermark_x, $watermark_y));

               * Use of value of a transparency of the alpha - channel

               $watermark_alpha = round (((127 - $watermark_rbg ['alpha']) / 127), 2);

               $watermark_alpha = $watermark_alpha * $alpha_level;

               * Calculation of color in a place of imposing of pictures

               $avg_red = $this-> _ get_ave_color ($main_rgb ['red'], $watermark_rbg ['red'], $watermark_alpha);

               $avg_green = $this-> _ get_ave_color ($main_rgb ['green'], $watermark_rbg ['green'], $watermark_alpha);

               $avg_blue = $this-> _ get_ave_color ($main_rgb ['blue'], $watermark_rbg ['blue'], $watermark_alpha);

               * Using the received data, we calculate an index of color

$return_color = $this-> _ get_image_color ($return_img, $avg_red, $avg_green, $avg_blue);

               * If to fail to choose color we shall simply take a copy of an initial pixel

} else {$return_color = imagecolorat ($main_img_obj, $x, $y);}

* From the received pixels we draw new izoborazhenie

imagesetpixel ($return_img, $x, $y, $return_color);

}

}


After a spelling of such significant part of a code it is possible to make a pause and more in detail to stop on his  analysis.


First of all our script carries out detour of the image with the help of two cycles 'for'. Coordinates of each pixel of a watermark in parallel are still counted up.


Further it is prospected information about RGB for kazhogo a pixel. If current piksel to not be in the field of crossing the initial image and a watermark our class only duplicates piksel for the new image. In case of an arrangement of a pixel in the field of crossing, it is necessary for us to define{determine} his  color as result of imposing of initial figure and a watermark.


For definition of color of area of crossing, all over again it is necessary to receive value of a RGB-variable of a watermark, using the information which we have received in cycles 'for'. Then with the help of function "_get_ave_color" average value of color for the new image is defined{determined}. Function "_get_image_color" for definition of color scale which will be used by function "return_img" further follows.


In a result, after end of job of cycles 'for' we have ready image with a watermark.


And now we shall check up our class in business.


Part the fourth - the test - drajv


For the beginning we will need two files. The first we shall name "watermark_test.php" and we shall place in him the following code:



<! - original image->

<img src = "main.jpg">

<br> <br>

<! - watermarked image->

<img src = " image.php? main=main.jpg*watermark=watermark.png ">


Purposes{Appointments} ehtotgo a file very simple: he displays in a browser initial (main.jpg) and received (watermark.png, with a watermark) images.


As it is possible to see, our second image (watermark.png) refers to a php-file image.php, instead of on the file - image. This link looks like GET-search where in a php-file values of two variables are passed: $main and $watermark.


The second file we shall name "image.php" and and we shall place in him such code:



<? php

        * We connect our class 'watermark'

        include 'api.watermark.php';

        $watermark = new watermark ();

        * We create objects - images using initial files (main.jpg and watermark.png)

        $main_img_obj = imagecreatefromjpeg ($ _GET ['main']);

        $watermark_img_obj = imagecreatefrompng ($ _GET ['watermark']);

        * We create the image with a watermark - value of a transparency of the alpha - channel of a watermark we shall establish in 66 %

        $return_img_obj = $watermark-> create_watermark ($main_img_obj, watermark_img_obj, 66);

        * We shall display our received image in a browser - but all over again we inform him, what is it a jpeg-file

        header (' Content-Type: image/jpeg ');

        header (' Content-Disposition: inline; filename = '. $ _GET ['src']);

        imagejpeg ($return_img_obj, ", 50);

?>


Well also have stolen up to the ending.

For those who wants to receive more information on creation of images in popular formats, I allow some links:


http://www.php.net/manual/en/function.imagecreatefromgif.php

http://www.php.net/manual/en/function.imagecreatefromjpeg.php

http://www.php.net/manual/en/function.imagecreatefrompng.php


To test our script, simply start in a browser a file "watermark_test.php". As result, should be two images - initial and with a watermark.


And in summary


I hope, my clause{article} has helped you to understand better a principle of job with graphic library GD. More full information on job with images can be read on the Internet: http://www.php.net/manual/en/ref.image.php