<?php
function resizeFit($im,$width,$height) {
//Original sizes
$ow = imagesx($im); $oh = imagesy($im);
//To fit the image in the new box by cropping data from the image, i have to check the biggest prop. in height and width
if($width/$ow > $height/$oh) {
$nw = $width;
$nh = ($oh * $nw) / $ow;
$px = 0;
$py = ($height - $nh) / 2;
} else {
$nh = $height;
$nw = ($ow * $nh) / $oh;
$py = 0;
$px = ($width - $nw) / 2;
}
//Create a new image width requested size
$new = imagecreatetruecolor($width,$height);
$idColorTransparente = imagecolorallocatealpha($new, $colorTransparente['red'], $colorTransparente['green'], $colorTransparente['blue'], 127); // Asigna un color en una imagen retorna identificador de color o FALSO o -1 apartir de la version 5.1.3
imagefill($new, 0, 0, $idColorTransparente);// rellena de color desde una cordenada, en este caso todo rellenado del color que se definira como transparente
imagecolortransparent($new, $idColorTransparente); //Ahora definimos que en el nueva imagen el color transparente serĂ¡ el que hemos pintado el fondo.
imagesavealpha($new,true);
//Copy the image loosing the least space
imagecopyresampled($new, $im, $px, $py, 0, 0, $nw, $nh, $ow, $oh);
return $new;
}
?>