daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
  3.   $imgsize = getimagesize($source_file);
  4.   $width = $imgsize[0];
  5.   $height = $imgsize[1];
  6.   $mime = $imgsize['mime'];
  7.  
  8.   switch($mime){
  9.       case 'image/gif':
  10.           $image_create = "imagecreatefromgif";
  11.           $image = "imagegif";
  12.           break;
  13.  
  14.       case 'image/png':
  15.           $image_create = "imagecreatefrompng";
  16.           $image = "imagepng";
  17.           $quality = 7;
  18.           break;
  19.  
  20.       case 'image/jpeg':
  21.           $image_create = "imagecreatefromjpeg";
  22.           $image = "imagejpeg";
  23.           $quality = 80;
  24.           break;
  25.  
  26.       default:
  27.           return false;
  28.           break;
  29.   }
  30.  
  31.   $dst_img = imagecreatetruecolor($max_width, $max_height);
  32.   $src_img = $image_create($source_file);
  33.  
  34.   $width_new = $height * $max_width / $max_height;
  35.   $height_new = $width * $max_height / $max_width;
  36.   //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
  37.   if($width_new > $width){
  38.       //cut point by height
  39.       $h_point = (($height - $height_new) / 2);
  40.       //copy image
  41.       imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
  42.   }else{
  43.       //cut point by width
  44.       $w_point = (($width - $width_new) / 2);
  45.       imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
  46.   }
  47.  
  48.   $image($dst_img, $dst_dir, $quality);
  49.  
  50.   if($dst_img)imagedestroy($dst_img);
  51.   if($src_img)imagedestroy($src_img);
  52. }
  53.  
  54. $types = array( 'png', 'jpg', 'jpeg', 'gif' );
  55. if ( $handle = opendir('pastax') ) {
  56.     while ( $entry = readdir( $handle ) ) {
  57.         $ext = strtolower( pathinfo( $entry, PATHINFO_EXTENSION) );
  58.         if( in_array( $ext, $types ) )
  59.         //echo $entry;
  60.       //  echo '<img src="img/'.$id.'/'.$entry.'" alt="" class="img-thumbnail" style="width:140px;height:140px;margin:5px"">';
  61.     echo '
  62.          <div class="col s6 l2" style="margin-top:5px">
  63.          <img  class="materialboxed" style="width:140px; height:140px;" src="pastax/'.$entry.'">
  64.          </div>
  65.         ';
  66.  
  67.     }
  68.     closedir($handle);
  69. }
  70. ?>
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top