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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
resize image before saving to mysql as blob

Posted by jonbond (jonbond), 2 May 2007
hello,

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:

Code:
$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
                                         
                                         fclose($fp);
                                         // 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
                                         $thumb=imagecreatetruecolor($imgw,$imgh);                  
                                         // 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:

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 www.opentalk.org.uk 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)
1 unpublished comment pending on this page

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

You can Add a comment or ranking or edit your own comments
There are presently 1 new comment(s) pending publication on this page

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho