piznac Posted July 9, 2012 Share Posted July 9, 2012 Hi! I got stuck with a project utilizing a DB full of images in a BLOB format. It's a basic shopping cart and I looped through the images with something like this: <img src="data:image/jpeg;base64,<?php echo base64_encode($content); ?>" width="100" /> Where $content equals the Blob data. As I thought, this is laggy as can be(trying to loop and re size is a nightmare slow.). So I want to write a script to convert them all to images and then save to file. And then of course write a line to a new table to use(since I always use the link in DB and image in file system technique, I have no knowledge on this Blob thing). I have tried many things, however I can't seem to get any images to save as actual files(There seems to be tons of articles on saving as Blob and re displaying image, but not on saving it back to file.). Sadly my pc just crashed so I don't have any of the code where I tried. I don't need it written, just a nudge in the write direction I can write everything up until the actual save to this directory portion. Just need that nudge with that part. Thanks for anyone's time for this!! **Edit - Just noticed my sig. None of that data is correct at this point. I can provide that data if needed. php 5.XX of course. Quote Link to comment https://forums.phpfreaks.com/topic/265453-convert-blob-image-then-save-to-file/ Share on other sites More sharing options...
Adam Posted July 9, 2012 Share Posted July 9, 2012 file_put_contents is probably the easiest way to write to files. Given the data is already in binary format, you just need to write it to a file with a generated name. Can't really comment on how you will generate file names, given we don't know enough about the data. Essentially though all you need to do is write a script that will first create a new column for the file name, then loop through each record; writing the data to a file and storing the new file name back into the database, then at the end assuming all went well you can drop the column. Probably goes without saying it would be a wise idea to back-up the data before you run anything. Quote Link to comment https://forums.phpfreaks.com/topic/265453-convert-blob-image-then-save-to-file/#findComment-1360406 Share on other sites More sharing options...
piznac Posted July 9, 2012 Author Share Posted July 9, 2012 Wow, holy crap I could have sworn I tried that. But a quick script later and now it worked fine. public function testImageConvert() { $query = $this->db->query("SELECT Picture FROM `ItemPicture` LIMIT 1"); if($query->num_rows() > 0) { $row = $query->row(); file_put_contents('test.jpg',$row->Picture); } return false; } Thanks for the help!!! - I must have done something strange last time I tried this. Quote Link to comment https://forums.phpfreaks.com/topic/265453-convert-blob-image-then-save-to-file/#findComment-1360425 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.