Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.

See also [here] for status update
resize image before saving to mysql as blob

Posted by jonbond (jonbond), 2 May 2007

I have a web form that allows a user to upload a picture.  I want to resize this image to a 180 pixel heigth,  then store the thumbnail as a blob in mysql.  

Here is what I have so far, but unfortuneatly, this code only stores the address of the resized image to mysql:

$fileName = $_FILES['imageFile']['name'];
$tmpName  = $_FILES['imageFile']['tmp_name'];
$fileSize = $_FILES['imageFile']['size'];
$fileType = $_FILES['imageFile']['type'];

if ($fileName)
                                         $fp      = fopen($tmpName, 'r+');
                                         $content = fread($fp, filesize($tmpName)); //reads $fp, to end of file length
                                         // get originalsize of image
                                         $im = imagecreatefromstring($content);
                                         $width = imagesx($im);
                                         $height = imagesy($im);            
                                         // Set thumbnail-height to 180 pixels                                    
                                         $imgh = 180;                                          
                                         // calculate thumbnail-height from given width to maintain aspect ratio
                                         $imgw = $width / $height * $imgh;                                          
                                         // create new image using thumbnail-size
                                         // copy original image to thumbnail
                                         imagecopyresampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im)); //makes thumb
                                         //  Need help here  how do I put ImageJPEG($thumb) to mysql as a blob?
                                         $query = ("UPDATE user SET picture = '$thumb' WHERE userID = '$userID' ");
                                         $result = mysql_query($query) or die( "An error has ocured: " .mysql_error (). ":" .mysql_errno ());

How do I convert the resized image ($thumb) to a jpeg, and store it as a blob in mysql?  

Thank you for any help or advice, -jon

Posted by admin (Graham Ellis), 2 May 2007
As a quick fix, Jon, I would save the resampled image to a temportary file, read it back into a variable, and save that to the database.   I'm on the road at the moment - please feed back further if that doesn't point you in the right direction and I'll get back further in a day or two

Posted by jonbond (jonbond), 2 May 2007
Thanks for your quick reply Graham.  I tried your suggestion and now it is working with the following added code:

imagejpeg($thumb, "test.jpg", 80);  //imagejpeg($resampled, $fileName, $quality);            
                                         $instr = fopen("test.jpg","rb");  //need to move this to a safe directory
                                     $image = addslashes(fread($instr,filesize("test.jpg")));                        
                                         mysql_query("UPDATE `user` SET `picture`= ( \"".$image."\") WHERE `userID`= '$userID' ");

BTW, thanks for posting all your great examples.  This site is very resourcefull and much appreciated.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

Comment: "Thanks for this. ..."
Visitor Ranking 5.0 (5=excellent, 1=poor)

Comment by Anon (published 2011-02-18)
Thanks for this. [#3870]

You can Add a comment or ranking or edit your own comments

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho