Last active
August 5, 2017 13:29
-
-
Save xombra/11310865 to your computer and use it in GitHub Desktop.
Redimensionar imagen en caliente
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modificado a función. Y mejorado la compresión de imagen