Question

Am dealing with webservices in my project. i am not sure about the bankend logic like what kind of method they used to store images in database.

This is the response i got as my profile image from server

Array
(
 [0] => -119
 [1] => 80
 [2] => 78
 [3] => 71
 [4] => 13
 [5] => 10
 [6] => 26
 .
 .
 .
 [3778] => 123
 [3779] => 60
 [3780] => -82
 [3781] => -57
 [3782] =
)

How do convert this numerical array into PNG Image?

I have created a new image file and put all contents into that file using

file_put_contents('downloads/myImage.png', $profileImage);

$profileImage variable contains the above array.

this code creates a new image with some dump of datas. I know i have missed something by doing this. please help me with this.

i have used this same code to create PDF files from the server in this same project. that's working fine. thats why i tried with the same code to get my image. But its not working with images

Was it helpful?

Solution

Your question is a little bit unclear — I can't tell if you want to save the PNG file somewhere or deliver it as the response to an HTTP request. Also, you seem to imply that $profileImage is an array variable, but provide it as an argument to file_put_contents(), which expects a string.

But either way, the binary data you are being provided with looks fine.

If $profileImage is an array of signed byte values, then you can convert it to a string of binary data with the following command"

$rawPNG = implode(array_map('chr',$profileImage));

If you need to, you can then use file_put_contents() to write this data to a file. (The function is binary-safe, so there shouldn't be any problems.)

Alternatively, if you want to return a PNG image to a web client, just echo it with an appropriate Content-Type header:

header('Content-Type: image/png');
die ($rawPNG);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top