Edward Posted February 18, 2007 Share Posted February 18, 2007 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. Link to comment https://forums.phpfreaks.com/topic/38978-restraining-image-dimensions-when-unknown/ Share on other sites More sharing options...
phat_hip_prog Posted February 18, 2007 Share Posted February 18, 2007 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); } Link to comment https://forums.phpfreaks.com/topic/38978-restraining-image-dimensions-when-unknown/#findComment-187638 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.