Jump to content

proportional thumbnail - width and height


felipeebs

Recommended Posts

I tried this code, but it don't work fine.

if I try to transform an image from 1000x5000 or 5000x1000 (i.e.) ,it will not work to resize to the maximum 800x600 proportionally

 

 

my code:

function gravaImg($img, $folder, $max_x=800, $max_y=600, $quality=100)
{
list($width,$height) = getimagesize($img['tmp_name']);

if ($width > $max_x or $height > $max_y)
{
	if ($height > $width)
	{
		$tax = $width/$max_x;
		$x = $max_x;
		$y = $height/$tax;
	}
	elseif ($width > $height)
	{
		$tax = $height/$max_y;
		$x = $width/$tax;
		$y = $max_y;
	}
	// elseif ($width == $height)
	else
	{
		$tax = $width/$max_x;
		$x = $width/$tax;
		$tax = $height/$max_y;
		$y = $height/$tax;
	}
}
else
{
	$x = $width;
	$y = $height;
}

$redim = imagecreatetruecolor($x,$y);

if($img['type']=='image/png' or $img['type']=='image/x-png')
{
	$original = @imagecreatefrompng($img['tmp_name']);
}
elseif($img['type']=='image/gif')
{
	$original = @imagecreatefromgif($img['tmp_name']);
}
else{
	$original = @imagecreatefromjpeg($img['tmp_name']);
}

chdir($folder);

$nome = md5(time()).'.jpg';

imagecopyresized($redim, $original, 0, 0, 0, 0, $x, $y, $width, $height);

imagejpeg($redim,$nome,$quality);

imagedestroy($redim);
}

 

what's wrong???

 

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/115276-proportional-thumbnail-width-and-height/
Share on other sites

To resize proportionally X1/Y1 = X2/Y2

 

Therefore, if you know X2 (e.g. the width is greater than height, so width = 800), then Y2 = Y1/X1 * X2  - by rearranging the above formula

 

Conversely, if you know Y2, then X2 = X1/Y1 * Y2

Or you can divide the current with by the output width, get a ratio and multiply the height by the same ratio. If you want to keep your pics their original aspect ratio, you are better off just using a max size and resize the picture accordingly.

 

there is an example in the FAQ/code snippet section of the forum.

 

Ray

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.