Guest User

Untitled

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