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

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 *


9 − = two

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>