CodeIgniter Forums
Image convert into base64 and store in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Image convert into base64 and store in database (/showthread.php?tid=70846)



Image convert into base64 and store in database - ktmonty - 06-07-2018

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.
Code:
if ( ! $this->upload->do_upload('course_profile_image'))
            {
                
                error($this->upload->display_errors(),TRUE,400);
            }else{
                $data = array('upload_data' => $this->upload->data());
                $filename=$data['upload_data']['file_name'];
                $pathinfo = 'dist/img/courseimage/'.$filename;
                $filetype = pathinfo($pathinfo, PATHINFO_EXTENSION);
                $filecontent = file_get_contents($pathinfo);
                
                try{
                    $base64=rtrim(base64_encode($filecontent));
                                    
                }catch(Exception $e){
                    error($e,TRUE,855);
                    
                    
                }
                
                $image = 'data:image/' . $filetype . ';base64,' . $base64;



RE: Image convert into base64 and store in database - InsiteFX - 06-07-2018

You should use the BLOB data type.


RE: Image convert into base64 and store in database - dave friend - 06-07-2018

(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.


RE: Image convert into base64 and store in database - InsiteFX - 06-07-2018

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.


RE: Image convert into base64 and store in database - CINewb - 06-18-2018

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.


RE: Image convert into base64 and store in database - InsiteFX - 06-18-2018

Most of us just store the image filename in the database not the image itself.


RE: Image convert into base64 and store in database - php_rocs - 06-18-2018

@ktmonty,

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: https://stackoverflow.com/questions/25196910/storing-images-in-a-database-versus-a-filesystem

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