How do I turn a BLOB-type file into an image using PHP?

Asked

Viewed 2,822 times

2

I need to transform a BLOB file that comes from a MYSQL database to the Image type, to use on my site, I need to do the conversion using PHP, when I use the image in BLOB format, it goes to the entire site in binary.

1 answer

2


There are two ways, one is to save the binary in file (which is not interesting) and another is to add to srctag img informing that it is a binary. To do this, just add data:image/jpeg;base64, followed by binary code.

That is to say:

<img src="data:image/jpeg;base64,<?= base64_encode($binary) ?>" />

Depending on how you saved (in rare cases) the base64_encode may be omitted

  • Keep bringing me the binary image.

  • 1

    If binary code is appearing, there is something wrong with its implementation. Using the above code, if the image cannot be displayed, it should display the "broken image" icon and not the binary code. Make sure there is no quote that might be breaking the tag img

  • It’s like @Gabrielheming said, using the img tag you had to see at least one broken image. But there is a caveat, for his reply, you will use "image/jpeg" if you know what the image format is? also has to know if the data has not already been saved as Base64, in this case would not need to use the function base64_encode.

  • The photo is jpeg and was saved with the following Insert :Insert into FOTOS_PRODUTOS (id,image) values (3,LOAD_FILE('/opt/lampp/htdocs/mvp/images/Prod-top/terceira.jpg'))

  • Checked the generated HTML to check if there is no, or missing, character?

  • how can I do this conference?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.