Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/01/2009, 15:40
Avatar de ElJavista
ElJavista
Colaborador
 
Fecha de Ingreso: marzo-2007
Ubicación: Lima Perú
Mensajes: 2.231
Antigüedad: 19 años, 6 meses
Puntos: 67
Respuesta: redimensionar imagenes con php

Mira, si solo quieres modificar el tamaño por HTML eso no es nada eficiente porque si es una imagen muy grande igual se va a demorar mucho en cargar. Aquí tienes una clase para cambiar el tamaño de una imagen. Puedes cambiar una de sus dimensiones, ancho o alto y automáticamente la otra dimensión queda proporcionada, también puedes definir unas dimensiones máxmas, también aparecerá la imágen de manera proporcional, otra opcion es cortarla en dimensiones específicas.

Código PHP:
<?php
    
class Imagen {
          var 
$entrada = "";
          var 
$width = 0;
          var 
$height = 0;
          var 
$tipo = 0;

          function 
Imagen($ent) {
                   
$this->entrada = $ent;
                   list(
$wid, $hei, $tip) = getimagesize($ent);
                   
$this->width = $wid;
                   
$this->height = $hei;
                   
$this->tipo = $tip;
          }

          function 
getThumb($w, $h, $salida, $opc = false) {
                   
$ent = $this->entrada;
                   
$tip = $this->tipo;
                   
$wid = $this->width;
                   
$hei = $this->height;

                   if (
$w) {
                      
$newwidth = $w;
                      
$newheight = round(($hei / $wid) * $newwidth);
                   }  elseif (
$h) {
                      
$newheight = $h;
                      
$newwidth = round(($wid / $hei) * $newheight);
                   }
                   
// Cargar la imagen
                   
$thumb = imagecreatetruecolor($newwidth, $newheight);
                   if (
$tip == 1) {
                      
$source = imagecreatefromgif($ent);
                   }  elseif (
$tip == 2) {
                      
$source = imagecreatefromjpeg($ent);
                   }  elseif (
$tip == 3) {
                      
$source = imagecreatefrompng($ent);
                   }

                   
// Redimensionar
                   
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $wid, $hei);

                   
// Mostrar la nueva imagen
                   
if ($opc) return $thumb;
                   else return 
imagejpeg($thumb, $salida);
          }

          function 
getNothumb($w, $h, $salida, $opc = false) {
                   
$ent = $this->entrada;
                   
$tip = $this->tipo;
                   
$wid = $this->width;
                   
$hei = $this->height;

                   if (
$h) {
                      
$newheight = $h;
                      
$newwidth = round(($wid / $hei) * $newheight);
                   }  else if (
$w) {
                      
$newwidth = $w;
                      
$newheight = round(($hei / $wid) * $newwidth);
                   }
                   
// Cargar la imagen
                   
$thumb = imagecreatetruecolor($newwidth, $newheight);
                   if (
$tip == 1) {
                      
$source = imagecreatefromgif($ent);
                   }  elseif (
$tip == 2) {
                      
$source = imagecreatefromjpeg($ent);
                   }  elseif (
$tip == 3) {
                      
$source = imagecreatefrompng($ent);
                   }

                   
// Redimensionar
                   
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $wid, $hei);

                   
// Mostrar la nueva imagen
                   
if ($opc) return $thumb;
                   else return 
imagejpeg($thumb, $salida);
          }

          function 
getCorte($we, $he, $salida, $opc = false) {
                   
$img = $this->entrada;
                   
$wid = $this->width;
                   
$hei = $this->height;
                   
$tip = $this->tipo;

                   if (
$wid / $we > $hei / $he) {
                      
$hp = $hei;
                      
$wp = round(($we / $he) * $hp);
                   }  else {
                      
$wp = $wid;
                      
$hp = round(($he / $we) * $wp);
                   }

                   
$lp = round(($wid - $wp) / 2);
                   
$tp = round(($hei - $hp) / 2);

                   
$thumb = imagecreatetruecolor($we, $he);
                   if (
$tip == 1) {
                     
$source = imagecreatefromgif($img);
                    }  elseif (
$tip == 2) {
                     
$source = imagecreatefromjpeg($img);
                   }  elseif (
$tip == 3) {
                     
$source = imagecreatefrompng($img);
                   }

                   
imagecopyresampled($thumb, $source, 0, 0, $lp, $tp, $we, $he, $wp, $hp);

                   if (
$opc) return $thumb;
                   else return 
imagejpeg($thumb, $salida);
          }
    }

    if (isset(
$_GET["img"])) {
       
$img = new Imagen($_GET["img"]);
       
$tip = $_GET["tip"];
       
$w = $_GET["w"];
       
$h = $_GET["h"];

       
header("Content-type: image/jpeg");
       if (
$tip == 0) {
          
imagejpeg($img->getThumb($w, $h, "", true));
       }  elseif (
$tip == 1) {
          
imagejpeg($img->getNothumb($w, $h, "", true));
       }  elseif (
$tip == 2) {
          
imagejpeg($img->getCorte($w, $h, "", true));
       }
    }
?>
Este es un ejemplo de su uso: digamos yo tengo una imagen llamada "imagen.jpg", entonces hago lo siguiente:

Código PHP:
include("class.imagen.php");
$img = new Imgen("imagen.jpg");
if (
$img->getThumb(100, 100, "nuevaimgen.jpg")) echo "se generó la nueva imagen"; 
Si usas el método getNothumb entonces defines el ancho y alto máximo que puede tener la imagen y si usas el método getCorte entonces creas un corte de la imagen. Eso es interesante, porque digamos tienes una imagen de 600 x 800 y creas un corte de 100 x 100 no vas a crear un cuadro de 100 x 100 de la imagen original sino en realidad una reducción del corte de 600 x 600 de la imagen original, o sea, trata de abarcar la mayor área posible.

Bien, este archivo también se puede usar de la siguiente manera:

<img src="class.imagen.php?img=imagen.jpg&tip=2&w=100&h =100" />

Esto insertará una imagen de corte de 100 x 100 de la imagen origina. Bueno ya tienes varias opciones, tú decide. Espero que te sirva.
[/PHP]