Thormar

sfljsdlfjsldkfjos

Jun 7th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.32 KB | None | 0 0
  1.         Class resize
  2.         {
  3.             // *** Class variables
  4.                     private $image;
  5.             private $width;
  6.             private $height;
  7.                     private $imageResized;
  8.                     private $log;
  9.  
  10.             function __construct($fileName)
  11.             {
  12.                             $this->log=new KLogger('/home/192.168.1.101/www/logs/', KLogger::INFO);
  13.                 // *** Open up the file
  14.                             $this->image = $this->openImage($fileName);
  15.                             $this->log->logInfo("FILE WAS OPENED:".$this->image);
  16.                 // *** Get width and height
  17.                 $this->width  = imagesx($this->image);
  18.                 $this->height = imagesy($this->image);
  19.                             $this->log->logInfo("DIMENSIONS[width:".$this->width.";height:".$this->height."]");
  20.                            
  21.             }
  22.  
  23.             ## --------------------------------------------------------
  24.  
  25.             private function openImage($file)
  26.             {
  27.                 // *** Get extension
  28.                 $extension = strtolower(strrchr($file, '.'));
  29.  
  30.                 switch($extension)
  31.                 {
  32.                     case '.jpg':
  33.                     case '.jpeg':
  34.                                         case '.JPG':
  35.                                         case '.JPEG':
  36.                         $img = @imagecreatefromjpeg($file);                                                
  37.                         break;
  38.                     case '.gif':
  39.                         $img = @imagecreatefromgif($file);
  40.                         break;
  41.                     case '.png':
  42.                         $img = @imagecreatefrompng($file);
  43.                         break;
  44.                     default:
  45.                         $img = false;
  46.                         break;
  47.                 }
  48.                 return $img;
  49.             }
  50.  
  51.             ## --------------------------------------------------------
  52.  
  53.             public function resizeImage($newWidth, $newHeight, $option="auto")
  54.             {
  55.                                
  56.                 // *** Get optimal width and height - based on $option
  57.                 $optionArray = $this->getDimensions($newWidth, $newHeight, $option);
  58.  
  59.                 $optimalWidth  = $optionArray['optimalWidth'];
  60.                 $optimalHeight = $optionArray['optimalHeight'];
  61.                                
  62.  
  63.                 // *** Resample - create image canvas of x, y size
  64.                 $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
  65.                 imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
  66.  
  67.  
  68.                 // *** if option is 'crop', then crop too
  69.                 if ($option == 'crop') {
  70.                     $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
  71.                 }                                
  72.             }
  73.  
  74.             ## --------------------------------------------------------
  75.            
  76.             private function getDimensions($newWidth, $newHeight, $option)
  77.             {
  78.  
  79.                switch ($option)
  80.                 {
  81.                     case 'exact':
  82.                         $optimalWidth = $newWidth;
  83.                         $optimalHeight= $newHeight;
  84.                         break;
  85.                     case 'portrait':
  86.                         $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  87.                         $optimalHeight= $newHeight;
  88.                         break;
  89.                     case 'landscape':
  90.                         $optimalWidth = $newWidth;
  91.                         $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  92.                         break;
  93.                     case 'auto':
  94.                         $optionArray = $this->getSizeByAuto($newWidth, $newHeight);
  95.                         $optimalWidth = $optionArray['optimalWidth'];
  96.                         $optimalHeight = $optionArray['optimalHeight'];
  97.                         break;
  98.                     case 'crop':
  99.                         $optionArray = $this->getOptimalCrop($newWidth, $newHeight);
  100.                         $optimalWidth = $optionArray['optimalWidth'];
  101.                         $optimalHeight = $optionArray['optimalHeight'];
  102.                         break;
  103.                 }
  104.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  105.             }
  106.  
  107.             ## --------------------------------------------------------
  108.  
  109.             private function getSizeByFixedHeight($newHeight)
  110.             {
  111.                 $ratio = $this->width / $this->height;
  112.                 $newWidth = $newHeight * $ratio;
  113.                 return $newWidth;
  114.             }
  115.  
  116.             private function getSizeByFixedWidth($newWidth)
  117.             {
  118.                 $ratio = $this->height / $this->width;
  119.                 $newHeight = $newWidth * $ratio;
  120.                 return $newHeight;
  121.             }
  122.  
  123.             private function getSizeByAuto($newWidth, $newHeight)
  124.             {
  125.                 if ($this->height < $this->width)
  126.                 // *** Image to be resized is wider (landscape)
  127.                 {
  128.                     $optimalWidth = $newWidth;
  129.                     $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  130.                 }
  131.                 elseif ($this->height > $this->width)
  132.                 // *** Image to be resized is taller (portrait)
  133.                 {
  134.                     $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  135.                     $optimalHeight= $newHeight;
  136.                 }
  137.                 else
  138.                 // *** Image to be resizerd is a square
  139.                 {
  140.                     if ($newHeight < $newWidth) {
  141.                         $optimalWidth = $newWidth;
  142.                         $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  143.                     } else if ($newHeight > $newWidth) {
  144.                         $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  145.                         $optimalHeight= $newHeight;
  146.                     } else {
  147.                         // *** Sqaure being resized to a square
  148.                         $optimalWidth = $newWidth;
  149.                         $optimalHeight= $newHeight;
  150.                     }
  151.                 }
  152.  
  153.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  154.             }
  155.  
  156.             ## --------------------------------------------------------
  157.  
  158.             private function getOptimalCrop($newWidth, $newHeight)
  159.             {
  160.  
  161.                 $heightRatio = $this->height / $newHeight;
  162.                 $widthRatio  = $this->width /  $newWidth;
  163.  
  164.                 if ($heightRatio < $widthRatio) {
  165.                     $optimalRatio = $heightRatio;
  166.                 } else {
  167.                     $optimalRatio = $widthRatio;
  168.                 }
  169.  
  170.                 $optimalHeight = $this->height / $optimalRatio;
  171.                 $optimalWidth  = $this->width  / $optimalRatio;
  172.  
  173.                 return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
  174.             }
  175.  
  176.             ## --------------------------------------------------------
  177.  
  178.             private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight)
  179.             {
  180.                 // *** Find center - this will be used for the crop
  181.                 $cropStartX = ( $optimalWidth / 2) - ( $newWidth /2 );
  182.                 $cropStartY = ( $optimalHeight/ 2) - ( $newHeight/2 );
  183.  
  184.                 $crop = $this->imageResized;
  185.                 //imagedestroy($this->imageResized);
  186.  
  187.                 // *** Now crop from center to exact requested size
  188.                 $this->imageResized = imagecreatetruecolor($newWidth , $newHeight);
  189.                 imagecopyresampled($this->imageResized, $crop , 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight , $newWidth, $newHeight);
  190.             }
  191.  
  192.             ## --------------------------------------------------------
  193.  
  194.             public function saveImage($savePath, $imageQuality="100")
  195.             {
  196.                 // *** Get extension
  197.                 $extension = strrchr($savePath, '.');
  198.                 $extension = strtolower($extension);
  199.  
  200.                 switch($extension)
  201.                 {
  202.                     case '.jpg':
  203.                     case '.jpeg':
  204.                                         case '.JPEG':
  205.                                         case '.JPG':
  206.                         if (imagetypes() & IMG_JPG) {
  207.                             imagejpeg($this->imageResized, $savePath, $imageQuality);
  208.                         }
  209.                         break;
  210.  
  211.                     case '.gif':
  212.                         if (imagetypes() & IMG_GIF) {
  213.                             imagegif($this->imageResized, $savePath);
  214.                         }
  215.                         break;
  216.  
  217.                     case '.png':
  218.                         // *** Scale quality from 0-100 to 0-9
  219.                         $scaleQuality = round(($imageQuality/100) * 9);
  220.  
  221.                         // *** Invert quality setting as 0 is best, not 9
  222.                         $invertScaleQuality = 9 - $scaleQuality;
  223.  
  224.                         if (imagetypes() & IMG_PNG) {
  225.                              imagepng($this->imageResized, $savePath, $invertScaleQuality);
  226.                         }
  227.                         break;
  228.  
  229.                     // ... etc
  230.  
  231.                     default:
  232.                         // *** No extension - No save.
  233.                         break;
  234.                 }
  235.  
  236.                 imagedestroy($this->imageResized);
  237.             }
  238.  
  239.  
  240.             ## --------------------------------------------------------
  241.  
  242.         }
Advertisement
Add Comment
Please, Sign In to add comment