我需要将图像存储到MSSQL数据库中,作为由PHP处理的表单处理的一部分。
在此之前,我的客户使用以下C#代码完成了此任务:
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
我试图通过基于http://php.net/manual/en/function.mssql-query.php#31688的unpack()函数复制它
// 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'] );
但是,他无法恢复它。老实说,我也不知道如何用PHP还原它-将标头更改为Image不能解决问题。
// view the packed file
header("Content-type: image/jpeg;");
echo $unpacked['hex'];
您能帮忙吗?我以前从未做过此事,很乐意解决问题。