2

我为 Google Glass 开发了一个 Mirror API 应用程序,但我被困在一个非常基本的事情上。我想保存时间线项目中的图像。

require_once 'config.php';
require_once 'mirror-client.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
require_once 'util.php';
require 'facebook-php-sdk/src/facebook.php';

if($_SERVER['REQUEST_METHOD'] != "POST") {
  http_send_status(400);
  exit();
}

// Parse the request body
$body = http_get_request_body();
$request = json_decode($body, true);


// A notification has come in. If there's an attached photo, bounce it back
// to the user
$user_id = $request['userToken'];
$access_token = get_credentials($user_id);

$client = get_google_api_client();
$client->setAccessToken($access_token);

// A glass service for interacting with the Mirror API
$mirror_service = new Google_MirrorService($client);

//Save image to file
$itemId = $request['itemId'];
$timeLineItem = $mirror_service->timeline->get($itemId);
$request = new Google_HttpRequest($timeLineItem['attachments'][0]['contentUrl'], 'GET',         null, null);
$httpRequest = Google_Client::$io->authenticatedRequest($request);
if ($httpRequest->getResponseHttpCode() == 200) {
    $image = $httpRequest->getResponseBody();
    imagejpeg($image, 'test.jpg');
  } else {
    // An error occurred.
    die('This sucks! '. $httpRequest->getResponseBody());
  }

我不断收到此错误: PHP 警告:imagejpeg() 期望参数 1 是资源

我是 php 新手,所以恐怕我什至不在正确的轨道上。我究竟做错了什么?

4

1 回答 1

4

的第一个参数imagejpeg应该是使用“imagecreatetruecolor()”创建的资源http://www.php.net/manual/en/function.imagecreatetruecolor.php

当 $httpRequest->getResponseBody() 返回图像的二进制内容时,您可以将其保存为:file_put_contents('test.jpg', $httpRequest->getResponseBody());

当您的数据使用 MIME base64 编码时: file_put_contents('test.jpg', base64_decode($httpRequest->getResponseBody()));

于 2013-05-05T23:43:55.303 回答