2

I am trying to create a back-end for my mobile application. I am sending an image's contents as a byte array in the HTTP request's body (output stream) I want to read this stream of bytes in the PHP script and get back an image.

Can someone please tell me how I can do that?

Thanks.

2
  • Please be more specific. What does send the byte array, how is it encoded (if it is), where is it sent (in the http request ? Then it must be encoded), etc. If you could provide an exemple it would be easier to answer.
    – FWH
    Jul 23, 2009 at 6:41
  • Do you want to simply send it to a user? If that is the case just piping the bytes to output and setting the content type using header() will be sufficient. Or tell me what you want to do with the image in the script? Jul 23, 2009 at 6:51

1 Answer 1

8

You can obtain the request body by reading from php://input

$body = file_get_contents('php://input');

What you do with that data is up to you. You could write the data to a file with file_put_contents. As you mentioned it is image data, you might also be able to drop the data into an ImageMagick object with Imagick::readImageBlob. Another alternative is to use GD and load the image with imagecreatefromstring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.