Share Pastebin
Guest
Public paste!

Smart Photo Resizer

By: a guest | Apr 1st, 2010 | Syntax: PHP | Size: 11.27 KB | Hits: 219 | Expires: Never
Copy text to clipboard
  1. <?
  2.         /**************************************
  3.          *       Smart Photo Resizer          *
  4.          *         By: Greg Schoppe           *
  5.          *         www.gschoppe.com           *
  6.          *            04/01/2010              *
  7.          *  Resizes, Crops, Tints, watermarks *
  8.          *           Requires  GD             *
  9.          **************************************/
  10.  
  11.   /*
  12.     function smart_resize($file,
  13.                               $width              = 0,
  14.                               $height             = 0,
  15.                               $proportional       = 'absolute', // absolute | proportional | proportionalcrop | crop
  16.                               $padding                    = 'none';             // none | transparent | #xxxxxx
  17.                               $filter             = 'none',     // none | bw | sepia | washout | #xxxxxx
  18.                               $overlay            = 'none',     // define watermark, or thumbnail overlay as path
  19.                               $overlay_position   = 'br',       // define watermark position - (t|m|b)(l|c|r) eg tl = top left
  20.                               $output             = 'file',     // file | browser | return
  21.                               $outputFile         = 'none',     // none or directory path
  22.                               $delete_original    = true,
  23.                               $use_linux_commands = false ) {
  24.   */
  25.  
  26.   function validateHexColor($color) {
  27.     $color = substr($color, 0, 7);
  28.         return preg_match('/#[0-9a-fA-F]{6}/', $color);
  29.   }
  30.  
  31.   function imagefilterhue($im,$r,$g,$b){
  32.     $rgb = $r+$g+$b;
  33.     $col = array($r/$rgb,$b/$rgb,$g/$rgb);
  34.     $height = imagesy($im);
  35.     $width = imagesx($im);
  36.     for($x=0; $x<$width; $x++){
  37.         for($y=0; $y<$height; $y++){
  38.             $rgb = ImageColorAt($im, $x, $y);
  39.             $r = ($rgb >> 16) & 0xFF;
  40.             $g = ($rgb >> 8) & 0xFF;
  41.             $b = $rgb & 0xFF;
  42.             $newR = $r*$col[0] + $g*$col[1] + $b*$col[2];
  43.             $newG = $r*$col[2] + $g*$col[0] + $b*$col[1];
  44.             $newB = $r*$col[1] + $g*$col[2] + $b*$col[0];
  45.             imagesetpixel($im, $x, $y,imagecolorallocate($im, $newR, $newG, $newB));
  46.         }
  47.     }
  48.   }
  49.  
  50.   function imagesepia( $img ) {
  51.     $total = imagecolorstotal( $img );
  52.     for ( $i = 0; $i < $total; $i++ ) {
  53.         $index = imagecolorsforindex( $img, $i );
  54.         $red = ( $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189 ) / 1.351;
  55.         $green = ( $index["red"] * 0.349 + $index["green"] * 0.686 + $index["blue"] * 0.168 ) / 1.203;
  56.         $blue = ( $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131 ) / 2.140;
  57.         imagecolorset( $img, $i, $red, $green, $blue );
  58.     }
  59.   }
  60.  
  61.   function smart_resize($file,
  62.                               $width              = 0,
  63.                               $height             = 0,
  64.                               $proportional       = 'absolute', // absolute | proportional | proportionalcrop | crop
  65.                               $padding                    = 'none',             // none | transparent | #xxxxxx
  66.                               $filter             = 'none',     // none | bw | sepia | washout | #xxxxxx
  67.                               $overlay            = 'none',     // define watermark, or thumbnail overlay as path
  68.                               $overlay_position   = 'br',       // define watermark position - (t|m|b)(l|c|r) eg tl = top left
  69.                               $output             = 'file',     // file | browser | return
  70.                               $outputFile         = 'none',     // none or directory path
  71.                               $delete_original    = true,
  72.                               $use_linux_commands = false ) {
  73.      
  74.     if ( $height <= 0 && $width <= 0 ) return false;
  75.  
  76.     # Setting defaults and meta
  77.    $info                         = getimagesize($file);
  78.     $image                        = '';
  79.     $final_width                  = 0;
  80.     $final_height                 = 0;
  81.     list($width_old, $height_old) = $info;
  82.     $cropIt = false;
  83.  
  84.     # Calculating proportionality
  85.         switch ( $proportional ) {
  86.       case 'proportional'               :
  87.                 if      ($width  == 0)  $factor = $height/$height_old;
  88.                 elseif  ($height == 0)  $factor = $width/$width_old;
  89.                 else                    $factor = min( $width / $width_old, $height / $height_old );
  90.  
  91.                 $final_width  = round( $width_old  * $factor );
  92.                 $final_height = round( $height_old * $factor );
  93.                 break;
  94.       case 'proportionalcrop'   :
  95.                 if      ($width  == 0)  $factor = $height/$height_old;
  96.                 elseif  ($height == 0)  $factor = $width/$width_old;
  97.                 else                    $factor = max( $width / $width_old, $height / $height_old );
  98.  
  99.                 $final_width  = round( $width_old  * $factor );
  100.                 $final_height = round( $height_old * $factor );
  101.                 $cropIt = true;
  102.                 break;
  103.       case 'crop'                               :
  104.                         $final_width  = $width_old;
  105.                     $final_height = $height_old;
  106.                     $cropIt = true;
  107.                 break;
  108.       default:
  109.                     $final_width  = ( $width  <= 0 ) ? $width_old : $width;
  110.                     $final_height = ( $height <= 0 ) ? $height_old : $height;
  111.     }
  112.        
  113.        
  114.     # Loading image to memory according to type
  115.    switch ( $info[2] ) {
  116.       case IMAGETYPE_GIF:   $image = imagecreatefromgif($file);   break;
  117.       case IMAGETYPE_JPEG:  $image = imagecreatefromjpeg($file);  break;
  118.       case IMAGETYPE_PNG:   $image = imagecreatefrompng($file);   break;
  119.       default: return false;
  120.     }
  121.    
  122.     $image_resized = imagecreatetruecolor( $final_width, $final_height );
  123.     if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  124.       $transparency = imagecolortransparent($image);
  125.  
  126.       if ($transparency >= 0) {
  127.         $transparent_color  = imagecolorsforindex($image, $trnprt_indx);
  128.         $transparency       = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  129.         imagefill($image_resized, 0, 0, $transparency);
  130.         imagecolortransparent($image_resized, $transparency);
  131.       }
  132.       elseif ($info[2] == IMAGETYPE_PNG) {
  133.         imagealphablending($image_resized, false);
  134.         $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  135.         imagefill($image_resized, 0, 0, $color);
  136.         imagesavealpha($image_resized, true);
  137.       }
  138.     }
  139.     imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);
  140.    
  141.     //filter Code
  142.     // none | bw | sepia | washout | #xxxxxx
  143.    
  144.     switch ( $filter ) {
  145.                 case 'bw' :
  146.                         //make it bw
  147.                         imagefilter($image_resized, IMG_FILTER_GRAYSCALE);
  148.                         break;
  149.                 case 'sepia' :
  150.                         //make it sepia
  151.                         imagefilter($image_resized, IMG_FILTER_GRAYSCALE);
  152.                         imagefilter($image_resized, IMG_FILTER_COLORIZE, 90, 55, 30);
  153.                         break;
  154.                 case 'washout' :
  155.                         //wash it out <-- still unimplemented
  156.                        
  157.                         break;
  158.                        
  159.                 default  :
  160.                         if (validateHexColor($filter))
  161.                         {
  162.                                 //colorize to #xxxxxx
  163.                                 $cA = sscanf($filter, '#%2x%2x%2x');
  164.                                 imagefilterhue($image_resized, $cA[0], $cA[1], $cA[2]);
  165.                         }
  166.         }
  167.    
  168.    
  169.     //padding code
  170.     if($padding != 'none') {
  171.                 $image_resized2 = imagecreatetruecolor( $width, $height );
  172.                 if( ($padding == 'transparent') && ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) )
  173.                 {
  174.                         $transparency = imagecolortransparent($image);
  175.                         if ($transparency >= 0) {
  176.                                 $transparent_color  = imagecolorsforindex($image, $trnprt_indx);
  177.                                 $transparency       = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  178.                                 imagefill($image_resized, 0, 0, $transparency);
  179.                                 imagecolortransparent($image_resized, $transparency);
  180.                         }
  181.                         elseif ($info[2] == IMAGETYPE_PNG) {
  182.                                 imagealphablending($image_resized, false);
  183.                                 $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  184.                                 imagefill($image_resized, 0, 0, $color);
  185.                                 imagesavealpha($image_resized, true);
  186.                         }
  187.                 }
  188.                 elseif (validateHexColor($padding))
  189.                 {
  190.                         $cA = sscanf($padding, '#%2x%2x%2x');
  191.                         $color = imagecolorallocate($image_resized2, $cA[0], $cA[1], $cA[2]);
  192.                         imagefill($image_resized2, 0, 0, $color);
  193.                 }
  194.                 imagecopyresampled($image_resized2, $image_resized, 0, 0, 0, 0, $final_width, $final_height, $final_width, $final_height);
  195.                 $image_resized = $image_resized2;
  196.     }
  197.    
  198.     //crop code for proportionalcrop and crop mode
  199.     if($cropIt)
  200.     {
  201.                 // assuming that $img holds the image with which you are working
  202.                 $img_width  = imagesx($image_resized);
  203.                 $img_height = imagesy($image_resized);
  204.                
  205.                 // Starting point of crop
  206.                 $tlx = floor($img_width / 2) - floor ($width / 2);
  207.                 $tly = floor($img_height / 2) - floor($height / 2);
  208.                
  209.                 // Adjust crop size if the image is too small
  210.                 if ($tlx < 0)
  211.                 {
  212.                   $tlx = 0;
  213.                 }
  214.                 if ($tly < 0)
  215.                 {
  216.                   $tly = 0;
  217.                 }
  218.                
  219.                 if (($img_width - $tlx) < $width)
  220.                 {
  221.                   $width = $img_width - $tlx;
  222.                 }
  223.                 if (($img_height - $tly) < $height)
  224.                 {
  225.                   $height = $img_height - $tly;
  226.                 }
  227.                
  228.                 $image_resized2 = imagecreatetruecolor($width, $height);
  229.                 imagecopy($image_resized2, $image_resized, 0, 0, $tlx, $tly, $width, $height);
  230.                 $image_resized = $image_resized2;
  231.     }
  232.     //bool imagecopyresampled  ( resource $dst_image  , resource $src_image  , int $dst_x  , int $dst_y  , int $src_x  , int $src_y  , int $dst_w  , int $dst_h  , int $src_w  , int $src_h  )
  233.    
  234.    
  235.    
  236.    
  237.    
  238.    
  239.    
  240.    
  241.     # Taking care of original, if needed
  242.    if ( $delete_original ) {
  243.       if ( $use_linux_commands ) exec('rm '.$file);
  244.       else @unlink($file);
  245.     }
  246.        
  247.         //insert overlay Code
  248.         if($overlay != 'none')
  249.         {
  250.                 // Find base image size
  251.                 $swidth = imagesx($image_resized);
  252.                 $sheight = imagesy($image_resized);
  253.                  
  254.                 // Turn on alpha blending
  255.                 imagealphablending($image_resized, true);
  256.                  
  257.                 // Create overlay image
  258.                 $overlay = imagecreatefrompng($overlay);
  259.                  
  260.                 // Get the size of overlay
  261.                 $owidth = imagesx($overlay);
  262.                 $oheight = imagesy($overlay);
  263.                
  264.                 // define posion for top left corner $oposx, $oposy
  265.                 switch ( substr($overlay_position, 0, 1) ) {
  266.                         case 't' :
  267.                                 $oposy= 0;
  268.                                 break;
  269.                        
  270.                         case 'm' :
  271.                                 $oposy= ($sheight - $oheight) / 2;
  272.                                 break;
  273.                        
  274.                         default  :
  275.                                 $oposy= $sheight - $oheight;
  276.                 }
  277.                 switch ( substr($overlay_position, 1, 1) ) {
  278.                         case 'l' :
  279.                                 $oposx= 0;
  280.                                 break;
  281.                                
  282.                         case 'c' :
  283.                                 $oposx= ($swidth - $owidth) / 2;
  284.                                 break;
  285.                                
  286.                         default  :
  287.                                 $oposx= $swidth - $owidth;
  288.                 }
  289.                 // Overlay watermark
  290.                 imagecopy($image_resized, $overlay, $oposx, $oposy, 0, 0, $owidth, $oheight);
  291.         }
  292.        
  293.     # Preparing a method of providing result
  294.    switch ( strtolower($output) ) {
  295.       case 'browser':
  296.         $mime = image_type_to_mime_type($info[2]);
  297.         header("Content-type: $mime");
  298.         $output = NULL;
  299.       break;
  300.       case 'file':
  301.         if($outputFile == 'none')
  302.                         $output = $file;
  303.         else
  304.                         $output = $outputFile;
  305.       break;
  306.       case 'return':
  307.         return $image_resized;
  308.       break;
  309.       default:
  310.       break;
  311.     }
  312.    
  313.     # Writing image according to type to the output destination
  314.    switch ( $info[2] ) {
  315.       case IMAGETYPE_GIF:   imagegif($image_resized, $output);    break;
  316.       case IMAGETYPE_JPEG:  imagejpeg($image_resized, $output);   break;
  317.       case IMAGETYPE_PNG:   imagepng($image_resized, $output);    break;
  318.       default: return false;
  319.     }
  320.  
  321.     return true;
  322.   }
  323. ?>