• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image convert into base64 and store in database

#1
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;
Reply

#2
You should use the BLOB data type.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(06-07-2018, 06: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.
Reply

#4
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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
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.
Reply

#6
Most of us just store the image filename in the database not the image itself.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
@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/2519...filesystem

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.