Tero

[+] Easy Image RESIZE & OPTIMIZE Function

Aug 3rd, 2017
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Source: https://raw.githubusercontent.com/Nimrod007/PHP_image_resize/master/smart_resize_image.function.php
  4.  * easy image resize function
  5.  * @param  $file - file name to resize
  6.  * @param  $string - The image data, as a string
  7.  * @param  $width - new image width
  8.  * @param  $height - new image height
  9.  * @param  $proportional - keep image proportional, default is no
  10.  * @param  $output - name of the new file (include path if needed)
  11.  * @param  $delete_original - if true the original image will be deleted
  12.  * @param  $use_linux_commands - if set to true will use "rm" to delete the image, if false will use PHP unlink
  13.  * @param  $quality - enter 1-100 (100 is best quality) default is 100
  14.  * @param  $grayscale - if true, image will be grayscale (default is false)
  15.  * @return boolean|resource
  16.  */
  17.   function smart_resize_image($file,
  18.                               $string             = null,
  19.                               $width              = 0,
  20.                               $height             = 0,
  21.                               $proportional       = false,
  22.                               $output             = 'file',
  23.                               $delete_original    = true,
  24.                               $use_linux_commands = false,
  25.                               $quality            = 100,
  26.                               $grayscale          = false
  27.          ) {
  28.      
  29.     if ( $height <= 0 && $width <= 0 ) return false;
  30.     if ( $file === null && $string === null ) return false;
  31.  
  32.     # Setting defaults and meta
  33.    $info                         = $file !== null ? getimagesize($file) : getimagesizefromstring($string);
  34.     $image                        = '';
  35.     $final_width                  = 0;
  36.     $final_height                 = 0;
  37.     list($width_old, $height_old) = $info;
  38.     $cropHeight = $cropWidth = 0;
  39.  
  40.     # Calculating proportionality
  41.    if ($proportional) {
  42.       if      ($width  == 0)  $factor = $height/$height_old;
  43.       elseif  ($height == 0)  $factor = $width/$width_old;
  44.       else                    $factor = min( $width / $width_old, $height / $height_old );
  45.  
  46.       $final_width  = round( $width_old * $factor );
  47.       $final_height = round( $height_old * $factor );
  48.     }
  49.     else {
  50.       $final_width = ( $width <= 0 ) ? $width_old : $width;
  51.       $final_height = ( $height <= 0 ) ? $height_old : $height;
  52.       $widthX = $width_old / $width;
  53.       $heightX = $height_old / $height;
  54.      
  55.       $x = min($widthX, $heightX);
  56.       $cropWidth = ($width_old - $width * $x) / 2;
  57.       $cropHeight = ($height_old - $height * $x) / 2;
  58.     }
  59.  
  60.     # Loading image to memory according to type
  61.    switch ( $info[2] ) {
  62.       case IMAGETYPE_JPEG:  $file !== null ? $image = imagecreatefromjpeg($file) : $image = imagecreatefromstring($string);  break;
  63.       case IMAGETYPE_GIF:   $file !== null ? $image = imagecreatefromgif($file)  : $image = imagecreatefromstring($string);  break;
  64.       case IMAGETYPE_PNG:   $file !== null ? $image = imagecreatefrompng($file)  : $image = imagecreatefromstring($string);  break;
  65.       default: return false;
  66.     }
  67.    
  68.     # Making the image grayscale, if needed
  69.    if ($grayscale) {
  70.       imagefilter($image, IMG_FILTER_GRAYSCALE);
  71.     }    
  72.    
  73.     # This is the resizing/resampling/transparency-preserving magic
  74.    $image_resized = imagecreatetruecolor( $final_width, $final_height );
  75.     if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  76.       $transparency = imagecolortransparent($image);
  77.       $palletsize = imagecolorstotal($image);
  78.  
  79.       if ($transparency >= 0 && $transparency < $palletsize) {
  80.         $transparent_color  = imagecolorsforindex($image, $transparency);
  81.         $transparency       = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
  82.         imagefill($image_resized, 0, 0, $transparency);
  83.         imagecolortransparent($image_resized, $transparency);
  84.       }
  85.       elseif ($info[2] == IMAGETYPE_PNG) {
  86.         imagealphablending($image_resized, false);
  87.         $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  88.         imagefill($image_resized, 0, 0, $color);
  89.         imagesavealpha($image_resized, true);
  90.       }
  91.     }
  92.     imagecopyresampled($image_resized, $image, 0, 0, $cropWidth, $cropHeight, $final_width, $final_height, $width_old - 2 * $cropWidth, $height_old - 2 * $cropHeight);
  93.    
  94.    
  95.     # Taking care of original, if needed
  96.    if ( $delete_original ) {
  97.       if ( $use_linux_commands ) exec('rm '.$file);
  98.       else @unlink($file);
  99.     }
  100.  
  101.     # Preparing a method of providing result
  102.    switch ( strtolower($output) ) {
  103.       case 'browser':
  104.         $mime = image_type_to_mime_type($info[2]);
  105.         header("Content-type: $mime");
  106.         $output = NULL;
  107.       break;
  108.       case 'file':
  109.         $output = $file;
  110.       break;
  111.       case 'return':
  112.         return $image_resized;
  113.       break;
  114.       default:
  115.       break;
  116.     }
  117.    
  118.     # Writing image according to type to the output destination and image quality
  119.    switch ( $info[2] ) {
  120.       case IMAGETYPE_GIF:   imagegif($image_resized, $output);    break;
  121.       case IMAGETYPE_JPEG:  imagejpeg($image_resized, $output, $quality);   break;
  122.       case IMAGETYPE_PNG:
  123.         $quality = 9 - (int)((0.9*$quality)/10.0);
  124.         imagepng($image_resized, $output, $quality);
  125.         break;
  126.       default: return false;
  127.     }
  128.  
  129.     return true;
  130.   }
Advertisement
Add Comment
Please, Sign In to add comment