Aaran McGuire
Aaran McGuire

Reputation: 3225

PHP: Show JPG from Binary

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

Upvotes: 1

Views: 10940

Answers (2)

Aaran McGuire
Aaran McGuire

Reputation: 3225

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

?>

Upvotes: 1

Wpigott
Wpigott

Reputation: 884

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

Upvotes: 0

Related Questions