PHP:从Binary显示JPG [英] PHP: Show JPG from Binary
问题描述
我有一个存储在外部数据库中的jpg blob,我希望通过php将其显示为图像。问题是每当我将 Content-Type
设置为 image / jpeg
并回显blob时,我得到了破碎的图像浏览它时的图标。
I have a jpg blob thats been stored in an external DB and I'm looking to display that as an image via php. The issue is whenever I set the Content-Type
to image/jpeg
and echo out the blob I get the broken image icon when browsing to it.
我尝试通过sublime从头开始制作文件,当我将其保存为十六进制文件时可以正常工作,因此我知道数据有效。
I have tried making the file from scratch via sublime and that works when I save it as a hexadecimal file so I know the data is valid.
我试过让脚本创建一个文件但它设置了 charset = us-ascii
所以它赢了被视为图像文件。
I have tried making the script create a file but it sets the charset=us-ascii
so it won't get seen as a image file.
有没有人对原始图像二进制文件有任何经验?有谁知道如何显示图像甚至将其保存到文件中?
Does anyone have any experience with raw image binary files? anyone know how I can display the image or even save it out to a file?
提前致谢。
PS我会提供二进制文件,但它太大而无法放在这里。
P.S. I would provide the binary but its just too big to put on here.
(添加了一些代码)
<?php
header('Content-Type: image/jpeg;');
$data = 'some long string of hex';
// tried echoing it directly..
echo $data;
// and writing to a file...
file_put_contents('test.jpg', $data);
?>
推荐答案
继续研究后我发现这篇文章PHP:从HEX字符串创建文件
After continuing research I found this post PHP: create file from an HEX string
使用以下代码修复了问题。
With the following code I fixed the issue.
<?php
header('Content-Type: image/jpeg;');
$data = 'The hex data';
$data = pack('H*',$data);
$im = imagecreatefromstring($data);
imagejpeg($im);
?>
这篇关于PHP:从Binary显示JPG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!