This one time, at band camp, Kevin Waterson <[EMAIL PROTECTED]> wrote:

> I need to store the thumbnails in the database.
> So, I need to resize the image at the same time as I store it.

Ok, so the secret here is output buffering

         // prepare the image for insertion
         $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
         // do not use addslashes yet
        $thumbData = $_FILES['userfile']['tmp_name'];
        // get the image info..
        $size = getimagesize($_FILES['userfile']['tmp_name']);
        // database connection
        mysql_connect("localhost", "$dbusername", "$dbpassword") OR DIE 
(mysql_error());
        // select the db
        mysql_select_db ($dbname) OR DIE ("Unable to select db".mysql_error());

        // create the thumbnail
        $height   = '50';         // the height of the thumbnail
        $width    = '50';         // the width of the thumbnail
        // snarf the thumbdata
        $src = ImageCreateFromjpeg($thumbData);
        // create the thumbnail
        $destImage = ImageCreateTrueColor($height, $width);
        // copy the resize/resampled image to the destImage
        ImageCopyResampled($destImage, $src, 0,0,0,0, $width, $height, $size[0], 
$size[1]);
        // start the output buffering
        ob_start();
        // pretend to send the image to the browser
        imageJPEG($destImage);
        // stick the output buffer in a variable
        $photo_thumb = ob_get_contents();
        // now we can addslashes for inserting into the db
        $photo_thumb = addslashes($photo_thumb);
        // tidy up a little
        ob_end_clean();

       $sql = "INSERT INTO phototable ( photo_id, photo_cat, photo_type ,photo_thumb, 
photo_data, photo_size, photo_name) VALUES ('', 'cat', '{$size['mime']}', 
'{$photo_thumb}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";
                                                                                       
                              
        // insert the image
       mysql_query($sql)

Easy as that! You do not need to create the file on the filesystem as some have 
suggested,
or use ImageCreateFromString() and pull the original out of the db. It can all be done 
in
one dastardly sweep.

Thanks to all
Kevin

-- 
 ______                              
(_____ \                             
 _____) )  ____   ____   ____   ____ 
|  ____/  / _  ) / _  | / ___) / _  )
| |      ( (/ / ( ( | |( (___ ( (/ / 
|_|       \____) \_||_| \____) \____)
Kevin Waterson
Port Macquarie, Australia

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to