1

I have resized the image that I received from the user using the below code:

$imagedata = getimagesize($_FILES['uploadedimage']['tmp_name']);
            $newwidth = $imagedata['0']/3;
            $newheight = $imagedata['1']/3; 
            $thumb = imagecreatetruecolor($newwidth, $newheight);
            $source = imagecreatefromjpeg($_FILES['uploadedimage']['tmp_name']);
            // Resize
            imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $imagedata['0'], $imagedata['1']);

            // Output and free memory
            ob_start();
            imagejpeg( $thumb );
            echo '<img src="data:image/jpeg;base64,' . base64_encode(ob_get_clean()) . '">';
            imagedestroy($thumb);

now I am using this line

echo '<img src="data:image/jpeg;base64,' . base64_encode(ob_get_clean()) . '">';

to display the image but instead, I want to store it in my database. I am not able to figure it out. I have searched in many forums and have failed to find any answer. Please help.

  • 1
    You are not include the db and insert query here. How should it insert? – Anand Pandey Aug 05 '17 at 12:46
  • @AnandPandey yes that code is there I haven't added it here and I know to how make a query and INSERT it into DB. The point here is how do I store it into DB as in what value do I sent in values. "INSERT INTO db (image) VALUES (????)"; – Zeeshan Ahmed Aug 05 '17 at 19:32

3 Answers3

1

You shouldn’t store the image, what you can do is store the images in a known path and then store the unique id or name in the db, so then to retrieve you look for the id in the db and construct the path again.

Is it a good practice to save a base64 string on the Database?

Joaquin Peraza
  • 343
  • 1
  • 15
  • 1
    I don't want to store it into DB as base64 that is the only way I have found on the web to convert it to viewable format through research. I want to know how can I store the result from imagejpeg() in MySQL DB as blob data type. – Zeeshan Ahmed Aug 05 '17 at 20:22
  • That's ok, have a look at this question maybe it help, https://stackoverflow.com/questions/7052655/insert-blobs-in-mysql-databases-with-php – Joaquin Peraza Aug 05 '17 at 20:24
  • thanks but that is not my question. Thanks for the help though. if by any mean I am unclear through my question please let me know. I will try to describe it better. – Zeeshan Ahmed Aug 05 '17 at 20:38
0

I have fixed this by adding this line of code where echo is written:

$image = addslashes(ob_get_contents());

Now $image can be send normally to DB using query.

0

You don't store blobs unless you really, really have to.

Would you rather backup every day...

  • a 100MB database and 50GB of files (differentially, using rsync)
  • or a 50GB SQL database?
bobflux
  • 11,123
  • 3
  • 27
  • 27