0

I am trying to make the profile view of user using image from byte array. I have successfully rendered the image, but I have problem including the image with other profile content, because of the header('Content-Type: image/png') which takes the whole file as an image. How to resolve this problem?

$myArray = implode('', array_map(function($e) {
    return pack("C*", $e);
}, $image));
if      (substr($myArray, 0, 4) == "\x89PNG") header('Content-Type: image/png');
else if (substr($myArray, 0, 2) == "\xFF\xD8") header('Content-Type: image/jpeg');
else if (substr($myArray, 0, 4) == "GIF8") header('Content-Type: image/gif');
echo $myArray;

1 Answer 1

1

You can't serve raw image data as part of an HTML document.

There are a couple of ways you can go about serving the image:

  1. Base64 encode it and embed it in the img src. See this thread for an example.
  2. Save the image to a file and serve it like you would any other image.

#2 had the advantage of allowing the image to be cached by the browser. If the images are large I would try to work out a way to save it to a file.

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.