Skip to end of metadata
Go to start of metadata

The code cfolderResizeImage() will enable you to resize the source image with keeping aspect ratio in PHP.

function cfolderResizeImage($file, $target_with_no_ext, $target_width = 40, $target_height = 40)
{
    $source_properties = getimagesize($file);
    $image_type = $source_properties[2];
    if( $image_type == IMAGETYPE_JPEG )
    {  
        $image_resource_id = imagecreatefromjpeg($file); 
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1], $target_width, $target_height);
        imagejpeg($target_layer, $target_with_no_ext. ".jpg", $target_width, $target_height);
    }
    elseif( $image_type == IMAGETYPE_GIF )
    { 
        $image_resource_id = imagecreatefromgif($file);
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1], $target_width, $target_height);
        imagegif($target_layer, $target_with_no_ext. ".gif", $target_width, $target_height);
    }
    elseif( $image_type == IMAGETYPE_PNG )
    {
        $image_resource_id = imagecreatefrompng($file);
        $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1], $target_width, $target_height);
        imagepng($target_layer, $target_with_no_ext. ".png", $target_width, $target_height);
    }
}
 
function fn_resize($image_resource_id, $width, $height, $target_width, $target_height)
{
    $h = $height * $target_width / $width;
    if ($h>$target_height)
    {
        $w = $target_width;
    }
    else
    {
        $h = $target_height;
        $w = $width * $target_height / $height;
    }
 
    $target_layer=imagecreatetruecolor($w,$h);
    imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$w,$h, $width,$height);
    return $target_layer;
}