5 Replies - 973 Views - Last Post: 14 May 2012 - 07:20 AM Rate Topic: -----

#1 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

uploaded image is distorted

Posted 17 April 2012 - 03:51 AM

Hi..I have a problem in my PHP code, this code is for automatic thumbnail photo gallery. it will automatically make thumbnails for uploaded photos but the resized image are distorted. And if I adjust the size for larger thumbnail, the color will be black and white. Is there anyone can help me with this?
Sorry with my english, I am asian:)

This is my code

<?php
//-----preupload.php-------//	

include("config.inc.php");

	// initialization
	$photo_upload_fields = "";
	$counter = 1;

	// default number of fields
	$number_of_fields = 5; 

// If you want more fields, then the call to this page should be like, 
// preupload.php?number_of_fields=20

	if( $_GET['number_of_fields'] )
	$number_of_fields = (int)($_GET['number_of_fields']);

	// Firstly Lets build the Category List

	$result = mysql_query( "SELECT category_id,category_name FROM gallery_category" );
	while( $row = mysql_fetch_array( $result ) )
	{
$photo_category_list .=<<<__HTML_END
	<option value="$row[0]">$row[1]</option>\n
__HTML_END;
	}
	mysql_free_result( $result );	

// Lets build the Photo Uploading fields
	while( $counter <= $number_of_fields )
	{
$photo_upload_fields .=<<<__HTML_END
<tr>
	<td>
	     Photo {$counter}:
	    <input name=' photo_filename[]' type='file' />
	</td>
</tr>
<tr>
	<td>
	     Caption:
	    <textarea name='photo_caption[]' cols='30' rows='1'></textarea>
	</td>
</tr>
__HTML_END;
	$counter++;
	}

// Final Output
echo <<<__HTML_END
<html>
<head>
	<title>Lets upload Photos</title>
</head>
<body>
<form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'>
<table width='90%' border='0' align='center' style='width: 90%;'>
<tr>
	<td>
		Select Category
		<select name='category'>
			$photo_category_list
		</select>
	</td>
</tr>
<tr>
	<td>
		<p>&nbsp;</p>
	</td>
</tr>

<!-Insert the photo fields here -->
$photo_upload_fields

<tr>
	<td>
	        <input type='submit' name='submit' value='Add Photos' />
	</td>
</tr>
</table>
</form>
</body>
</html>
__HTML_END;
?>








--upload.php--
<?php
	include("config.inc.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 =200;
					$thumbnail_height = (int)(200 * $size[1] / $size[0]);
				}
				else
				{
					$thumbnail_width = (int)(200 * $size[0] / $size[1]);
					$thumbnail_height = 200;
				}
			
				// 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."' /> File ".($counter+1)." Added<br />";
			}
		}
	$counter++;
	}

	// Print Result
echo <<<__HTML_END

<html>
<head>
	<title>Photos uploaded</title>
</head>
<body>
	$result_final
</body>
</html>

__HTML_END;
?>




Is This A Good Question/Topic? 0
  • +

Replies To: uploaded image is distorted

#2 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: uploaded image is distorted

Posted 17 April 2012 - 08:02 AM

Possibly it is because you are handling all the images as the same type when you resize & upload.
              if($source_handle)
075
                {
076
                    // Let's create an blank image for the thumbnail
077
                        $destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height );
078
                 
079
                    // Now we resize it
080
                    ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
081
                }



Have a look at this, and see how the upload image types are handled.
Was This Post Helpful? 1
  • +
  • -

#3 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

Re: uploaded image is distorted

Posted 18 April 2012 - 05:44 AM

Ok thanks. Now I will try to solve the problem of my code.:)
Was This Post Helpful? 0
  • +
  • -

#4 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

Re: uploaded image is distorted

Posted 09 May 2012 - 02:02 AM

I still did not able to solve this problem. What part of my could should I change?:(
Was This Post Helpful? 0
  • +
  • -

#5 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: uploaded image is distorted

Posted 09 May 2012 - 09:42 AM

Has your code changed since last post?
Was This Post Helpful? 0
  • +
  • -

#6 insik  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 31-January 12

Re: uploaded image is distorted

Posted 14 May 2012 - 07:20 AM

No, because I'm still confused.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1