I need to store images into an MSSQL database as part of a form processing which is handled by PHP.
Before that, my client did this task with following C# code:
Dim content As Byte() = ImageToStream(fName)
cnn.Open()
Dim cmd As New SqlCommand("UPDATE lide SET pictPostava = @img WHERE ID = '" & GetValueToTextBox(iRow, "ID") & "'", cnn)
cmd.Parameters.AddWithValue("@img", content)
cmd.ExecuteNonQuery()
cnn.Close()
content = Nothing
Public Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return stream.ToArray()
End Function
I tried to replicate it via unpack() function based on http://php.net/manual/en/function.mssql-query.php#31688
// convert file
$file = file_get_contents('http://www.sunagency.cz/wp-content/uploads/2017/01/1745083-150x150.jpg');
$unpacked = "0x" . unpack("H*hex", $file);
// get the hexcode of image
print_r( $unpacked['hex'] );
However, he can't restore it. And to be honest, I'm not sure how to revert it back with PHP either - changing header into Image doesn't solve the problem.
// view the packed file
header("Content-type: image/jpeg;");
echo $unpacked['hex'];
Could you help? I have never done this before and would be more than happy to solve the problem.