Advertisement
michaelyuen

Untitled

Jun 4th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.90 KB | None | 0 0
  1. <?php
  2. /* 
  3.         this function intends to reduce the size of image and not for enlargement
  4.         action will be aborted if target file is larger the source
  5.         tested with PHP Version 7.0.13
  6. */
  7. function resize($image, $thumb, $option = 'maxwidth', $dimension = array('200')) {
  8.     $methods = [
  9.                             IMAGETYPE_GIF => ['imagegif','imagecreatefromgif'],
  10.                             IMAGETYPE_JPEG => ['imagejpeg','imagecreatefromjpeg'],
  11.                             IMAGETYPE_PNG => ['imagepng','imagecreatefrompng'],
  12.                             ];
  13.                            
  14.     $options = ['maxwidth','maxheight','contain','cover'];
  15.     if (!in_array($option, $options)) {
  16.         return $logs[] = 'Option Not Defined: maxwidth | maxheight | content | cover. Action aborted';
  17.     }
  18.    
  19.     if ($option == 'contain' || $option == 'cover') {
  20.         if (!is_array($dimension) || count($dimension) < 2) {
  21.             return $logs[] = 'Dimensions Not Defined: [width, height]. Action aborted';
  22.         }
  23.     }
  24.    
  25.     list($source_image_width, $source_image_height, $source_image_type) = getimagesize($image);
  26.     $extension = pathinfo($image)['extension'];
  27.     if (
  28.             ($extension == 'image/gif' && $cource_image_type != IMAGETYPE_GIF) ||
  29.             ($extension == 'image/jpg' && $cource_image_type != IMAGETYPE_JPEG) ||
  30.             ($extension == 'image/png' && $cource_image_type != IMAGETYPE_PNG)
  31.             ) {
  32.                 return $logs[] = "File extension and file type mismatch. Action aborted";
  33.     }
  34.    
  35.     if (($option == 'maxwidth' && $source_image_width < $dimension[0])  || ($option == 'maxheight' && $source_image_height < $dimension[0])) {
  36.         return $logs[] = "Target file is larger than source file. Action aborted";
  37.     }
  38.    
  39.     if (pathinfo($image)['extension'] != pathinfo($thumb)['extension']) {
  40.         return $logs[] = "Target file extension mismatch. Action aborted";
  41.     }
  42.     $ratio = $source_image_height / $source_image_width;
  43.    
  44.     switch ($option) {
  45.         case 'maxwidth':
  46.             $thumbwidth = $dimension[0];
  47.             $thumbheight = $thumbwidth * $ratio;
  48.         break;
  49.         case 'maxheight':
  50.             $thumbheight = $dimension[0];
  51.             $thumbwidth = $thumbheight / $ratio;
  52.         break;
  53.         case 'contain':
  54.             $thumbwidth = $dimension[0];
  55.             $thumbheight = $thumbwidth * $ratio;
  56.             if ($thumbheight > $dimension[1]) {
  57.                 $thumbheight = $dimension[1];
  58.                 $thumbwidth = $thumbheight / $ratio;
  59.             }
  60.         break;
  61.         case 'cover':
  62.             if ($dimension[0] > $dimension[1]) {
  63.                 $thumbwidth = $dimension[0];
  64.                 $thumbheight = $thumbwidth * $ratio;
  65.             } else {
  66.                 $thumbheight = $dimension[1];
  67.                 $thumbwidth = $thumbheight / $ratio;
  68.             }
  69.         break;
  70.         default:
  71.             $thumbwidth = $dimension[0];           
  72.             $thumbheight = $thumbwidth * $ratio;
  73.         break;
  74.     }
  75.  
  76.     $temp_thumb = imagecreatetruecolor($thumbwidth, $thumbheight);
  77.     /* $methods indexes based on IMAGETYPE_XXX constants */
  78.  
  79.     if (!$temp_source = $methods[$source_image_type][1]($image)) { $logs[] = $methods[$source_image_type][1] . 'failure: ' . $image; }
  80.     switch ($source_image_type) {
  81.         case IMAGETYPE_GIF:
  82.         $transparency = imagecolorallocatealpha($temp_thumb, 255, 255, 255, 127);
  83.         imagefilledrectangle($temp_thumb, 0, 0, $thumbwidth, $thumbheight, $transparency);
  84.         break;
  85.         case IMAGETYPE_JPEG:
  86.         break;
  87.         case IMAGETYPE_PNG:
  88.         imagealphablending($temp_thumb, false);
  89.         imagesavealpha($temp_thumb,true);
  90.         $transparency = imagecolorallocatealpha($temp_thumb, 255, 255, 255, 127);
  91.         imagefilledrectangle($temp_thumb, 0, 0, $thumbwidth, $thumbheight, $transparency);
  92.         break;
  93.         default:
  94.         $logs[] = "Unknown type: {$image}";
  95.         break;
  96.     }
  97.    
  98.     imagecopyresampled($temp_thumb, $temp_source, 0, 0, 0, 0, $thumbwidth, $thumbheight, $source_image_width, $source_image_height);
  99.    
  100.     if ($option == 'cover') {
  101.         /* calculate offset-x and offset-y for center crop */
  102.         $x = (imagesx($temp_thumb) - $dimension[0]) / 2;
  103.         $y = (imagesy($temp_thumb) - $dimension[1]) / 2;
  104.         $x = ($x > 0)? $x : 0; /* probably don't need this */
  105.         $y = ($y > 0)? $y : 0; /* probably don't need this */
  106.         $temp_thumb = imagecrop($temp_thumb, ['x' => $x, 'y' => $y, 'width' => $dimension[0], 'height' => $dimension[1]]);
  107.     }
  108.  
  109.     if (!$methods[$source_image_type][0]($temp_thumb,$thumb, 9)) { $logs[] = "Unable to create thumb from temp image of ${image}"; }
  110.     if (!imagedestroy($temp_thumb)) { $log[] = "Unable to destroy temp file for {$image}"; }
  111.     imagedestroy($temp_source);
  112.     return (!empty($logs))? $logs : true;
  113. }
  114. ?>
  115. <!DOCTYPE html>
  116. <html>
  117.     <head>
  118.         <style>
  119.             .box {
  120.                 display: flex;
  121.                 align-items: center;
  122.                 justify-content: center;
  123.                 width: 200px;
  124.                 height: 400px;
  125.                 border: 1px solid purple;
  126.             }
  127.         </style>
  128.     </head>
  129.     <body>
  130. <?php
  131.     $source_files = ['test.gif','test.png','test.jpg'];
  132.     $dimension = [200, 400];
  133.     foreach ($source_files as $file) {
  134.         $option = 'cover';
  135.         $target_file = 'upload/'.$option.'_'.$file;
  136.         if ($result = resize('upload/' . $file, $target_file, $option, $dimension)) {
  137.             echo '<div class="box"><img src="'.$target_file.'"></div>';        
  138.         } else {
  139.             echo '<div class="box">'.implode($result).'</div>';
  140.         }
  141.     }
  142.     echo '<br>';
  143.     foreach ($source_files as $file) {
  144.         $option = 'contain';
  145.         $target_file = 'upload/'.$option.'_'.$file;
  146.         if ($result = resize('upload/' . $file, $target_file, $option, $dimension)) {
  147.             echo '<div class="box"><img src="'.$target_file.'"></div>';        
  148.         } else {
  149.             echo '<div class="box">'.implode($result).'</div>';
  150.         }
  151.     }
  152.     echo '<br>';
  153.     foreach ($source_files as $file) {
  154.         $option = 'maxwidth';
  155.         $target_file = 'upload/'.$option.'_'.$file;
  156.         if ($result = resize('upload/' . $file, $target_file, $option, [$dimension[0]])) {
  157.             echo '<div class="box"><img src="'.$target_file.'"></div>';        
  158.         } else {
  159.             echo '<div class="box">'.implode($result).'</div>';
  160.         }
  161.     }
  162.     echo '<br>';
  163.     foreach ($source_files as $file) {
  164.         $option = 'maxheight';
  165.         $target_file = 'upload/'.$option.'_'.$file;
  166.         if ($result = resize('upload/' . $file, $target_file, $option, [$dimension[1]])) {
  167.             echo '<div class="box"><img src="'.$target_file.'"></div>';        
  168.         } else {
  169.             echo '<div class="box">'.implode($result).'</div>';
  170.         }
  171.     }
  172. ?>
  173.     </body>
  174. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement