PHP get image from byte array

PHP get image from byte array

Tags

So in previous blog post we talked about saving image to byte array in order to pass it to backend SOAP service. But how about retrieving such image from backend?

Well the logic is similar, so considering you have the byte array returned from SOAP, you will create new php file named image.php:

<?php 
   //Here write your code to get $byte_array
    $data = base64_decode($byte_array);
    $im = imagecreatefromstring($data);
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im); 
?>

We decoded the byte array, and then using imagecreatefromstring() which will actually create image resource. In order to display that resource you can use imagejpeg() or imagepng() functions. At the end we destroy the image resource so it doesn't waste memory on server. So to display the image in the page you could use something like: 

<img src="/image.php"/>