Jump to content

How do I adjust the size with GD ??


spacepoet

Recommended Posts

Hello:

 

Hoping someone can help me figure this out...

 

I am resizing a photo/thumbnails with GD (I think that's the correct term) upon uploading.

 

Works fine but the one thing I can't figure out is how to scale the height proportionate with the width.

 

Meaning, I have the width set to 690px, and the height set to 500px.

 

It is making the images look squished.

 

What I would like to do is have a set width (like 690px), but have the height scale down proportionalety.

 

Or, can I just remove the height (although I tried that and it did not work).

 

This is the code:

			// Let's get the Thumbnail size
			$size = GetImageSize( $images_dir."/".$filename );
			if($size[0] > $size[1])


			//{
				//$thumbnail_width = 100;
				//$thumbnail_height = (int)(100 * $size[1] / $size[0]);
			//}
			//else
			//{
				//$thumbnail_width = (int)(100 * $size[0] / $size[1]);
				//$thumbnail_height = 100;
			//}



			{
				//$thumbnail_width = 690;
				//$thumbnail_height = (int)(500 * $size[1] / $size[0]);



				$old_width = $size[0];
				$old_height = $size[1];

				$thumbnail_width = 690;
				$thumbnail_height = ($old_height * $thumbnail_width / $old_width);					





			}
			else
			{
				$thumbnail_width = (int)(690 * $size[0] / $size[1]);
				$thumbnail_height = 500;
			}



			// Build Thumbnail with GD 1.x.x, you can use the other described methods too
			$function_suffix = $gd_function_suffix[$filetype];
			$function_to_read = "ImageCreateFrom".$function_suffix;
			$function_to_write = "Image".$function_suffix;

			// Read the source file
			$source_handle = $function_to_read ( $images_dir."/".$filename ); 

			if($source_handle)
			{
				// Let's create an blank image for the thumbnail
			     	//$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );
			     	
			    $destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height ); 	
			     	
			     	

				// Now we resize it

		      	ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );


			}

			// Let's save the thumbnail
			$function_to_write( $destination_handle, $images_dir."/tb_".$filename );
			ImageDestroy($destination_handle );
			//

			$result_final .= "<img src='".$images_dir. "/tb_".$filename."' style='margin-right: 20px; width: 100px;' />";

 

Anyone have any ideas on this?

 

Thanks much.

Link to comment
Share on other sites

You want "all" images to be 690 wide?  I'm not all that familiar with "GD", but could you calculate the percentage of the original image width, then apply that percentage to the height?  Example: original image dimensions = 1024px BY 768px.

 

690 / 1024 = .6738281 <- % width

 

.6738281 * 768 = 517px <- height in px.

 

Or I may not understand your issue.  Sorry.

 

 

Link to comment
Share on other sites

Hi:

 

Yes, I want them to be 690px wide, but to keep the height in proportion.

 

Example: if the raw image is 1380px wide X 900px high, I want it to scale

to 690px X 450px high, based on setting the width only.

 

Or, if the next image is 1500px wide X 1300px high, I want it to scale

to 690px X 650px high, based on setting the width only.

 

I think it needs to happen here:

$size = GetImageSize( $images_dir."/".$filename );
if($size[0] > $size[1])

//{
//$thumbnail_width = 100;
//$thumbnail_height = (int)(100 * $size[1] / $size[0]);
//}
//else
//{
//$thumbnail_width = (int)(100 * $size[0] / $size[1]);
//$thumbnail_height = 100;
//}

{
//$thumbnail_width = 690;
//$thumbnail_height = (int)(500 * $size[1] / $size[0]);

$old_width = $size[0];
$old_height = $size[1];

$thumbnail_width = 690;
$thumbnail_height = ($old_height * $thumbnail_width / $old_width);	

}
else
{
$thumbnail_width = (int)(690 * $size[0] / $size[1]);
$thumbnail_height = 500;
}

 

Any ideas on how to revise the above code and get this to work?

 

Thanks much.

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.