Advertisement
michaelyuen

Untitled

Apr 16th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.02 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors',1); // enable php error display for easy trouble shooting
  3. error_reporting(E_ALL); // set error display to all
  4.  
  5. /* 
  6.         this function intends to reduce the size of image and not for enlargement
  7.         action will be aborted if target file is larger the source
  8.         tested with PHP Version 7.0.13
  9. */
  10.     class Image {
  11.        
  12.         private $_logs = null,
  13.                         $_quality = [IMAGETYPE_GIF => 0, IMAGETYPE_JPEG => 100, IMAGETYPE_PNG => 9],
  14.                         $_methods = [
  15.                             IMAGETYPE_GIF => ['imagegif','imagecreatefromgif'],
  16.                             IMAGETYPE_JPEG => ['imagejpeg','imagecreatefromjpeg'],
  17.                             IMAGETYPE_PNG => ['imagepng','imagecreatefrompng'],
  18.                         ];
  19.                        
  20.         private function _checkType() {
  21.             if (
  22.                     ($this->_extension == 'image/gif' && $this->_sourceType != IMAGETYPE_GIF) ||
  23.                     ($this->_extension == 'image/jpg' && $this->_sourceType != IMAGETYPE_JPEG) ||
  24.                     ($this->_extension == 'image/png' && $this->_sourceType != IMAGETYPE_PNG)
  25.                     ) {
  26.                 $this->_logs[] = "File extension and file type mismatch. Action aborted";
  27.                 return false;
  28.             }
  29.             return true;
  30.         }
  31.        
  32.         private function _load($image) {
  33.             $this->_sourceFile = $image;
  34.             $this->_extension = pathinfo($image)['extension'];
  35.             list($this->_sourceWidth, $this->_sourceHeight, $this->_sourceType) = getimagesize($image);
  36.             $this->_sourceImage = $this->_methods[$this->_sourceType][1]($image);
  37.         }
  38.        
  39.         private function _createTarget($width, $height) {
  40.             $this->_targetImage = imagecreatetruecolor($width, $height);
  41.             return true;
  42.         }
  43.        
  44.         private function _output($imageData, $targetPath) {
  45.             imagesavealpha($imageData, true);
  46.             if ($this->_sourceType === IMAGETYPE_GIF || $this->_sourceType === IMAGETYPE_PNG) {
  47.                 imagecolortransparent($imageData, $this->_targetTransparency);
  48.             }
  49.            
  50.             if (!$this->_methods[$this->_sourceType][0]($imageData,$targetPath, $this->_quality[$this->_sourceType])) { "Output failure for {$this->_sourceFile}"; }
  51.             if (!imagedestroy($this->_sourceImage)) { $this->_logs[] = "Unable to destroy temp data for {$this->_sourceFile}"; }
  52.             if (isset($this->_targetImage)) {
  53.                 if (!imagedestroy($this->_targetImage)) { $this->_logs[] = "Unable to destroy temp target for {$this->_sourceFile}"; }             
  54.             }
  55.             return (!empty($this->_logs))? $this->_logs : true;
  56.         }
  57.        
  58.         public function resize($image, $thumb, $option = 'maxwidth', $dimension = array('200')) {
  59.                        
  60.             /* validation 1 */
  61.             $options = ['maxwidth','maxheight','contain','cover'];
  62.             if (!in_array($option, $options)) {
  63.                 return $this->_logs[] = 'Option Not Defined: maxwidth | maxheight | content | cover. Action aborted';
  64.             }
  65.            
  66.             /* validation 2 */
  67.             if ($option == 'contain' || $option == 'cover') {
  68.                 if (!is_array($dimension) || count($dimension) < 2) {
  69.                     return $this->_logs[] = 'Dimensions Not Defined: [width, height]. Action aborted';
  70.                 }
  71.             }
  72.            
  73.             $this->_load($image);
  74.            
  75.             if (!$this->_checkType()) {
  76.                 return false;
  77.             }
  78.            
  79.             /* validation */           
  80.             if (($option == 'maxwidth' && $this->_sourceWidth < $dimension[0])  || ($option == 'maxheight' && $this->_sourceHeight < $dimension[0])) {
  81.                 $this->_logs[] = "Target file is larger than source file. Action aborted";
  82.             }
  83.            
  84.             /* validation */
  85.             if (pathinfo($image)['extension'] != pathinfo($thumb)['extension']) {
  86.                 $this->_logs[] = "Target file extension mismatch. Action aborted";
  87.             }
  88.            
  89.             /* get aspect ratio from source file */
  90.             $ratio = $this->_sourceHeight / $this->_sourceWidth;
  91.            
  92.             switch ($option) {
  93.                 case 'maxwidth':
  94.                     $thumbwidth = $dimension[0];
  95.                     $thumbheight = $thumbwidth * $ratio;
  96.                 break;
  97.                 case 'maxheight':
  98.                     $thumbheight = $dimension[0];
  99.                     $thumbwidth = $thumbheight / $ratio;
  100.                 break;
  101.                 case 'contain':
  102.                     $thumbwidth = $dimension[0];
  103.                     $thumbheight = $thumbwidth * $ratio;
  104.                     if ($thumbheight > $dimension[1]) {
  105.                         $thumbheight = $dimension[1];
  106.                         $thumbwidth = $thumbheight / $ratio;
  107.                     }
  108.                 break;
  109.                 case 'cover':
  110.                     if ($dimension[0] > $dimension[1]) {
  111.                         $thumbwidth = $dimension[0];
  112.                         $thumbheight = $thumbwidth * $ratio;
  113.                     } else {
  114.                         $thumbheight = $dimension[1];
  115.                         $thumbwidth = $thumbheight / $ratio;
  116.                     }
  117.                 break;
  118.                 default:
  119.                     $thumbwidth = $dimension[0];           
  120.                     $thumbheight = $thumbwidth * $ratio;
  121.                 break;
  122.             }
  123.  
  124.             /* create new instant */
  125.             $this->_createTarget($thumbwidth, $thumbheight);
  126.            
  127.             /* $methods indexes based on IMAGETYPE_XXX constants */
  128.             switch ($this->_sourceType) {
  129.                 case IMAGETYPE_GIF:
  130.                     $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0x7f, 0x7f, 0x7f, 0);
  131.                     imagefill($this->_targetImage, 0, 0, $this->_targetTransparency);
  132.                     imagecolortransparent($this->_targetImage, $this->_targetTransparency);
  133.                     break;
  134.                     case IMAGETYPE_JPEG:
  135.                     $quality = 100;
  136.                 break;
  137.                 case IMAGETYPE_PNG:
  138.                     imagealphablending($this->_targetImage, false);
  139.                     imagesavealpha($this->_targetImage,true);
  140.                     $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0, 0, 0, 127);
  141.                     // $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 255, 255, 255, 127);
  142.                     imagefilledrectangle($this->_targetImage, 0, 0, $thumbwidth, $thumbheight, $this->_targetTransparency);
  143.                 break;
  144.                 default:
  145.                     $this->_logs[] = "Unknown type: {$image}";
  146.                 break;
  147.             }
  148.            
  149.             /* create $temp_thumb image */
  150.             imagecopyresampled($this->_targetImage, $this->_sourceImage, 0, 0, 0, 0, $thumbwidth, $thumbheight, $this->_sourceWidth, $this->_sourceHeight);
  151.            
  152.             if ($option == 'cover') {
  153.                 /* calculate offset-x and offset-y for center crop */
  154.                 $x = (imagesx($this->_targetImage) - $dimension[0]) / 2;
  155.                 $y = (imagesy($this->_targetImage) - $dimension[1]) / 2;
  156.                 $x = ($x > 0)? $x : 0; /* probably don't need this */
  157.                 $y = ($y > 0)? $y : 0; /* probably don't need this */
  158.                 $this->_targetImage = imagecrop($this->_targetImage, ['x' => $x, 'y' => $y, 'width' => $dimension[0], 'height' => $dimension[1]]);
  159.             }
  160.            
  161.             $this->_output($this->_targetImage, $thumb);
  162.             return (!empty($this->_logs))? $this->_logs : true;
  163.         }
  164.        
  165.         public function flip ($image, $targetpath, $mode = IMG_FLIP_HORIZONTAL) {
  166.             /* posible $mode = IMG_FLIP_HORIZONTAL | IMG_FLIP_VERTICAL | IMG_FLIP_BOTH */
  167.             $this->_load($image);
  168.             if (!$this->_checkType()) {
  169.                 return false;
  170.             }
  171.            
  172.             imageflip($this->_sourceImage, $mode);
  173.            
  174.             $this->_createTarget($this->_sourceWidth, $this->_sourceHeight);
  175.  
  176.             switch ($this->_sourceType) {
  177.                 case IMAGETYPE_GIF:
  178.                     $this->_targetTransparency = imagecolorallocatealpha($this->_targetImage, 0x7f, 0x7f, 0x7f, 0);
  179.                     imagefill($this->_targetImage, 0, 0, $this->_targetTransparency);
  180.                 break;
  181.                 case IMAGETYPE_JPEG:
  182.                 break;
  183.                 case IMAGETYPE_PNG:
  184.  
  185.                 break;
  186.                 default:
  187.                     $this->_logs[] = "Unknown type: {$image}";
  188.                 break;
  189.             }
  190.             imagecopyresampled($this->_targetImage, $this->_sourceImage, 0, 0, 0, 0, $this->_sourceWidth, $this->_sourceHeight, $this->_sourceWidth, $this->_sourceHeight);
  191.             $this->_output($this->_sourceImage, $targetpath);
  192.             return (!empty($this->_logs))? $this->_logs : true;
  193.         }
  194.    
  195.         public function logs() {
  196.             return $this->_logs;
  197.         }
  198.     }
  199. ?>
  200. <!DOCTYPE html>
  201. <html>
  202.     <head>
  203.         <style>
  204.             .box {
  205.                 display: flex;
  206.                 align-items: center;
  207.                 justify-content: center;
  208.                 width: 200px;
  209.                 height: 400px;
  210.                 border: 1px solid purple;
  211.             }
  212.         </style>
  213.     </head>
  214.     <body>
  215. <?php
  216.     $image = new Image;
  217.     $source_files = ['test.gif','test.png','test.jpg'];
  218.    
  219.     foreach ($source_files as $file) {
  220.         $source = 'upload/'.$file;
  221.         $target = 'upload/resize1_'.$file;
  222.         $image->resize($source,$target, 'maxwidth', [400]);
  223.         echo '<div style="background-color: red;"><img src="'.$target.'"></div>';
  224.     }
  225.  
  226.     foreach ($source_files as $file) {
  227.         $source = 'upload/'.$file;
  228.         $target = 'upload/flip1_'.$file;
  229.         $image->flip($source,$target, IMG_FLIP_HORIZONTAL);
  230.         echo '<div style="background-color: red;"><img src="'.$target.'"></div>';
  231.     }
  232.  
  233. ?>
  234.     </body>
  235. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement