Jump to content

Restraining image dimensions when unknown?


Edward

Recommended Posts

 

Hi,

 

I have created a webpage where people can post messages and images. I want to display all of the images on the same page, without them looking all different sizes. Obviously, I won't know whether they are landscape, portrait, or square, so can't set the width and heighht properties. Does anyone know the best way I can restrict the size of the image being displayed, without distorting it? Perhaps limiting the width and height to a maximum, while keeping the existing ratio?

 

Thank you.

1. Find out if width is bigger than the height, e.g. landscape or portrait.

2. Calculate a scale for a given max size max

3. Resize

 

maybe something like this?

 

function LoadJpeg_maxsize($imgname, $maxw, $maxh)
{
//	KEEPS ASPECT RATIO

// File and new size
$filename = $imgname;

list($width, $height) = getimagesize($filename);
$wi = $maxw / $width;
$hi = $maxh / $height;
$i=0;
if ($wi >= $hi)
{
	// $hi is smallest
	$i = $hi;
}
else
{
	//	$wii is smallest
	$i = $wi;
}
$w = $width * $i;
$h = $height * $i;

// Load
$thumb = imagecreatetruecolor($w, $h);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $w, $h, $width, $height);
//imagecopyresampled($thumb, $source, 0, 0, 0, 0, $w, $h, $width, $height);

// Output
imagejpeg($thumb);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.