PHP resize image using ratio

To maintain the aspect ratio of your original image, calculate the factor by which you have to resize the image in the vertical and horizontal direction.
Example, resize image to 1920 max width or max height

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
list($width, $height) = getimagesize("your file path");
$ratio = $width / $height;
 
if ($width > $height)
{
    //landscape
    if ($width > 1920 )
    {
        $full_width = 1920;
    }
    else
    {
        $full_width = $width;
    }
    //set full size
    $full_height = $full_width / $ratio;
 
    $size_full = array($full_width, $full_height);
}
else
{
    // Portrait
    if ($height > 1920 )
    {
        $full_height = 1920;
    }
    else
    {
        $full_height = $width;
    }
    //set full size
    $full_width = $full_height * $ratio;
 
    $size_full = array($full_width, $full_height);
}

By Keenlio, September 25, 2014

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + = 13

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>