I am looking for a way to convert an image base64
to varbinary
a through PHP
.
I am trying to convert it as follows by doing a successful insert in MSSQL
:
CONVERT(VARBINARY(MAX),$_POST(['imgBase64']) //Un trazo cualquiera
But when trying to encode it to show it again as an image, it appears as if it doesn't exist and the result base64_encode($imagen)
is totally different from the original...
Entrada: iVBORw0K...kJggg==
varbinary almacenado: 0x6956424F52773...545726B4A6767673D3D
Salida: aVZCT1J3MEtHZ2...JrSmdnZz09
Other images already stored in the database are displayed correctly, but it does not work with the new ones that I am trying to insert.
What is the correct way to do the conversion from base64
to varbinary
?
Thanks to @Triby's guidance I managed to solve it...
I receive the image in
base64
from a formHTML
and apply the functionbase64_decode()
to itPHP
, then I pass the variable as a parameter in the functionCONVERT
...MSSQL
Something similar to this:In this way, the record is correctly inserted in the database and when I apply the function
base64_encode($respuesta['IMAGEN'])
, the image obtained from the type field of the databasePHP
is correctly displayed in the browser .VARBINARY