How to include the image from byte array with other content in codeigniter?

414 Views Asked by At

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

There are 1 best solutions below

0
On

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.