Jump to content

GD Image Resizing .. I think .. ??


spacepoet

Recommended Posts

Hello:

 

I have a photo uploader in my admin area. First time I tried this so it's new to me. The upload form sends it to the page below, and does the resizing and saving.

 

It seems to automatically resize the photos to make thumbnails, which is really nice.

 

However, I am trying to do 2 things and can't figure it out.

 

1 - I would like to resize the main photo and not the thumbnail (or just use the thumbnail as the main photo, whichever seems easier).

I would like it to be 690px wide but let it scale in proportion to what the height should be (not set a height as I think that's what it is doing now).

 

This is the area where it gets resized:

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

 

Also, the thumbnails are coming out very dark and de-colored. How can I set it so it retains all the color and clarity, but just resizes them?

 

Point I'm trying to do is just resize upon upload, as most of my clients do not and will not resize photos before uploading them, and that means each photo from a digital camera will be about 1.3 MBs, which starts to make a photo gallery page very heavy and clunky.

 

This is the full code, if it will help:

<?php
// initialization
$result_final = "";
$counter = 0;

// List of our known photo types
$known_photo_types = array( 
					'image/pjpeg' => 'jpg',
					'image/jpeg' => 'jpg',
					'image/gif' => 'gif',
					'image/bmp' => 'bmp',
					'image/x-png' => 'png'
				);

// GD Function List
$gd_function_suffix = array( 
					'image/pjpeg' => 'JPEG',
					'image/jpeg' => 'JPEG',
					'image/gif' => 'GIF',
					'image/bmp' => 'WBMP',
					'image/x-png' => 'PNG'
				);

// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];

// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];

while( $counter <= count($photos_uploaded) )
{
	if($photos_uploaded['size'][$counter] > 0)
	{
		if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
		{
			$result_final .= "File ".($counter+1)." is not a photo<br />";
		}

		else
		{
			mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
			$new_id = mysql_insert_id();
			$filetype = $photos_uploaded['type'][$counter];
			$extention = $known_photo_types[$filetype];
			$filename = $new_id.".".$extention;

			mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );

			// Store the orignal file
			copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename);

			// 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]);
			}
			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 );

				// 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;' />";


		}
	}
$counter++;
}

// Print Result
echo <<<__HTML_END

$result_final

__HTML_END;
?>

 

Anyone know how to fix this?

 

Thanks.

Link to comment
Share on other sites

too scale:

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

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

 

Quality:

change:

$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );

to:

$destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height );

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.