Skip to content

Instantly share code, notes, and snippets.

@xombra
Last active August 5, 2017 13:29
Show Gist options
  • Save xombra/11310865 to your computer and use it in GitHub Desktop.
Save xombra/11310865 to your computer and use it in GitHub Desktop.
Redimensionar imagen en caliente
function REDIMENSIONAR($anchura,$maxima_y,$nombre)
{ $img_nueva = $nombre;
$datos = getimagesize($nombre);
$maxima_y = $datos[1];
switch ($datos[2]) {
case 2:
$img = imagecreatefromjpeg($nombre);
break;
case 3:
$img = imagecreatefrompng($nombre);
break;
default:
die("Tipo de Imagen no valida");
}
$ratiow = $anchura / $datos[0];
$ratioh = $maxima_y / $datos[1];
$ratio = min($ratioh, $ratiow);
$anchura = $datos[0] * $ratio;
$altura = $datos[1] * $ratio;
$ratio = $datos[0] / $anchura;
$altura = $datos[1] / $ratio;
$thumb = imagecreatetruecolor($anchura,$altura);
$blanco = imagecolorallocate($thumb, 255, 255, 255);
if ($datos[2] == 3){
imagecolortransparent($thumb,$blanco);
imagesavealpha($thumb,true);
$color = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($thumb,0,0,$color);
}
if (!imagecopyresampled($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]))
{ die("ERROR: La Imagen que proporcionó está corrupta. Intente con otra imagen.<br/>
Regrese a la página anterior");
}
switch ($datos[2]) {
case 2:
imagejpeg($thumb,$img_nueva,90);
break;
case 3:
imagepng($thumb,$img_nueva,7);
break;
}
imagedestroy($img);
imagedestroy($thumb);
return $img_nueva;
}
@xombra
Copy link
Author

xombra commented Jul 26, 2017

Modificado a función. Y mejorado la compresión de imagen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment