Foros del Web » Programando para Internet » PHP »

Subir imagen con un tamaño fijo

Estas en el tema de Subir imagen con un tamaño fijo en el foro de PHP en Foros del Web. Hola a todos.! necesito hacer que si subo una imagen de 1024x800px, al guardarse quede en 500x500px, tengo un código que sube imagen y me ...
  #1 (permalink)  
Antiguo 08/06/2018, 14:34
Avatar de fedefrankk  
Fecha de Ingreso: agosto-2007
Mensajes: 871
Antigüedad: 16 años, 8 meses
Puntos: 7
Pregunta Subir imagen con un tamaño fijo

Hola a todos.! necesito hacer que si subo una imagen de 1024x800px, al guardarse quede en 500x500px, tengo un código que sube imagen y me guarda el nombre en la base de datos, no se que agregarle para que haga lo comentado...

Dejo el código que tengo para subir imágenes ,


Código PHP:
Ver original
  1. <?php
  2.  
  3. if (count($_FILES)){
  4.     $temp= $_FILES['imagen']['tmp_name'];
  5.     $img_info = getimagesize($_FILES['imagen'] ['tmp_name']); //
  6.     $ancho=$img_info[0];
  7.     $alto=$img_info[1];
  8.     if(is_uploaded_file($temp)){
  9.         $sep=explode('image/',$_FILES["imagen"]["type"]); // Separamos image/
  10.         $tipo=$sep[1]; // Optenemos el tipo de imagen que es
  11.         $destino="../imgpublicidad/";
  12.         //$ape=$usunumclie;
  13.         //$nom_img=$usunumclie;
  14.         //trim($todo);
  15.         move_uploaded_file ( $_FILES [ 'imagen' ][ 'tmp_name' ], $destino . '/' .$nom_img.'.'.$tipo);  // Subimos el archivo
  16.            
  17.  
  18.  
  19.                                
  20.         //print " el archivo fu cargado";
  21.     }else{
  22.         //print "el archivo no fue cargado";
  23.     }
  24.     }
  25.  
  26. $imag=mysqli_query($cone,"INSERT INTO users ( numerocli,nombref,email,password, imgpuntos, nombrealt,imgtipo,imgnombre,domicilio,telefono,whasap)
  27. VALUES
  28. ('$numerocli','$nombref','$email','$password','$imgpuntos','$nombrealt','$tipo','$nom_img','$domicilio','$telefono','$whasap')");
  29.  
  30.  
  31.  
  32.  
  33. mysqli_close($cone);
  34.  
  35. echo "todo bien";
  36.  
  37. ?>


Muchas gracias a todos por su tiempo.
saludos
fede
  #2 (permalink)  
Antiguo 08/06/2018, 16:00
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: Subir imagen con un tamaño fijo

Pues necesitas agregarle una función que redimensione la imagen.

Hay varios ejemplos en la red, incluso aquí mismo, en php se suele usar gd

Aunque también hay que usa imagemagick cuando GD se queda corto en sus necesidades.
  #3 (permalink)  
Antiguo 08/06/2018, 17:49
Avatar de fedefrankk  
Fecha de Ingreso: agosto-2007
Mensajes: 871
Antigüedad: 16 años, 8 meses
Puntos: 7
Pregunta Respuesta: Subir imagen con un tamaño fijo

Hola gracias por responder,, ahora me fijo lo de la librería GD,

Como me doy cuenta que la versión de PHP la soporta.?

saludos.!!
  #4 (permalink)  
Antiguo 09/06/2018, 09:28
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: Subir imagen con un tamaño fijo

pues si ejecutas phpinfo() sabrás si la tienes instalada. Actualmente es una librería común, ya viene en la mayoría de instalaciones.
  #5 (permalink)  
Antiguo 17/11/2018, 11:49
 
Fecha de Ingreso: diciembre-2008
Mensajes: 122
Antigüedad: 15 años, 4 meses
Puntos: 1
Respuesta: Subir imagen con un tamaño fijo

Tambien puedes usar simpleimage es sencillo y muy util yo uso ese
  #6 (permalink)  
Antiguo 18/11/2018, 20:58
Avatar de ArturoGallegos
Moderador
 
Fecha de Ingreso: febrero-2008
Ubicación: Morelia, México
Mensajes: 6.774
Antigüedad: 16 años, 1 mes
Puntos: 1146
Respuesta: Subir imagen con un tamaño fijo

Cita:
Iniciado por santa2r Ver Mensaje
Tambien puedes usar simpleimage es sencillo y muy util yo uso ese
Eso es un script PHP que hace uso de
  #7 (permalink)  
Antiguo 26/11/2018, 14:18
alvaro_trewhela
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Subir imagen con un tamaño fijo

no lo he probado, pero debería andar bien:

Código PHP:
Ver original
  1. function resize($img,$mW,$mH){
  2.  
  3. // Only do something if is a image file
  4. $fi = new finfo(FILEINFO_MIME);
  5. $type = explode(";", $fi->file($img))[0];
  6. $im = null;
  7.  
  8.     if($type == "image/jpeg"){
  9.     $im = imagecreatefromjpeg($img);
  10.     }
  11.     else if($type == "image/png"){
  12.     $im = imagecreatefrompng($img);
  13.     }
  14.     else if($type == "image/bmp"){
  15.     $im = imagecreatefrombmp($img);
  16.     }
  17.     else if($type == "image/gif"){
  18.     $im = imagecreatefromgif($img);
  19.     }
  20.     else{
  21.     return null;
  22.     }
  23.  
  24. // End check if is image
  25.  
  26. list($w, $h) = getimagesize($img); //get dimensions
  27. $nW = $mW/$w; //How much we must resize width
  28. $nH = $mH/$h; //How much we must resize height
  29. $res = false; //Auxiliary variable to know if we must resize or not
  30.  
  31. //Get wich side we must resize most
  32. $major = 0; //major var
  33.     if($nW < 1){ //If we need to resize width
  34.     $res = true; //res true
  35.     $major = $nW; // and major resize side = $nW
  36.     }
  37.  
  38.     if($nH < 1){ //If we need to resize height
  39.     $res = true; //res true
  40.         if($nH > $major) $major = $nH; //if new height is less than major, then major = nH
  41.     }
  42.  
  43.     if($res){ //If is neccessary to resize then...
  44.     $th = imagecreatetruecolor($w*$major, $h*$major); //Creating a thumb for the resized image
  45.     imagecopyresized($th, $im, 0, 0, 0, 0, $w*$major, $h*$major, $w, $h); //resizing
  46.     return $th; //return the thumb
  47.     }
  48.  
  49. //Else we return the im resorce
  50. return $im;
  51. }

$img es la dirección completa de la imagen
$mW el ancho máximo permitido
$mH el alto máximo permitido

No lo probé pero creo que debería andar bien

Última edición por alvaro_trewhela; 27/11/2018 a las 15:15

Etiquetas: fijo, mysql, nombre, query, sql, tamaño
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 13:30.