CodeIgniter Forums

Full Version: Image convert into base64 and store in database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have try to convert image into base64 but its not showing image.when i fetch it from database( it works with smaller images like 400X300. but not with large dimension images  ). which datatype should i use to store this data.
if ( ! $this->upload->do_upload('course_profile_image'))
                $data = array('upload_data' => $this->upload->data());
                $pathinfo = 'dist/img/courseimage/'.$filename;
                $filetype = pathinfo($pathinfo, PATHINFO_EXTENSION);
                $filecontent = file_get_contents($pathinfo);
                }catch(Exception $e){
                $image = 'data:image/' . $filetype . ';base64,' . $base64;
You should use the BLOB data type.
(06-07-2018, 05:24 AM)InsiteFX Wrote: [ -> ]You should use the BLOB data type.

Or, perhaps MEDIUMBLOB if you want to store encoded images greater than 65,535 characters.
Keep in mind that Base64-encoded data takes about 33% more space than the original data.
TINYBLOB, TINYTEXT - maximum length of 255 characters.
BLOB, TEXT - maximum length of 65535 characters.
MEDIUMBLOB, MEDIUMTEXT - maximum length of 16777215 characters.
LONGBLOB, LONGTEXT - maximum length of 4294967295 characters.
I know this doesn't answer the question, but is there a good reason why you want to store images in the database? I considered this a long time ago and never went down that route, and I'm glad I didn't. It makes it so much harder to access the images directly if you need to, uses more space than storing the images in the file system, makes it potentially more difficult to hand them off to a content delivery network, and prevents you from storing the images on a file server separate to your database server if ever you need to.
Most of us just store the image filename in the database not the image itself.

While I prefer not to store an image in the database here is an answer from StackOverflow that I thought was fitting for this thread:

P.S. Despite the time frame of the answer it still is relevant today.