PHP: Show JPG from Binary

一世执手 提交于 2019-12-01 11:15:32

问题


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.

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.

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?

Thanks in advance.

P.S. I would provide the binary but its just too big to put on here.

EDIT: (added some code)

<?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);
?>

回答1:


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);

?>



回答2:


Try $im = imagecreatefromstring($data); The output it by imagejpeg($im);



来源:https://stackoverflow.com/questions/18949663/php-show-jpg-from-binary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!