Search code examples
phpimage-processingmysqliphp-gd

How to store php imagejpg() result in mysql database


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.


Solution

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