Ver Mensaje Individual
  #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