Foros del Web » Programando para Internet » PHP »

Problema con clase resize-crop!

Estas en el tema de Problema con clase resize-crop! en el foro de PHP en Foros del Web. Hola gente! Estoy usando una clase PHP para hacer resize y crop de unas imagenes, sacando dos copias de diferentes tamaños. Con una imagen anda ...
  #1 (permalink)  
Antiguo 24/07/2012, 07:54
Avatar de NnikoO  
Fecha de Ingreso: agosto-2008
Ubicación: Rosario
Mensajes: 245
Antigüedad: 15 años, 7 meses
Puntos: 0
Pregunta Problema con clase resize-crop!

Hola gente!
Estoy usando una clase PHP para hacer resize y crop de unas imagenes, sacando dos copias de diferentes tamaños. Con una imagen anda de maravillas.
El tema es que la aplicacion permite cargar hasta de 10 fotos.

Probando con dos imagenes, la primera siempre anda de maravilla, pero la segunda ya no funciona, la imagen original no se redimensiona, y las dos copias de diferentes de tamaños las genera, pero son dos imagenes completamente negras.

Primero cargo las fotos, y guardo las rutas en la base de datos, luego con un header paso por la url las variables con el directorio donde estan almacenadas y los nombres de cada una. (Esto va a un archivo llamada crop.php)

En crop se realiza la redimension si la imagen original sobrepasa una sierta resolucion, y se generan las dos copias. Y se redirecciona hasta la pagina principal.

Repito, con una imagen anda de maravillas, pero ya con dos, trae problemas.

Espero que alguien pueda darme una mano.
Gracias!
  #2 (permalink)  
Antiguo 24/07/2012, 11:21
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Problema con clase resize-crop!

Con un código podríamos ayudarte de maravillas, sin ver el código que usas, trae problemas.
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 25/07/2012, 04:59
Avatar de NnikoO  
Fecha de Ingreso: agosto-2008
Ubicación: Rosario
Mensajes: 245
Antigüedad: 15 años, 7 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
  #4 (permalink)  
Antiguo 26/07/2012, 04:50
Avatar de NnikoO  
Fecha de Ingreso: agosto-2008
Ubicación: Rosario
Mensajes: 245
Antigüedad: 15 años, 7 meses
Puntos: 0
Respuesta: Problema con clase resize-crop!

Y este el archivo que llama a la clase y hace los resize que cada imágen. Por get paso el directorio donde se encuentra la foto, y el nombre de la imagen:

Código PHP:
Ver original
  1. <?php
  2.    
  3.     $directorio = $_GET['directorio'];
  4.     $imagen     = $_GET['foto'];
  5.    
  6.    
  7.     include("resize-class.php");
  8.    
  9.     //FOTO 01
  10.     $resizeObj = new resize($directorio . "/" . $imagen);  
  11.        
  12.     //Redimensiona para SLIDE
  13.     $resizeObj -> resizeImage(368, 288, 'crop');
  14.     $resizeObj -> saveImage($directorio . "/" . "slide/slide_" . $imagen, 100);
  15.    
  16.     //Redimensiona para LISTADO
  17.     $resizeObj -> resizeImage(198, 198, 'crop');
  18.     $resizeObj -> saveImage($directorio . "/" . "listado/listado_" . $imagen, 100);
  19.    
  20.     //Redimensiona original si es mayor a 800 x 600
  21.     $dimension = getimagesize($directorio . "/" . $imagen);
  22.     if($dimension[0] > 800 OR $dimension[1] > 600){
  23.        
  24.         $resizeObj -> resizeImage(800, 600, 'auto');
  25.    
  26.         $resizeObj -> saveImage($directorio . "/" . $imagen, 100);
  27.    
  28.     }
  29.    
  30.  
  31.         header("Location: ../inicio.php");
  32.     exit;
  33.        
  34.    
  35. ?>
  #5 (permalink)  
Antiguo 26/07/2012, 08:22
 
Fecha de Ingreso: abril-2012
Mensajes: 590
Antigüedad: 12 años
Puntos: 58
Respuesta: Problema con clase resize-crop!

A mi también me daba problemas al redimensionar varias imágenes y tras mucho romperme la cabeza conseguí solucionarlo realizando la instalación de imagick con un parámetro que desactiva las advertencias.

Lo que pasaba era que al hacer mucho procesamiento imagick me sacaba un mensaje de advertencia y se cortaba el proceso o me hacía cosas raras con las imagenes (negras, deformes)...

Ahora no recuerdo dicho parámetro pero si lo encuentro lo pongo por aquí.

Etiquetas: crop, fotos, imagenes, redimension, resize
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:18.