PHP para subir y tamaño de imagen
-
23-09-2019 - |
Pregunta
Estoy trabajando en un script que carga una imagen usando PHP y quiero hacerlo cambiar el tamaño de la imagen al ancho 180 antes de guardarla.
He intentado utilizar la biblioteca y WIDEimage -> saveFileTO (...) pero cuando incluyo el WideImage.php en la página, la página se queda en blanco !!
Así que aquí está mi script si usted me puede ayudar y decirme cómo hacer que guardar la versión redimensionada
Solución
Puede utilizar el PHP GD biblioteca para cambiar el tamaño de una imagen en carga .
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);
}
Otros consejos
Se puede utilizar una clase que he escrito por sólo una tarea:
http://code.google. com / p / imagen2 / fuente / Navegar / # svn / trunk / includes / clases
<?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
Usted no necesita ni siquiera utilizar la biblioteca WIDEimage.
Comprobar este script aquí: http://bgallz.org/502/php-upload-resize-image/
Se inicia mediante la subida de la imagen y guardar en un archivo de imagen Temp. Este script se ejecuta de una forma con entradas para la altura o anchura max max. Por lo tanto, a continuación, generará un nuevo archivo de imagen en base a la nueva anchura / altura y luego copiar la imagen temporal en el nuevo creado 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);
no lo utilizo de cualquier biblioteca Compruebe este script http://dr-wordpress.blogspot.com/2013 /12/image-resizing-using-php.html Acaba de dar a la calidad de imges de (0-99) este código automáticamente el tamaño de las imágenes durante la carga