PHP convert image to byte array
Recently I worked on integration between Drupal CMS with backend SOAP service and encountered a problem with saving image to the backend. The backend required image to be passed as byte array. Googling around I was suprised to find out answers like "I don't think PHP has such low level control..." or "change your backend service...". Eventually after not finding anything useful I decided to tackle the issue myself and soon I came up with a solution which is actually very easy.
So to convert image to byte array you will have to read the image using file_get_contents() and then base encode it using base64_encode before sending it to backend SOAP service:
<?php $client = new SoapClient('http://yoursoapservice.com'); $byte_array = file_get_contents('myprofilepic.jpg'); $image = base64_encode($byte_array); $ap_param['userId'] = $uid; $ap_param['image'] = $image; $arrayRes = $client->UploadImage($ap_param); //UploadImage is name of my SOAP function, yours might be different ?>