0

The API documentation shows this example:

$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';
$content = $driveService->files->export($fileId,
  'application/pdf',
  array('alt' => 'media')
);

How do I show the image in my HTML?

(I've already figured out how authorization with the Google API works.)

  • What is the resulting response that you get from your API call? If you get the raw file data, you could embed the image in an img tag as base 64 encoded data. [Perhaps this might help.](http://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html) Otherwise it could get complicated. – segFault Oct 04 '16 at 03:37
  • I get an object. [Here's the contents of that object.](http://codepaste.net/evrvos) I don't know how to extract the raw file data from that object. – Kalle Kanon Oct 04 '16 at 11:35
  • Since it is returning a GuzzleHttp Response object, you can access the content like: `$content->getBody()->getContents();` (Assuming `$content` is the variable you are you using) – segFault Oct 04 '16 at 12:15

1 Answers1

0

You can embed the raw image data using base64_encode

Your API call is returning a GuzzleHttp Response object, so you can get the raw data by calling:

$rawData = $response->getBody()->getContents();
// Alternatively, you should be able to cast it to a string (I would try the first method then this alternative if you want)
$rawData = (string) $response->getBody();

To embed that raw data as an html <img /> you can do something like:

// Convert to base64 encoding
$imageData = base64_encode($rawData);

// Get the content-type of the image from the response
$contentType = $response->getHeader("content-type");

// Format the image src:  data:{mime};base64,{data}
$src = 'data: '.$contentType.';base64,'.$imageData;

// Echo out a sample image
echo '<img src="'.$src.'" />';

Source

Community
  • 1
  • 1
segFault
  • 3,887
  • 1
  • 19
  • 31
  • 1
    Worked brilliantly, thank you! The getHeader() method returns an array (with only one element), not a string, so you'll get a PHP notice. – Kalle Kanon Oct 04 '16 at 13:32