thumbnailing AND setting limit on size of fullsize pic

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
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

thumbnailing AND setting limit on size of fullsize pic

Post by decoy1 »

Hello,

I'm using the following snippet of code to accept an array (of pictures) from a form which automatically creates proportional thumbnails and automatically links the thumbs to the fullsize pics. Everything below works fine. You can see near the bottom how the prefix 'tb_' is automatically appended to the thumbs.

What I am having a brain fart about is how to proportionately resize the fullsize picture as well to set a limit on how big each picture will be.

Code: Select all

if($_REQUEST['action'] == 'photos_uploaded')
  {
    // 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 LIBRARY 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'];

    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($_REQUEST['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);

          // 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
          $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)
          {
            // CREATE AN BLANK IMAGE FOR THE THUMBNAIL
            $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]);
            ImageCopyResampled($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]);            
          }
      
        // 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++;
    }


Thanks for any help
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What I am having a brain fart about is how to proportionately resize the fullsize picture as well to set a limit on how big each picture will be.
It sure looks like your scaling code would work, however I'm unsure as to what you want to limit with "how big"?
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Hi,

I thought the same thing. Earlier I did the following...

$photos_uploaded = $_FILES['photo_filename']; <-- coming from form

$photos_uploaded = $_FILES['photo_filename'];
$photos_uploaded2 = $photos_uploaded; <-- original vars stored here

I then basically used the same code I posted with new maximum dimensions for the full size pics. In other words one right after the other, except the second loop takes $photos_uploaded2. Where am I going wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

okay.. .. so what actually happens right now?
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Nothing is happening. Nothing is breaking, but the full size pics that are uploaded are not being resized.

For this line where I'm saving the pic to a directory

Code: Select all

$function_to_write($destination_handle, $images_dir.'/tb_'.$filename);
I take out the 'tb_' string so it will just save normally, but it isn't doing it.

Does my approach to it sound correct? What would you do?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd add a bunch of debug prints.. you may be screwing up the file type checking.. print all your variables and make sure they are getting set correctly. Also make sure to check your error logs, if you have display_errors off. At the same time, ensure error_reporting is set to E_ALL.

I'd suggest not using the type passed by the browser/user.. as it could be screwy. For images, I always use getimagesize() to tell me what file type it is. Make sure you check if the return is strictly false, as it'll return that if the file isn't a recognized image type.
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

I am using getimagesize().

Knowing what I want and seeing the code I posted, how would you do it?
Is Germantown in Memphis?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, I know you are using getimagesize.. but it happens later.. and you aren't checking the return before assuming it's an array. What I'm saying I usually do is use getimagesize() almost exclusively for it. I completely ignore the type information sent by the browser. That's all I was saying about it..

Personally, I'd make sure the function pointer variables get set right, and start adding debug prints like:

Code: Select all

echo __FILE__ . '->' . __LINE__ . "\n";
all over.. see where it dies out.. and work from there..


yeah, Germantown is a suburb of Memphis, on the east side, just south of Cordova and west of Collierville.
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

okay thanks
Post Reply