2013-05-05 40 views
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

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()));

+0

非常感謝! – theJosh 2013-05-06 04:42:28