Ver Mensaje Individual
  #3 (permalink)  
Antiguo 25/07/2012, 04:59
Avatar de NnikoO
NnikoO
 
Fecha de Ingreso: agosto-2008
Ubicación: Rosario
Mensajes: 245
Antigüedad: 15 años, 8 meses
Puntos: 0
Respuesta: Problema con clase resize-crop!

Si mil disculpas, es que lo postie en el trabajo cuando apenas pude tener tiempo libre, muy infimo jaja.

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

Última edición por webosiris; 26/07/2012 a las 00:15