Как загрузить вложение с изображением из элемента временной шкалы?


Я разрабатываю приложение Mirror API для Google Glass, и я застрял на очень фундаментальной вещи. Я хочу сохранить изображение из элемента временной шкалы.

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, поэтому боюсь, что я даже не на правильном пути. Что я делаю не так?

Author: theJosh, 2013-05-06

1 answers

Первым параметром 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()));

 4
Author: Bass Jobsen, 2013-05-05 23:43:55