Advertisement
krot

api.watermark.php

Nov 20th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.78 KB | None | 0 0
  1. <?php
  2. /*http://www.devshed.com/c/a/php/dynamic-watermarking-with-php/*/
  3. class watermark{
  4.  
  5.     # given two images, return a blended watermarked image
  6.     function create_watermark( $main_img_obj, $watermark_img_obj, $alpha_level = 100 ) {
  7.         $alpha_level    /= 100; # convert 0-100 (%) alpha to decimal
  8.    
  9.         # calculate our images dimensions
  10.         $main_img_obj_w = imagesx( $main_img_obj );
  11.         $main_img_obj_h = imagesy( $main_img_obj );
  12.         $watermark_img_obj_w    = imagesx( $watermark_img_obj );
  13.         $watermark_img_obj_h    = imagesy( $watermark_img_obj );
  14.        
  15.         # determine center position coordinates
  16.         $main_img_obj_min_x = floor( ( $main_img_obj_w / 2 ) - ( $watermark_img_obj_w / 2 ) );
  17.         $main_img_obj_max_x = ceil( ( $main_img_obj_w / 2 ) + ( $watermark_img_obj_w / 2 ) );
  18.         $main_img_obj_min_y = floor( ( $main_img_obj_h / 2 ) - ( $watermark_img_obj_h / 2 ) );
  19.         $main_img_obj_max_y = ceil( ( $main_img_obj_h / 2 ) + ( $watermark_img_obj_h / 2 ) );
  20.        
  21.         # create new image to hold merged changes
  22.         $return_img = imagecreatetruecolor( $main_img_obj_w, $main_img_obj_h );
  23.    
  24.         # walk through main image
  25.         for( $y = 0; $y < $main_img_obj_h; $y++ ) {
  26.             for( $x = 0; $x < $main_img_obj_w; $x++ ) {
  27.                 $return_color   = NULL;
  28.                
  29.                 # determine the correct pixel location within our watermark
  30.                 $watermark_x    = $x - $main_img_obj_min_x;
  31.                 $watermark_y    = $y - $main_img_obj_min_y;
  32.                
  33.                 # fetch color information for both of our images
  34.                 $main_rgb = imagecolorsforindex( $main_img_obj, imagecolorat( $main_img_obj, $x, $y ) );
  35.                
  36.                 # if our watermark has a non-transparent value at this pixel intersection
  37.                 # and we're still within the bounds of the watermark image
  38.                 if (    $watermark_x >= 0 && $watermark_x < $watermark_img_obj_w &&
  39.                             $watermark_y >= 0 && $watermark_y < $watermark_img_obj_h ) {
  40.                     $watermark_rbg = imagecolorsforindex( $watermark_img_obj, imagecolorat( $watermark_img_obj, $watermark_x, $watermark_y ) );
  41.                    
  42.                     # using image alpha, and user specified alpha, calculate average
  43.                     $watermark_alpha    = round( ( ( 127 - $watermark_rbg['alpha'] ) / 127 ), 2 );
  44.                     $watermark_alpha    = $watermark_alpha * $alpha_level;
  45.                
  46.                     # calculate the color 'average' between the two - taking into account the specified alpha level
  47.                     $avg_red        = $this->_get_ave_color( $main_rgb['red'],      $watermark_rbg['red'],      $watermark_alpha );
  48.                     $avg_green  = $this->_get_ave_color( $main_rgb['green'],    $watermark_rbg['green'],    $watermark_alpha );
  49.                     $avg_blue       = $this->_get_ave_color( $main_rgb['blue'], $watermark_rbg['blue'],     $watermark_alpha );
  50.                    
  51.                     # calculate a color index value using the average RGB values we've determined
  52.                     $return_color   = $this->_get_image_color( $return_img, $avg_red, $avg_green, $avg_blue );
  53.                    
  54.                 # if we're not dealing with an average color here, then let's just copy over the main color
  55.                 } else {
  56.                     $return_color   = imagecolorat( $main_img_obj, $x, $y );
  57.                    
  58.                 } # END if watermark
  59.        
  60.                 # draw the appropriate color onto the return image
  61.                 imagesetpixel( $return_img, $x, $y, $return_color );
  62.        
  63.             } # END for each X pixel
  64.         } # END for each Y pixel
  65.            
  66.         # return the resulting, watermarked image for display
  67.         return $return_img;
  68.    
  69.     } # END create_watermark()
  70.    
  71.     # average two colors given an alpha
  72.     function _get_ave_color( $color_a, $color_b, $alpha_level ) {
  73.         return round( ( ( $color_a * ( 1 - $alpha_level ) ) + ( $color_b    * $alpha_level ) ) );
  74.     } # END _get_ave_color()
  75.        
  76.     # return closest pallette-color match for RGB values
  77.     function _get_image_color($im, $r, $g, $b) {
  78.         $c=imagecolorexact($im, $r, $g, $b);
  79.         if ($c!=-1) return $c;
  80.         $c=imagecolorallocate($im, $r, $g, $b);
  81.         if ($c!=-1) return $c;
  82.         return imagecolorclosest($im, $r, $g, $b);
  83.     } # EBD _get_image_color()
  84.  
  85. } # END watermark API
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement