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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.