Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Redimensionar imagen para subir

Estas en el tema de Redimensionar imagen para subir en el foro de PHP en Foros del Web. Hola tengo un problema al redimensionar una imagen que quiero subir al servidor. He estado mirando aquí en el foro y en arios sitios y ...
  #1 (permalink)  
Antiguo 16/06/2014, 05:44
 
Fecha de Ingreso: marzo-2009
Mensajes: 221
Antigüedad: 15 años, 1 mes
Puntos: 0
Redimensionar imagen para subir

Hola tengo un problema al redimensionar una imagen que quiero subir al servidor.
He estado mirando aquí en el foro y en arios sitios y he llegado a este código.

Cuando quiero mostrar la imagen con
imagejpeg($thumb);

o hacer un copy al servidor se queja como si no se hubiese generado bien la imagen
Warning: copy(Resource id #2) [function.copy]: failed to open stream: No such file or directory ...

Tampoco se como puedo saber si alguna de estas lineas lo hace mal
$thumb = imagecreatetruecolor($nuevo_ancho, $nuevo_alto);

$origen = imagecreatefromjpeg($nombre_archivo);


He hecho un var_dump pero solo me devuelve esto
string(18) "Resource id #2

Haber si me orientas un poco que ya no se por donde mirar

Gracias

<?php


// Tipo de contenido
header('Content-type: image/jpeg');


$nombre_archivo = $_FILES['portada']['tmp_name'];


// Obtener nuevos tamaños
list($ancho, $alto) = getimagesize(rtrim($nombre_archivo));
$ancho_horizontal = '640'; // pixeles
$alto_horizontal = '480'; // pixeles

$ancho_vertical = '400'; // pixeles
$alto_vertical = '600'; // pixeles

echo("nombre_archivo " .$nombre_archivo. "<br>");

echo("ANCHO " .$ancho. "<br>");
echo("ALTO " .$alto. "<br>");


//Foto horizontal
if($ancho > $alto){
$nuevo_ancho = $ancho_horizontal;
$nuevo_alto = $alto_horizontal;

}else{
$nuevo_ancho = $ancho_vertical;
$nuevo_alto = $alto_vertical;


}

// Carcgar
$thumb = imagecreatetruecolor($nuevo_ancho, $nuevo_alto);

$origen = imagecreatefromjpeg($nombre_archivo);


// Cambiar el tamaño

imagecopyresampled($thumb, $origen, 0, 0, 0, 0, $nuevo_ancho, $nuevo_alto, $ancho, $alto);


imagejpeg($thumb);

//copy($thumb, $nombre.'1.jpg');


?>
  #2 (permalink)  
Antiguo 16/06/2014, 06:18
Avatar de Italico76  
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: Redimensionar imagen para subir

Solo mirando por encima el codigo veo que generas la imagen "por pantalla" ya que no le pasas el segundo parametro a imagejpeg($thumb, $filename);

Cita:
imagejpeg($thumb, $filename);

Y si la quieres en un archivo para que los headers ?

Cita:
header('Content-type: image/jpeg');
__________________
Salu2!

Última edición por Italico76; 16/06/2014 a las 06:25
  #3 (permalink)  
Antiguo 16/06/2014, 08:03
 
Fecha de Ingreso: marzo-2009
Mensajes: 221
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Redimensionar imagen para subir

Hola italico, quiero copiarla lo que como no funcionaba he intentado mostrarla por pantalla haber si asi se mostraba.

El código sería este:


Código PHP:
<?php



$nombre_archivo 
$_FILES['portada']['tmp_name'];


// Obtener nuevos tamaños
list($ancho$alto) = getimagesize(rtrim($nombre_archivo));
$ancho_horizontal '640'// pixeles
$alto_horizontal '480'// pixeles

$ancho_vertical '400'// pixeles
$alto_vertical '600'// pixeles

echo("nombre_archivo " .$nombre_archivo"<br>");

echo(
"ANCHO " .$ancho"<br>");
echo(
"ALTO " .$alto"<br>");


//Foto horizontal
if($ancho $alto){
$nuevo_ancho $ancho_horizontal;
$nuevo_alto $alto_horizontal;

}else{
$nuevo_ancho $ancho_vertical;
$nuevo_alto $alto_vertical;


}

// Carcgar
$thumb imagecreatetruecolor($nuevo_ancho$nuevo_alto);

$origen imagecreatefromjpeg($nombre_archivo);


// Cambiar el tamaño

imagecopyresampled($thumb$origen0000$nuevo_ancho$nuevo_alto$ancho$alto);


copy($thumb$nombre.'1.jpg');


?>
  #4 (permalink)  
Antiguo 16/06/2014, 08:33
Avatar de Italico76  
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: Redimensionar imagen para subir

Lo primero....... revisa el formulario:

<form action="recibir.php" method="post" enctype="multipart/form-data">
<input type="file" name="portada"/><p/>
<button>Enviar</button>
</form>

El enctype es importante y al recibir... verifica todo este bien:

Código PHP:
Ver original
  1. <?php
  2. var_dump($_FILES);
  3.  
  4. $input_file  = $_FILES['portada']['tmp_name'];

Ahora veamos el archivo .php que procesa:

Código PHP:
Ver original
  1. <?php
  2.  
  3. $input_file  = $_FILES['portada']['tmp_name'];
  4. $output_file = 'test.jpg';
  5.  
  6. resize($input_file,640,480,$output_file);
  7.  
  8. function resize($input_file,$new_width,$new_height,$output_file)
  9. {
  10.     list($width, $height) = getimagesize($input_file);
  11.  
  12.     $image_p = imagecreatetruecolor($new_width, $new_height);
  13.     $image = imagecreatefromjpeg($input_file);
  14.     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  15.     imagejpeg($image_p, $output_file, 100);
  16. }
  17. ?>
  18. <img src="<?= $output_file ?>" />
__________________
Salu2!

Última edición por Italico76; 16/06/2014 a las 08:43
  #5 (permalink)  
Antiguo 16/06/2014, 08:46
 
Fecha de Ingreso: marzo-2009
Mensajes: 221
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Redimensionar imagen para subir

En el segundo código que he pegado ya he quitado los headers


el var_dump me devuelve esto:

["portada"]=> array(5) { ["name"]=> string(10) "pLucy4.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(27) "C:\Windows\Temp\phpE701.tmp" ["error"]=> int(0) ["size"]=> int(55229) }

El formulario si tengo el enctype

<form name="form1" accept-charset="utf-8" id="form1" enctype="multipart/form-data" method="post" action="resizeimage2.php">

El error que me da es este:

Warning: copy(Resource id #2) [function.copy]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\mooda29.com\httpdocs\pruebas\adm in\resizeimage2.php on line 120
  #6 (permalink)  
Antiguo 16/06/2014, 08:47
Avatar de Italico76  
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: Redimensionar imagen para subir

Prueba esto que escribi:
(el copy no es necesario)

Código PHP:
Ver original
  1. <?php
  2.  
  3. $input_file  = $_FILES['portada']['tmp_name'];
  4. $output_file = 'test.jpg';
  5.  
  6. resize($input_file,640,480,$output_file);
  7.  
  8. function resize($input_file,$new_width,$new_height,$output_file=null)
  9. {
  10.     if (is_null($output_file))
  11.         header('Content-Type: image/jpeg');
  12.     list($width, $height) = getimagesize($input_file);
  13.  
  14.     $image_p = imagecreatetruecolor($new_width, $new_height);
  15.     $image = imagecreatefromjpeg($input_file);
  16.     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  17.     imagejpeg($image_p, $output_file, 100);
  18. }
  19. ?>
  20. <img src="<?= $output_file ?>" />
__________________
Salu2!
  #7 (permalink)  
Antiguo 16/06/2014, 09:16
 
Fecha de Ingreso: marzo-2009
Mensajes: 221
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Redimensionar imagen para subir

Ahora si que funciona lo muestra y lo copia al servidor

He estado comparando y mirando el fallo mio y era al hacer el copy, con tu linea
imagejpeg($image_p, $output_file, 100);

también funciona el mio

Muchas Gracias de nuevo

Etiquetas: redimensionar
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:42.