Ver Mensaje Individual
  #6 (permalink)  
Antiguo 22/04/2010, 11:14
Avatar de gogupe
gogupe
 
Fecha de Ingreso: octubre-2006
Ubicación: Mallorca
Mensajes: 897
Antigüedad: 20 años
Puntos: 32
Respuesta: Usar libreria gd para redimensionar imagenes.

Hola, te pongo el formulario al completo de subir una fotografía, esto lo cogí por ahí de internet

Código PHP:
<? 
if ($_POST[submit]=='Aceptar')
    {
        
$max_upload_width = 200; 
        
$max_upload_height = 200; 

        
// JPG/JPEG
        
if($_FILES["imagen"]["type"] == "image/jpeg" || $_FILES["imagen"]["type"] == "image/pjpeg"){
            
$image_source = imagecreatefromjpeg($_FILES["imagen"]["tmp_name"]);

        }
        
// GIF
        
if($_FILES["imagen"]["type"] == "image/gif"){
            
$image_source = imagecreatefromgif($_FILES["imagen"]["tmp_name"]);
        }

        
// BMP
        
if($_FILES["imagen"]["type"] == "image/bmp"){
            
$image_source = imagecreatefromwbmp($_FILES["imagen"]["tmp_name"]);
        }
        
// PNG
        
if($_FILES["imagen"]["type"] == "image/x-png"){
            
$image_source = imagecreatefrompng($_FILES["imagen"]["tmp_name"]);
        }

        
// PNG
        
if($_FILES["imagen"]["type"] == "image/png"){
            
$image_source = imagecreatefrompng($_FILES["imagen"]["tmp_name"]);
        }



        
$remote_file = "../fotografias/".$_FILES["imagen"]["name"];  //la ruta donde se encuentra la imagen 
                                     
        
imagejpeg($image_source,$remote_file,100); 
        
chmod($remote_file,0777); 

        list(
$image_width, $image_height) = getimagesize($remote_file); 
                                         
        if(
$image_width>$max_upload_width || $image_height >$max_upload_height) 
            { 
                
$proportions = $image_width/$image_height; 

                if(
$image_width>$image_height){ 
                    
$new_width = $max_upload_width; 
                    
$new_height = round($max_upload_width/$proportions); 
                } 
                else{ 
                    
$new_height = $max_upload_height; 
                    
$new_width = round($max_upload_height*$proportions); 
                } 

                
$new_image = imagecreatetruecolor($new_width , $new_height); 
                
$image_source = imagecreatefromjpeg($remote_file); 

                
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height); 
                
imagejpeg($new_image,$remote_file,100); 

                
imagedestroy($new_image); 
            } 

        
imagedestroy($image_source); 
    }    
?>


<form name="subir_imagen" method="POST" enctype="multipart/form-data">

<input type="file" name="imagen">
<input type="submit"  name="submit" value="Aceptar">
</form>
Los valores
$max_upload_width = 200;
$max_upload_height = 200;
Significa que ni el alto ni el ancho va a superar los 200 px, por ejemplo, si una fotografia tiene 800x600, se convertirá en 200x153, pero si es 600x800 se convertira en 153x200... siempre respectado el aspect ratio

Saludos.