Photo resize (GD)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Granda
Forum Newbie
Posts: 7
Joined: Thu Mar 03, 2005 5:05 pm

Photo resize (GD)

Post by Granda »

hello all,

I have a script for uploading pictures, and making a thumbular and it works just fine, it uploads a picture, and makes a thumb with tb_ prefix.

Now, the problem is that the script doesent resize the original image, it only makes a thumb of some size i define in the script. I would like this script to make a thumb, as it does already, but i also would like it to resize the original image to some size, for example 800x600, so at the end i get 100x100 tumb and 800x600 pic.

the script :

Code: Select all

// UPLOAD !!!!
// initialization

	// Fetch the photo caption array
	

	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 rabljena_plovila_slike (`photo_filename`,`ID`) VALUES('0','$zadnji_id')" );
				$new_id = mysql_insert_id();
				$filetype = $photos_uploaded['type'][$counter];
				$extention = $known_photo_types[$filetype];
				$filename = $new_id.".".$extention;

				mysql_query( "UPDATE rabljena_plovila_slike 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;
				}
			
				// Build Thumbnail with GD 2.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 a 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 );
				//

				$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />";
			}
		}
	$counter++;
	}


// END UPLOAD
If someone can explain how to change it, or even change it for me, i would apriciate it very much becuase tomorow i have to finish the web page i am making and the customer wants that the images are all the same size.

thx in advanced.


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Last edited by Granda on Sat Apr 16, 2005 7:14 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just have the script make two thumbs .... one 800x600 and the other 100x100, then unlink() the original file uploaded.

You dont even need to do the unlink() if you haven't copied the uploaded file to a permanent location. i.e. You work with it as $_FILES['field_name']['tmp_name'] ;-)
asterinex
Forum Commoner
Posts: 52
Joined: Thu Nov 25, 2004 7:01 am
Location: Belgium

getimagsize is veeery slooow

Post by asterinex »

Hi,

I first used the getimagsize function to scale my pictures properly. But this function is so slow I think it is useless.
What I do is safe with and height of the pic in de db and call a function to resize it properly.
Just an idea

Code: Select all

function PHP_Picture_Blit ($pic,$max_pix,$pic_w,$pic_h)
{
		

	if ($pic_w<=0 || $pic_h<=0)
	{	$x =$max_pix;
		$y =$max_pix;
	
	}
	else
	{	 
		if ($pic_w>$pic_h)
			 $longest=$pic_w;
		else
			$longest=$pic_h;
					
		$x = $pic_w;
		$y = $pic_h;
		if ($longest>$max_pix)
		{
			$divnum=PHP_Picture_Resize_Get_DivitionNum ($longest,$max_pix);
			$x = $pic_w/$divnum;
			$y = $pic_h/$divnum;
		}
	}
	
	
	echo &quote;<img src=\&quote;$pic\&quote; width=\&quote;$x\&quote; height=\&quote;$y\&quote;>&quote;;
 
 
 	 
 
}

function PHP_Picture_Resize_Get_DivitionNum ($x,$to_lenght)
{
	
	$divnum= $x/$to_lenght;

	return $divnum;
}
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Re: getimagsize is veeery slooow

Post by malcolmboston »

asterinex wrote:Hi,
I first used the getimagsize function to scale my pictures properly. But this function is so slow I think it is useless.
dont know what code you have written but id seriouslty consider rewriting it, getimagesize is very fast for me so shouldnt be a problem, my image gallery parses in less than 0.1 seconds this includes automatic generation of thumbnails including cacheing, getimagesize calls, File System "queries", HTML output for layout of gallery etc
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

You have nothing defining the full picture size!

You are only defining a thumbnail size and function, there for only creating one image on the server.

Code: Select all

$size = GetImageSize( $images_dir."/".$filename );

        $Config_tbwidth_wide = 80; // width of wide image
        $Config_tbheight_wide = 80; // height of wide image

        $Config_tbwidth_tall = 80; // width of tall image
        $Config_tbheight_tall = 80; // height of tall image

// Create thumbnail code
        
		if($size[0] > $size[1]){
            $thumbnail_width = $Config_tbwidth_wide;
            $thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);

            if($thumbnail_height > $Config_tbheight_wide){
                $thumbnail_height = $Config_tbheight_wide;
                $thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
            }
        }else{
            $thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
            $thumbnail_height = $Config_tbheight_tall;

            if($thumbnail_width > $Config_tbwidth_tall){
                $thumbnail_width = $Config_tbwidth_tall;
                $thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
            }
        }

$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;

$source_handle = $function_to_read ( $images_dir."/".$filename );

if($source_handle){

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

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

$function_to_write( $destination_handle, $images_dir."/tb_".$filename, 100 );
ImageDestroy($destination_handle );

// Resize full size image

if ($size[0] > '500'){
        $Config_width_wide = 500; // width of wide image
        $Config_height_wide = 500; // height of wide image

        $Config_width_tall = 500; // width of tall image
        $Config_height_tall = 500; // height of tall image

        if($size[0] > $size[1]){
            $image_width = $Config_width_wide;
            $image_height = (int)($Config_width_wide * $size[1] / $size[0]);

            if($image_height > $Config_height_wide){
                $image_height = $Config_height_wide;
                $image_width = (int)($Config_height_wide * $size[0] / $size[1]);
            }
        }else{
            $image_width = (int)($Config_height_tall * $size[0] / $size[1]);
            $image_height = $Config_height_tall;

            if($image_width > $Config_width_tall){
                $image_width = $Config_width_tall;
                $image_height = (int)($Config_width_tall * $size[1] / $size[0]);
            }
        }

$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;

$source_handle = $function_to_read ( $images_dir."/".$filename );

if($source_handle){

$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );

ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}

$function_to_write( $destination_handle, $images_dir."/".$filename, 90 );
ImageDestroy($destination_handle );

}

$result_final .= <img src='".$images_dir. "/tb_".$filename."' /><br>File ".($counter+1)." Added Successfully";

}
}
$counter++;
}
bob
Post Reply