ukscotth Posted July 15, 2009 Share Posted July 15, 2009 Hi, I currently run a busy social networking site and im trying to change the software it runs off. Ive managed to transfer all the user data and the forum data but im stuck on transferring the pictures. The main problem im having is that on the old site they are stored in a folder on the server but on the new site they are stored in the database itself. heres the database structures for both sites : OLD SITE picture_id picture_username picture_type picture_path picture_label picture_status adult_status reason gallery 1 KellyIOW 1 pictures/193242254_nightclub_girls.jpg 1 0 ungrouped NEW SITE userid filedata dateline filename visible filesize width height filedata_thumb width_thumb height_thumb 30 [bLOB - 1.6 KB] 1247656853 me.jpg 1 1604 80 64 [bLOB - 1.4 KB] 60 48 I think Id need to do something like this : $fh = fopen("MyImg.jpg", "r"); $data = addslashes(fread($fh, filesize("MyImg.jpg"))); fclose($fh); // Create the query $SQL = " INSERT INTO Image(ImgTitle, ImgType, ImgData) VALUES('My Image', 'jpg', '$data')"; // Execute the query $RESULT = mysql_query($SQL) or die("Couldn't insert image"); But im unsure how to work out the right heights and widths for the images and the thumbnails Can anyone help plzzzz ? Thanks, Scott. Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted July 15, 2009 Share Posted July 15, 2009 Scott, You'll need to have php 'read' the binary content of the image and then store that content in a BLOB database column. It's also handy to store the original file name, file size and mime type. $filename = "test.jpg"; $content = file_get_contents($filename); $size = filesize($filename); Quote Link to comment Share on other sites More sharing options...
ukscotth Posted July 15, 2009 Author Share Posted July 15, 2009 thanks ryan. So would I do something like this ? $filename = "test.jpg"; $content = file_get_contents($filename); $size = filesize($filename); mysql_query("INSERT INTO customavatar (`userid`, `filedata`, `filename`, `filesize`, `filedata_thumb`) VALUES ('20', '".$content."', '".$filename."','".$size."','".$content."')"); Quote Link to comment Share on other sites More sharing options...
ukscotth Posted July 15, 2009 Author Share Posted July 15, 2009 Ok, Ive got it to work sort of using this : $fh = fopen("test.jpg", "r"); $data = addslashes(fread($fh, filesize("test.jpg"))); fclose($fh); // Create the query $SQL = " INSERT INTO customavatar(userid, filedata, filename,filedata_thumb) VALUES('13','$data','test.jpg','$data')"; // Execute the query $RESULT = mysql_query($SQL) or die("Couldn't insert image"); Not sure how to get the dimensions and create the thumbnail tho Quote Link to comment Share on other sites More sharing options...
ukscotth Posted July 15, 2009 Author Share Posted July 15, 2009 worked out how to get the dimensions, just stuck on the thumbnail part Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted July 15, 2009 Share Posted July 15, 2009 <?php function makeThumbs( $pathToImages, $pathToThumbs, $thumbWidth ) { $dir = opendir($pathToImages); while (false !== ($fname = readdir($dir))) { $info = pathinfo($pathToImages . $fname); if (strtolower($info['extension']) == 'jpg') { echo "Creating thumbnail for $fname <br />"; $img = imagecreatefromjpeg($pathToImages . $fname); $width = imagesx($img); $height = imagesy($img); $new_width = $thumbWidth; $new_height = floor($height * ($thumbWidth / $width)); $tmp_img = imagecreatetruecolor($new_width, $new_height); imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}"); } } closedir($dir); } makeThumbs("upload/","upload/thumbs/",100); ?> Quote Link to comment Share on other sites More sharing options...
ukscotth Posted July 15, 2009 Author Share Posted July 15, 2009 Thanks Ryan. So I should convert all the images to thumbnails first and then use the same way of coverting them to binary strings as before and then insert them into the database ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.