Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/12/2007, 13:31
132116806
 
Fecha de Ingreso: diciembre-2007
Ubicación: Arica-Chile
Mensajes: 4
Antigüedad: 16 años, 4 meses
Puntos: 0
Mi humilde aporte a este foro PHP

Hola Amigos, quiero compartir un código que adapte, que conste que no lo escribí yo, y espero que les sea de utilidad, lo que hace es lo siguiente:

1. Subir una imagen en formato jpg
2. Redimensionar al tamaño deseado la misma imagen sobreescibiendo la original
3. Generar el thumbnails correspondiente, tambien al tamaño deseado.

Nota: El redimensionamiento siempre mantiene la proporción.

Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
        <input name="archivo" type="file" size="35" />
        <input name="enviar" type="submit" value="Upload File" />
        <input name="action" type="hidden" value="upload" />     
</form>

<?php
if ($_POST["action"] == "upload"

   
$tamano $_FILES["archivo"]['size']; 
   
$tipo $_FILES["archivo"]['type']; 
   
$archivo $_FILES["archivo"]['name']; 

          
   (
$archivo != "") or die ("Error al subir la imagen ".$archivo); 
   
$destino =  "fotosavisos/".$archivo
   
move_uploaded_file($_FILES['archivo']['tmp_name'],$destino);
   
$status "La imagen <b>".$archivo."</b> se a subido correctamente !"
   echo 
$status

   
crearthumb("fotosavisos/","fotosavisos/",230,"",$archivo);
   
crearthumb("fotosavisos/","thumb/",85,"mini_",$archivo);
}
?>

<?php
function crearthumb ($pathToImages$pathToThumbs$thumbHeight$prefijo$nombreImagen)
{

  
$img imagecreatefromjpeg($pathToImages.$nombreImagen );
  
$width imagesx($img);
  
$height imagesy($img);

   
$thumbWidth = ($width $thumbHeight) / $height

   
//landscape 
   
if((($height/$thumbHeight) > $width/$thumbWidth)){ 
      echo(
'landscape: <br>'); 
      
$new_height $thumbHeight
      
$new_width floor($width $thumbHeight $height); 
   }else{
   
//vertical 
      
echo('portrait: <br>'); 
      
$new_height floor($height $thumbWidth $width); 
      
$new_width $thumbWidth
   }

  
// calcula el tamaño
    
$new_height $thumbHeight;
    
$new_width floor($width $thumbHeight $height); 

  
// crea una nueva imagen de manera temporal
  
$tmp_img imagecreatetruecolor($new_width$new_height);

  
// copia y dedimensiona la nueva imagen en la temporal que hemos creado
  
imagecopyresized($tmp_img$img0000$new_width$new_height$width$height);

  
// guarda el thumbnail en un archivo
  
imagejpeg($tmp_img$pathToThumbs.$prefijo.$nombreImagen100);
}
?> 
</body>
</html>
Saludos ....