Page 1 of 1

thumbnailing AND setting limit on size of fullsize pic

Posted: Thu Jan 20, 2005 12:55 am
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

Posted: Thu Jan 20, 2005 1:05 am
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"?

Posted: Thu Jan 20, 2005 1:42 am
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?

Posted: Thu Jan 20, 2005 1:53 am
by feyd
okay.. .. so what actually happens right now?

Posted: Thu Jan 20, 2005 2:01 am
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?

Posted: Thu Jan 20, 2005 2:10 am
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.

Posted: Thu Jan 20, 2005 2:21 am
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?

Posted: Thu Jan 20, 2005 2:41 am
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.

Posted: Thu Jan 20, 2005 1:06 pm
by decoy1
okay thanks