2010-01-29 15 views
6

Estoy trabajando en un script que carga una imagen usando PHP y quiero hacer que cambie el tamaño de la imagen al ancho 180 antes de guardarla.
He intentado usar la biblioteca WideImage y -> saveFileTO (...) pero cuando incluyo WideImage.php en la página, ¡la página queda en blanco!
Así que aquí es mi guión si usted me puede ayudar y decirme cómo hacer que guardar la versión redimensionada
Subida y cambio de tamaño de la imagen de PHP

Respuesta

6

Puede utilizar el PHP GD library cambiar el tamaño de una imagen en subida.

El siguiente código debe darle una idea de cómo implementar el cambio de tamaño:

// Get the image info from the photo 
$image_info = getimagesize($photo); 
$width = $new_width = $image_info[0]; 
$height = $new_height = $image_info[1]; 
$type = $image_info[2]; 

// Load the image 
switch ($type) 
{ 
    case IMAGETYPE_JPEG: 
     $image = imagecreatefromjpeg($photo); 
     break; 
    case IMAGETYPE_GIF: 
     $image = imagecreatefromgif($photo); 
     break; 
    case IMAGETYPE_PNG: 
     $image = imagecreatefrompng($photo); 
     break; 
    default: 
     die('Error loading '.$photo.' - File type '.$type.' not supported'); 
} 

// Create a new, resized image 
$new_width = 180; 
$new_height = $height/($width/$new_width); 
$new_image = imagecreatetruecolor($new_width, $new_height); 
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

// Save the new image over the top of the original photo 
switch ($type) 
{ 
    case IMAGETYPE_JPEG: 
     imagejpeg($new_image, $photo, 100); 
     break; 
    case IMAGETYPE_GIF: 
     imagegif($new_image, $photo);   
     break; 
    case IMAGETYPE_PNG: 
     imagepng($new_image, $photo); 
     break; 
    default: 
     die('Error saving image: '.$photo); 
} 
1

Puede utilizar una clase que he escrito por sólo una tarea:

http://code.google.com/p/image2/source/browse/#svn/trunk/includes/classes

<?php 

    try 
    { 
     $image = new Image2($path_to_image); 
    } 
    catch (NotAnImageException $e) 
    { 
     printf("FILE PROVIDED IS NOT AN IMAGE, FILE PATH: %s", $path_to_image); 
    } 

    $image -> resize(array("width" => 180)) -> saveToFile($new_path); // be sure to exclude the extension 
    $new_file_location = $image -> getFileLocation(); // this will include the extension for future use 
1

Ni siquiera necesita usar la biblioteca WideImage.

Comprobar este script aquí: http://bgallz.org/502/php-upload-resize-image/

Se empieza por subir la imagen y guardar en un archivo de imagen Temp. Este script ejecuta un formulario con entradas para la altura máxima o el ancho máximo. Entonces, generará un nuevo archivo de imagen basado en el nuevo ancho/alto y luego copiará la imagen temporal en la nueva creada en el servidor.

Esto se ve con el siguiente código:

// Create temporary image file. 
$tmp = imagecreatetruecolor($newwidth,$newheight); 
// Copy the image to one with the new width and height. 
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height); 
Cuestiones relacionadas