Advertisement
AVONnadozie

PHP Image Resize and Crop

Sep 12th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.81 KB | None | 0 0
  1. <?php
  2. ############ Configuration ##############
  3. $thumb_square_size      = 200; //Thumbnails will be cropped to 200x200 pixels
  4. $max_image_size         = 500; //Maximum image size (height and width)
  5. $thumb_prefix           = "thumb_"; //Normal thumb Prefix
  6. $destination_folder     = 'uploads/'; //upload directory ends with / (slash)
  7. $jpeg_quality           = 90; //jpeg quality
  8. ##########################################
  9.  
  10. //continue only if $_POST is set and it is a Ajax request
  11. if(isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
  12.  
  13.     // check $_FILES['ImageFile'] not empty
  14.     if(!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']['tmp_name'])){
  15.             die('Image file is Missing!'); // output error when above checks fail.
  16.     }
  17.    
  18.     //uploaded file info we need to proceed
  19.     $image_name = $_FILES['image_file']['name']; //file name
  20.     $image_size = $_FILES['image_file']['size']; //file size
  21.     $image_temp = $_FILES['image_file']['tmp_name']; //file temp
  22.  
  23.     $image_size_info    = getimagesize($image_temp); //get image size
  24.    
  25.     if($image_size_info){
  26.         $image_width        = $image_size_info[0]; //image width
  27.         $image_height       = $image_size_info[1]; //image height
  28.         $image_type         = $image_size_info['mime']; //image type
  29.     }else{
  30.         die("Make sure image file is valid!");
  31.     }
  32.  
  33.     //switch statement below checks allowed image type
  34.     //as well as creates new image from given file
  35.     switch($image_type){
  36.         case 'image/png':
  37.             $image_res =  imagecreatefrompng($image_temp); break;
  38.         case 'image/gif':
  39.             $image_res =  imagecreatefromgif($image_temp); break;          
  40.         case 'image/jpeg': case 'image/pjpeg':
  41.             $image_res = imagecreatefromjpeg($image_temp); break;
  42.         default:
  43.             $image_res = false;
  44.     }
  45.  
  46.     if($image_res){
  47.         //Get file extension and name to construct new file name
  48.         $image_info = pathinfo($image_name);
  49.         $image_extension = strtolower($image_info["extension"]); //image extension
  50.         $image_name_only = strtolower($image_info["filename"]);//file name only, no extension
  51.        
  52.         //create a random name for new image (Eg: fileName_293749.jpg) ;
  53.         $new_file_name = $image_name_only. '_' .  rand(0, 9999999999) . '.' . $image_extension;
  54.        
  55.         //folder path to save resized images and thumbnails
  56.         $thumb_save_folder  = $destination_folder . $thumb_prefix . $new_file_name;
  57.         $image_save_folder  = $destination_folder . $new_file_name;
  58.        
  59.         //call normal_resize_image() function to proportionally resize image
  60.         if(normal_resize_image($image_res, $image_save_folder, $image_type, $max_image_size, $image_width, $image_height, $jpeg_quality))
  61.         {
  62.             //call crop_image_square() function to create square thumbnails
  63.             if(!crop_image_square($image_res, $thumb_save_folder, $image_type, $thumb_square_size, $image_width, $image_height, $jpeg_quality))
  64.             {
  65.                 die('Error Creating thumbnail');
  66.             }
  67.            
  68.             /* We have succesfully resized and created thumbnail image
  69.             We can now output image to user's browser or store information in the database*/
  70.             echo '<div align="center">';
  71.             echo '<img src="uploads/'.$thumb_prefix . $new_file_name.'" alt="Thumbnail">';
  72.             echo '<br />';
  73.             echo '<img src="uploads/'. $new_file_name.'" alt="Resized Image">';
  74.             echo '</div>';
  75.         }
  76.        
  77.         imagedestroy($image_res); //freeup memory
  78.     }
  79. }
  80.  
  81. #####  This function will proportionally resize image #####
  82. function normal_resize_image($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){
  83.    
  84.     if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
  85.    
  86.     //do not resize if image is smaller than max size
  87.     if($image_width <= $max_size && $image_height <= $max_size){
  88.         if(save_image($source, $destination, $image_type, $quality)){
  89.             return true;
  90.         }
  91.     }
  92.    
  93.     //Construct a proportional size of new image
  94.     $image_scale    = min($max_size/$image_width, $max_size/$image_height);
  95.     $new_width      = ceil($image_scale * $image_width);
  96.     $new_height     = ceil($image_scale * $image_height);
  97.    
  98.     $new_canvas     = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
  99.    
  100.     //Copy and resize part of an image with resampling
  101.     if(imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height)){
  102.         save_image($new_canvas, $destination, $image_type, $quality); //save resized image
  103.     }
  104.  
  105.     return true;
  106. }
  107.  
  108. ##### This function corps image to create exact square, no matter what its original size! ######
  109. function crop_image_square($source, $destination, $image_type, $square_size, $image_width, $image_height, $quality){
  110.     if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize
  111.    
  112.     if( $image_width > $image_height )
  113.     {
  114.         $y_offset = 0;
  115.         $x_offset = ($image_width - $image_height) / 2;
  116.         $s_size     = $image_width - ($x_offset * 2);
  117.     }else{
  118.         $x_offset = 0;
  119.         $y_offset = ($image_height - $image_width) / 2;
  120.         $s_size = $image_height - ($y_offset * 2);
  121.     }
  122.     $new_canvas = imagecreatetruecolor( $square_size, $square_size); //Create a new true color image
  123.    
  124.     //Copy and resize part of an image with resampling
  125.     if(imagecopyresampled($new_canvas, $source, 0, 0, $x_offset, $y_offset, $square_size, $square_size, $s_size, $s_size)){
  126.         save_image($new_canvas, $destination, $image_type, $quality);
  127.     }
  128.  
  129.     return true;
  130. }
  131.  
  132. ##### Saves image resource to file #####
  133. function save_image($source, $destination, $image_type, $quality){
  134.     switch(strtolower($image_type)){//determine mime type
  135.         case 'image/png':
  136.             imagepng($source, $destination); return true; //save png file
  137.             break;
  138.         case 'image/gif':
  139.             imagegif($source, $destination); return true; //save gif file
  140.             break;          
  141.         case 'image/jpeg': case 'image/pjpeg':
  142.             imagejpeg($source, $destination, $quality); return true; //save jpeg file
  143.             break;
  144.         default: return false;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement