1

Good day

I'm trying to show an image using DATA URIs and encoding a string in base64 as shown below:

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

The image (JPG) is stored in the database in this way:

structure of the table

stored image

The problem is that using that DATA URI, it does not show the image on the screen. I don't know the error or if I have to do something different to be able to show that stored image.

HTML source code

Image not created :(

Is there any other way to display these images stored in SQL Server?

Thank you very much for your time.

5
  • You should stop using the image datatype. It has been deprecated for well over a decade now in favor of varbinary(max). I can't help you with php though. Commented Mar 1, 2018 at 19:33
  • 1
    Are you sure the value in db is base64? I don't think so Commented Mar 1, 2018 at 19:35
  • Thank you very much @SeanLange, unfortunately I can not make changes in the structure of the database, and I wanted to know if there is any approach to solve that problem. Commented Mar 1, 2018 at 19:52
  • @Fr0zenFyr In fact, the only thing I know is that it is a jpg file stored in the database Commented Mar 1, 2018 at 19:54
  • 1
    It is stored as a byte array. I have no idea how you handle that in php but surely there are thousands of example of this already. Commented Mar 1, 2018 at 19:54

1 Answer 1

1

The data in db is not base64, you need to convert this hexadecimal data to base64. Php, as I know doesn't have a direct function to do that but it can be achieved with pack(). I leave it to you as homework to study this function.

Convert the data from db to base64 like:

$fFoto = base64_encode(pack('H*', $foto)); Then pass this $fFoto to image src like you are already doing.

<IMG src="data:image/jpeg; base64, <?php echo $fFoto; ?>" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.