VeneHosting.com :: Tu Hosting en Venezuela

Autor Tema: Crear Thumbs o imagen reducida (redimensionar) al vuelo  (Leído 654 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado xombra

  • Moderador Global
  • Usuario Dedicado
  • *****
  • Mensajes: 169
  • Puntuación: 3
  • Sexo: Masculino
    • Ver Perfil
    • http://www.xombra.com
Crear Thumbs o imagen reducida (redimensionar) al vuelo
« : 29 de noviembre de 2009, 08:27:54 pm »
Puedes descargar el código e imagen desde: http://www.xombra.com/go.php?link=376

El ejemplo consta de dos secciones:

* crear_thumbs.php *
Código: [Seleccionar]

<html>
 <body>
   <p>Original</p>
     <p><img src="img/tux.jpg" alt="Tux" width="300" height="310" border="0" /></p>
   <p>Thumbs</p>
     <p>  <img src='salida.php?imagen=img/tux.jpg' border="0" /></p>
 </body>
</html>


y

salida.php
Código: [Seleccionar]

<?php  
/*
Script  bajo los términos y Licencia
GNU GENERAL PUBLIC LICENSE
Ver Terminos en:
http://www.gnu.org/copyleft/gpl.html
Traducción al español:
http://gugs.sindominio.net/gnu-gpl/gples.html

Autor: Hector A. Mantellini (Xombra)
*/ 

// Determinamos ancho y alto maximo para nuestro thumb

$anchura  100;  
$maxima_y 100

if (isset(
$_GET["imagen"]))
  { 
$nombre $_GET["imagen"];  }
else
{ die(
"Error no ha declarado una imagen");} 

# Determinamos el alto - ancho - tipo de imagen
$datos getimagesize($nombre);  

switch (
$datos[2]) {
    case 
1:
        
$img imagecreatefromgif($nombre);
        break;
    case 
2:
        
$img imagecreatefromjpeg($nombre);
        break;
    case 
3:
        
$img imagecreatefrompng($nombre);
        break;
default:
 die("Tipo de Imagen no valida");
}
 
   
$ratio  $datos[0] / $anchura;  
$altura $datos[1] / $ratio;  

if(
$altura >= $maxima_y)
   { 
$anchura2 $maxima_y $anchura $altura;
     
$altura   $maxima_y;
 $anchura  $anchura2;
   }  
   
$thumb imagecreatetruecolor($anchura,$altura);  
imagecopyresampled($thumb$img0000$anchura$altura$datos[0], $datos[1]);  

# Dependiendo del tipo de imagen le damos salida

switch ($datos[2]) {
    case 
1:
        
header("Content-type: image/gif"); 
imagegif($thumb);
        break;
    case 
2:
        
header("Content-type: image/jpeg");
    imagejpeg($thumb);
        break;
    case 
3:
        
header("Content-type: image/png");
imagepng($thumb); 
        break;
default:
 die("Tipo de Imagen no valida");
}

imagedestroy($thumb);  
?>


---------------------------------------------
Sitio web: http://www.xombra.com
Coordinador Safecreative Venezuela - http://www.safecreative.org
Coordinador Regional AWVEN - http://www.awven.com
Twitter: @xombra
Identi.ca: xombra
GNU/Linux User: #414452
-------------------------------------

PHP de Venezuela

Crear Thumbs o imagen reducida (redimensionar) al vuelo
« : 29 de noviembre de 2009, 08:27:54 pm »

 

PHP de Venezuela on Facebook