Resize image with a fixed height

0

I'm looking through the internet and found several examples of how to resize an image, but I do not know how to resize an image with a fixed height and that the width adapts proportionally to that height.

I thought it would be an easy thing to get the size with getimagesize and with imagescale to determine the height, I guess it calculated the width automatically.

Let's see if you can help me, thanks

    
asked by Isaac Palacio 21.08.2017 в 18:40
source

1 answer

1

I solved it, I put the resulting code:

function redimensionar($pathorigen,$pathdestino) {
    list($ancho_or, $alto_or) = getimagesize($pathorigen);  

    $ancho1 = 100 * $ancho_or;
    $ancho = $ancho1 / $alto_or;

    $alto = 100;

    $thumb = imagecreatetruecolor($ancho, $alto);       
    $fondo = imagecolorallocate($thumb, 255, 255, 255);
    imagefilledrectangle($thumb, 0, 0, $alto_or, $ancho_or, $fondo);

    $extension =  substr($pathorigen,-3);

    if ($extension == "jpg" || $extension == "peg") {       
        $origen = imagecreatefromjpeg($pathorigen);

        imagecopyresampled($thumb, $origen, 0, 0, 0, 0, $ancho, $alto, $ancho_or, $alto_or);
        imagejpeg($thumb,$pathdestino,72);  
    }
    else if ($extension == "png") { 
        $origen = imagecreatefrompng($pathorigen);

        imagecopyresampled($thumb, $origen, 0, 0, 0, 0, $ancho, $alto, $ancho_or, $alto_or);
        imagepng($thumb,$pathdestino,6);    
    }
    else if ($extension == "gif") {     
        $origen = imagecreatefromgif($pathorigen);

        imagecopyresampled($thumb, $origen, 0, 0, 0, 0, $ancho, $alto, $ancho_or, $alto_or);
        imagegif($thumb,$pathdestino);              
    }
    imagedestroy($thumb);
}       
    
answered by 22.08.2017 / 12:53
source