Jump to content

Convert BLOB image then save to file.


piznac

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.