1

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
  • @Dagon Done... Not sure how much help that is. Commented Sep 22, 2013 at 23:15

2 Answers 2

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

?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

You likely need to convert the binary string to a char string using a function like $data = unpack('H',$data); prior to creating the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.