0

I'm trying to show a Microsoft Graph image on a php website. I receive the image from graph, but its not in the correct format.

The result is like ����JFIF``��C and so on

The problem is that I receive an image as result and not a json. But i'm not able to display the image with the code above.

> HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: image/jpeg
ETag: "BB3972DD"
request-id: 1d3edddb-4278-43ac-be53-4a60861c7703
client-request-id: 1d3edddb-4278-43ac-be53-4a60861c7703
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Europe","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_2","ADSiteName":"DUB"}}
Duration: 176.1934
Date: Wed, 28 Feb 2018 18:35:52 GMT
����JFIF``��C��C����"�� 
���}!1A

Thanks to the help of this community I was able to solve my problem. My working code is:

$ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://graph.microsoft.com/v1.0/me/photo/%24value',
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => null,
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER => array(
            'authorization: Bearer '.ipDb()->selectValue('user','hash',array('id' => ipUser()->userId())),
            'content-type: image/jpeg; charset=utf-8'
        ) ,
    ));
    $response = curl_exec($ch);
    // Close the cURL resource, and free     system resources
    curl_close($ch);
    $returndata = 'photo <img src="data:image/jpeg;base64,' . base64_encode($response) . '"/>';
    return $returndata;
  • Possible duplicate of [How to convert microsoft graph 365 Users Profile Photos from binary data to a readable image format](https://stackoverflow.com/questions/47614068/how-to-convert-microsoft-graph-365-users-profile-photos-from-binary-data-to-a-re) – Marc LaFleur Feb 28 '18 at 17:04
  • Thanks for the suggestion, but I already tried the information on that topic before posting the question. I updated my question with this info. – Tim Vanden Hende Feb 28 '18 at 17:18
  • Are you encoding the `$`? Also, you need to convert the binary image to base64 before you can pass it as a data url (it's also a `jpg` rather than a `gif`). – Marc LaFleur Feb 28 '18 at 17:47
  • I tested some things and I updated my question. The problem is I receive no json but an jpeg from the endpoint. – Tim Vanden Hende Feb 28 '18 at 18:42
  • 1
    This API request doesn't return JSON - it returns jpeg by design and per the HTTP Content-Type header. See https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/profilephoto_get. So your code should expect to get back binary data, and not JSON. – Dan Kershaw - MSFT Mar 01 '18 at 05:48
  • When i change to request header "'content-type: image/jpeg; charset=utf-8'" the problem is still the same. I'm still not able to convert te response to an image in php. Not with and not without base64_encode of my response. – Tim Vanden Hende Mar 01 '18 at 09:11

1 Answers1

1

Since the $value URL returns the image binary, the line

    $returndata = 'photo <img src="data:image/jpeg;base64,' . base64_encode($res1['@odata.context']) . '"/>';

should not use $res1['@odata.context'] but instead $response directly.

So base64_encode($response) should be the last part of the img src tag.

RasmusW
  • 3,355
  • 3
  • 28
  • 46