Get the blob image and convert it to base64 string?

I have stored an image in MySQL as a Blob. When i try to get the image with eloquent, i get the following error:

Malformed UTF-8 characters, possibly incorrectly encoded

i tried first:

 $_Reservation->image = json_encode(base64_encode($_Reservation->image));

After that, i tried to return is directly but with the same error.
When i remove the image, it’s working fine so the encoding of the table shouldn’t be the issue.

Can someone kindly help me with this issue?

I even converted the image manually to blob and put it as : base64_encode(“image in binary”) but i still get that error.

Hello @ali.h2030

To avoid problems with binary data, try to save it base64-encoded.
What are you using for a datatype in your database?
Check the database connection parameters.

Hi odan,
I am using mediumblob.
I uploaded the image from the database manager so it should be uploaded correctly.
I don’t see any questions about blobs in this forum and all the questions regarding blobs are very old so it might not be the best option.

Maybe i better store them normally in a folder in the server but now i have the naming and fetching challenge.
Sorry that i opened this topic. It might be best to remove it if we are not going to continue with this method.

If you want my advice, don’t save files to the database. The file system is better suited for this. I would combine it with a table for all files that stores the references to the files. But of course this depends on many factors depending on the individual application.

That is the common way. I already implemented it now.
I thought of trying a different way since images are small and it would be easier to manage if you have the actual data instead of reference.
I used the reservation ID which is unique by itself by just in case anything is reset or something, i added the datetime now to the image name.

Thanks for the support.

1 Like