Advertisement
Speeedfire

Untitled

Jan 17th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.76 KB | None | 0 0
  1. /**
  2.      * Create thumnbnail and return it's URL on success
  3.      *
  4.      * @param  string  $path  file path
  5.      * @param  string  $mime  file mime type
  6.      * @return string|false
  7.      * @author Dmitry (dio) Levashov
  8.      **/
  9.     protected function createTmb($path, $stat) {
  10.         if (!$stat || !$this->canCreateTmb($path, $stat)) {
  11.             return false;
  12.         }
  13.  
  14.         $name = $this->tmbname($stat);
  15.         $tmb  = $this->tmbPath.DIRECTORY_SEPARATOR.$name;
  16.  
  17.         // copy image into tmbPath so some drivers does not store files on local fs
  18.         if (($src = $this->_fopen($path, 'rb')) == false) {
  19.             return false;
  20.         }
  21.  
  22.         if (($trg = fopen($tmb, 'wb')) == false) {
  23.             $this->_fclose($src, $path);
  24.             return false;
  25.         }
  26.  
  27.         while (!feof($src)) {
  28.             fwrite($trg, fread($src, 8192));
  29.         }
  30.  
  31.         $this->_fclose($src, $path);
  32.         fclose($trg);
  33.  
  34.         $result = false;
  35.        
  36.         $tmbSize = $this->tmbSize;
  37.                 $tmbHeight = $this->tmbHeight;
  38.        
  39.         if (($s = getimagesize($tmb)) == false) {
  40.             return false;
  41.         }
  42.    
  43.         /* If image smaller or equal thumbnail size - just fitting to thumbnail square */
  44.         if ($s[0] <= $tmbSize && $s[1]  <= $tmbHeight) {
  45.            $result = $this->imgResize($tmb, $s[0], $s[1], 'center', 'middle', $this->options['tmbBgColor']);
  46.  
  47.         } else {
  48.  
  49.             if ($this->options['tmbCrop']) {
  50.        
  51.                 /* Resize and crop if image bigger than thumbnail */
  52.                 if (!(($s[0] > $tmbSize && $s[1] <= $tmbHeight) || ($s[0] <= $tmbSize && $s[1] > $tmbHeight) ) || ($s[0] > $tmbSize && $s[1] > $tmbHeight)) {
  53.                     $result = $this->imgResize($tmb, $tmbSize, $tmbHeight, true, false);
  54.                 }
  55.  
  56.                 if (($s = getimagesize($tmb)) != false) {
  57.                     $x = $s[0] > $tmbSize ? intval(($s[0] - $tmbSize)/2) : 0;
  58.                     $y = $s[1] > $tmbHeight ? intval(($s[1] - $tmbHeight)/2) : 0;
  59.                     $result = $this->imgCrop($tmb, $tmbSize, $tmbHeight, $x, $y);
  60.                 }
  61.  
  62.             } else {
  63.                 $result = $this->imgResize($tmb, $tmbSize, $tmbHeight, true, true, $this->imgLib);
  64.                 //$result = $this->imgSquareFit($tmb, $tmbSize, $tmbHeight, 'center', 'middle', $this->options['tmbBgColor'] );
  65.             }
  66.  
  67.         }
  68.         if (!$result) {
  69.             unlink($tmb);
  70.             return false;
  71.         }
  72.  
  73.         return $name;
  74.     }
  75.    
  76.     /**
  77.      * Resize image
  78.      *
  79.      * @param  string   $path               image file
  80.      * @param  int      $width              new width
  81.      * @param  int      $height             new height
  82.      * @param  bool     $keepProportions    crop image
  83.      * @param  bool     $resizeByBiggerSide resize image based on bigger side if true
  84.      * @param  string   $destformat         image destination format
  85.      * @return string|false
  86.      * @author Dmitry (dio) Levashov
  87.      * @author Alexey Sukhotin
  88.      **/
  89.     protected function imgResize($path, $width, $height, $keepProportions = false, $resizeByBiggerSide = true, $destformat = null) {
  90.         if (($s = @getimagesize($path)) == false) {
  91.             return false;
  92.         }
  93.  
  94.         $result = false;
  95.        
  96.         list($size_w, $size_h) = array($width, $height);
  97.    
  98.         if ($keepProportions == true) {
  99.            
  100.             list($orig_w, $orig_h, $new_w, $new_h) = array($s[0], $s[1], $width, $height);
  101.        
  102.             /* Calculating image scale width and height */
  103.             $xscale = $orig_w / $new_w;
  104.             $yscale = $orig_h / $new_h;
  105.  
  106.             /* Resizing by biggest side */
  107.  
  108.             if ($resizeByBiggerSide) {
  109.  
  110.                 if ($orig_w > $orig_h) {
  111.                     $size_h = $orig_h * $width / $orig_w;
  112.                     $size_w = $width;
  113.                 } else {
  114.                     $size_w = $orig_w * $height / $orig_h;
  115.                     $size_h = $height;
  116.                 }
  117.      
  118.             } else {
  119.                 if ($orig_w > $orig_h) {
  120.                     $size_w = $orig_w * $height / $orig_h;
  121.                     $size_h = $height;
  122.                 } else {
  123.                     $size_h = $orig_h * $width / $orig_w;
  124.                     $size_w = $width;
  125.                 }
  126.             }
  127.         }
  128.  
  129.         switch ($this->imgLib) {
  130.             case 'imagick':
  131.                
  132.                 try {
  133.                     $img = new imagick($path);
  134.                 } catch (Exception $e) {
  135.  
  136.                     return false;
  137.                 }
  138.  
  139.                 $img->resizeImage($size_w, $size_h, Imagick::FILTER_LANCZOS, true);
  140.                    
  141.                 $result = $img->writeImage($path);
  142.  
  143.                 return $result ? $path : false;
  144.  
  145.                 break;
  146.  
  147.             case 'gd':
  148.                 if ($s['mime'] == 'image/jpeg') {
  149.                     $img = imagecreatefromjpeg($path);
  150.                 } elseif ($s['mime'] == 'image/png') {
  151.                     $img = imagecreatefrompng($path);
  152.                 } elseif ($s['mime'] == 'image/gif') {
  153.                     $img = imagecreatefromgif($path);
  154.                 } elseif ($s['mime'] == 'image/xbm') {
  155.                     $img = imagecreatefromxbm($path);
  156.                 }
  157.  
  158.                 if ($img &&  false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {
  159.                     if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
  160.                             return false;
  161.                     }
  162.        
  163.                     if ($destformat == 'jpg'  || ($destformat == null && $s['mime'] == 'image/jpeg')) {
  164.                         $result = imagejpeg($tmp, $path, 100);
  165.                     } else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
  166.                         $result = imagegif($tmp, $path, 7);
  167.                     } else {
  168.                         $result = imagepng($tmp, $path, 7);
  169.                     }
  170.  
  171.                     imagedestroy($img);
  172.                     imagedestroy($tmp);
  173.  
  174.                     return $result ? $path : false;
  175.  
  176.                 }
  177.                 break;
  178.         }
  179.        
  180.         return false;
  181.     }
  182.  
  183.     /**
  184.      * Crop image
  185.      *
  186.      * @param  string   $path               image file
  187.      * @param  int      $width              crop width
  188.      * @param  int      $height             crop height
  189.      * @param  bool     $x                  crop left offset
  190.      * @param  bool     $y                  crop top offset
  191.      * @param  string   $destformat         image destination format
  192.      * @return string|false
  193.      * @author Dmitry (dio) Levashov
  194.      * @author Alexey Sukhotin
  195.      **/
  196.     protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
  197.         if (($s = @getimagesize($path)) == false) {
  198.             return false;
  199.         }
  200.  
  201.         $result = false;
  202.        
  203.         switch ($this->imgLib) {
  204.             case 'imagick':
  205.                
  206.                 try {
  207.                     $img = new imagick($path);
  208.                 } catch (Exception $e) {
  209.  
  210.                     return false;
  211.                 }
  212.  
  213.                 $img->cropImage($width, $height, $x, $y);
  214.  
  215.                 $result = $img->writeImage($path);
  216.  
  217.                 return $result ? $path : false;
  218.  
  219.                 break;
  220.  
  221.             case 'gd':
  222.                 if ($s['mime'] == 'image/jpeg') {
  223.                     $img = imagecreatefromjpeg($path);
  224.                 } elseif ($s['mime'] == 'image/png') {
  225.                     $img = imagecreatefrompng($path);
  226.                 } elseif ($s['mime'] == 'image/gif') {
  227.                     $img = imagecreatefromgif($path);
  228.                 } elseif ($s['mime'] == 'image/xbm') {
  229.                     $img = imagecreatefromxbm($path);
  230.                 }
  231.  
  232.                 if ($img &&  false != ($tmp = imagecreatetruecolor($width, $height))) {
  233.  
  234.                     if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
  235.                         return false;
  236.                     }
  237.                    
  238.                     if ($destformat == 'jpg'  || ($destformat == null && $s['mime'] == 'image/jpeg')) {
  239.                         $result = imagejpeg($tmp, $path, 100);
  240.                     } else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
  241.                         $result = imagegif($tmp, $path, 7);
  242.                     } else {
  243.                         $result = imagepng($tmp, $path, 7);
  244.                     }
  245.  
  246.                     imagedestroy($img);
  247.                     imagedestroy($tmp);
  248.  
  249.                     return $result ? $path : false;
  250.  
  251.                 }
  252.                 break;
  253.         }
  254.  
  255.         return false;
  256.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement