1

A database has stored an image as binary. I'm looking to convert it back to a JPEG in PHP. Does anyone know how this might be accomplished?

I'm aware this might be something that I can find through Google but I have not yet tracked down an answer and thought it would be best to hedge my bets and ask a question here.

If I find out before I get a response, I will post the solution here.

4
  • Do you mean be able to manipulate it with PHP? Otherwise, it's already a JPEG, if you save it with fopen specifying the binary flag, it should render properly. Commented May 7, 2012 at 14:24
  • Sounds like it's just a matter of doing a database request and file_put_contents() to send the data back into a file. Or is there some stumbling stone you didn't mention? What does "binary" mean for you? Commented May 7, 2012 at 14:25
  • "Binary" is meaningless. Any format is binary. Commented May 7, 2012 at 14:30
  • The deal was that the database creators stored the image as "binary" info in the database rather than point to a spot on the server. Regarding this being meaningless, I'm not sure what other term I would use. I have read the term "blob" when searching online... Commented May 7, 2012 at 18:17

1 Answer 1

3

Simply select the data from the database and put it in the variable $image_data_from_database

Save to file:

<?php
    file_put_contents("image.jpg", $image_data_from_database);
?>

or display the image in-place:

<?php
    header('Content-Type: image/jpeg');
    echo $image_data_from_database;
?>
Sign up to request clarification or add additional context in comments.

Comments

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.