Ver Mensaje Individual
  #5 (permalink)  
Antiguo 05/07/2012, 15:54
Avatar de Nemutagk
Nemutagk
Colaborador
 
Fecha de Ingreso: marzo-2004
Ubicación: México
Mensajes: 2.633
Antigüedad: 20 años, 10 meses
Puntos: 406
Respuesta: Redimensionar imagen PHP

mmm puedes hacer algo así...

Código PHP:
Ver original
  1. $archivo = $_POST['imagen'];
  2.             $info = explode('/',$archivo);
  3.             $nombre = $info[count($info)-1];
  4.             $path = 'imagenes/';
  5.             $file = $path.$nombre;
  6.  
  7.             $maxHeight = isset($_POST['maxheight']) ? $_POST['maxheight'] : 400;
  8.             $maxWidth = isset($_POST['maxwidth']) ? $_POST['maxwidth'] : 400;
  9.  
  10.             $contenido = file_get_contents($archivo);
  11.  
  12.             if (file_put_contents($file,$contenido) !== false) {
  13.                 list($ancho,$alto,$tipo,$atributos) = getimagesize($file);
  14.                  
  15.                 $imagen = '';
  16.  
  17.                 if (!file_exists($file)) {
  18.                     die('El archivo no existe');
  19.                 }
  20.  
  21.                 switch($tipo) {
  22.                     case IMAGETYPE_GIF:
  23.                     $imagen = imagecreatefromgif($file);
  24.                     echo '<p>crear imagen desde gif</p>';
  25.                     break;
  26.                     case IMAGETYPE_JPEG:
  27.                     $imagen = imagecreatefromjpeg($file);
  28.                     echo '<p>crear imagen desde jpeg</p>';
  29.                     break;
  30.                     case IMAGETYPE_PNG:
  31.                     $imagen = imagecreatefrompng($file);
  32.                     echo '<p>crear imagen desde png</p>';
  33.                     break;
  34.                 }
  35.  
  36.                 if (empty($imagen)) {
  37.                     die('no se creo la imagen');
  38.                 }
  39.  
  40.                 $ratio = $ancho/$alto;
  41.  
  42.                 if (($maxWidth / $maxHeight) < $ratio) {
  43.                     $newWidth = ceil($maxHeight * $ratio);
  44.                     $newHeight = $maxHeight;
  45.                 }else {
  46.                     $newWidth = $maxWidth;
  47.                     $newHeight = ceil($maxWidth * $ratio);
  48.                 }
  49.  
  50.                 if ($newWidth != 0 && $newHeight != 0) {
  51.                     $newImg = imagecreatetruecolor($newWidth, $newHeight);
  52.                     echo '<p>Porcentaje: '.$por.' - Ancho: '.$newWidth.' - Alto: '.$newHeight.'</p>';
  53.                     imagecopyresampled($newImg, $imagen, 0, 0, 0, 0, $newWidth, $newHeight, $ancho, $alto);
  54.                     $nombre = substr($nombre, 0, strpos($nombre,'.')).'.jpg';
  55.  
  56.                     switch($tipo) {
  57.                         case IMAGETYPE_GIF:
  58.                         imagegif($newImg,$path.$nombre);
  59.                         break;
  60.                         case IMAGETYPE_JPEG:
  61.                         imagejpeg($newImg,$path.$nombre,80);
  62.                         break;
  63.                         case IMAGETYPE_PNG:
  64.                         imagepng($newImg,$path.$nombre);
  65.                         break;
  66.                     }
  67.  
  68.                     imagedestroy($newImg);
  69.  
  70.                     echo '<p><img src="'.$path.$nombre.'" /></p>';
  71.                 }else {
  72.                     echo 'el tama&ntilde;o es impreciso';
  73.                 }
  74.             }else {
  75.                  echo 'No se pudo crear la imagen';
  76.             }
__________________
Listo?, tendría que tener 60 puntos menos de IQ para considerarme listo!!!
-- Sheldon Cooper
http://twitter.com/nemutagk
PD: No contestaré temas vía mensaje personal =)