Page 1 of 1

GD query?

Posted: Fri Sep 29, 2006 10:21 am
by rami
i am using this part of script from "build image gallery in minutes".
This is working fine.But i want modify it for
1) Rather than uploading originl image(browsed by the user from their computer) and resizing images...i dont want to upload original

image but i just want to upload or save resized thumb nail version of image
i am confused how to manage that tmp things for this with out much performance issue as i upload 10 images at once using loop


2)The user browse the any size image from thier computer but i want to upload the original's image 400*400 version and thumbnail of

it....
do i need the re-run those gd function two times separately?


any codes
any help
i feel it will take only some modification but i want to do it in efficient manner as i upload 10 images(max) at once...

Code: Select all

<?php 
$page_title = 'Add photo';

if (isset($_POST['submit'])) 
{ 

	require_once ('../../dbconnect.php');	
	
	$images_dir = "images/years";
	// 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'
					);
	$noimage='noimage.jpg';
	// GD Function List
	$gd_function_suffix = array( 
			'image/pjpeg' => 'JPEG',
			'image/jpeg' => 'JPEG',
			'image/gif' => 'GIF',
			'image/bmp' => 'WBMP',
			'image/x-png' => 'PNG'
					);
					
					
		



	
	if (!empty( $_FILES['photo_filename']))
						{
	$photos_uploaded = $_FILES['photo_filename'];
			$ph=1;
				} else 
						{
		$photos_uploaded['size']=0;
					}

	
	if ($car) 
		{ 
if($photos_uploaded['size'] > 0)
					{	
		if(!array_key_exists($photos_uploaded['type'], 

$known_photo_types))
										{
										echo "File is not a photo<br />";
										}
									else
									{
		$query = "insert into  years (image) VALUES ('0')";	
	
										$result = mysql_query ($query) or 

trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());

										if (mysql_affected_rows() == 1)
						 { // If it ran OK			$new_id = mysql_insert_id();
								$filetype = $photos_uploaded['type'];
											$extention = $known_photo_types[$filetype];
											$filename = $new_id.".".$extention;
									$query1 = "update years set image='".addslashes($filename)."' where year_id='".addslashes($new_id)."'";	
		$result1 = @mysql_query ($query1);
											

copy($photos_uploaded['tmp_name'], $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 = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );
											
												// Now we resize 

it
												

ImageCopyResampled( $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 );
											//
echo  "<img src='".$images_dir. "/tb_".$filename."' />  <br> Photo Added<br />";
											echo '<h3>A Year has 

been Added!</h3>';
											exit();
				      }//if query returned value
					  
											else {