2017-02-14 77 views
2

我正在使用Google雲端存儲進行裁剪並上傳圖像。在這裏我已經上傳了圖片並將其提取回來進行剪裁。我用下面的方法來做到這一點:如何從存儲桶中獲取圖像並將其預覽給用戶

方法1:

$options = ['gs_bucket_name' => $my_bucket]; 
$upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $options); 

使用these文檔。但在這個我沒有找到課堂。我試圖例如包括文件:

require_once APPPATH."google/appengine/api/cloud_storage/CloudStorageTools.php"; 

$options = ['size' => 400, 'crop' => true]; 
$image_file = "gs://my_bucket/shiva.jpg"; 
$cloud_tools = new CloudStorageTools; 
$img = $cloud_tools->getImageServingUrl($image_file, $options); 

但我得到的類沒有找到

use google\appengine\CreateEncodedGoogleStorageKeyRequest; 

ANS等我查了CreateEncodedGoogleStorageKeyRequest的AppEngine上文件夾下。我發現它在那裏失蹤。我不知道發生了什麼事。

方法2: 我嘗試使用以下代碼上傳文件。

function upload_user_image($image_file, $bucket_name = ''){ 
$client = google_set_client(); 
$storage = new Google_Service_Storage($client); 
$sfilename = $image_file['name']; //filename here 
$obj = new Google_Service_Storage_StorageObject(); 

$obj->setName($sfilename); 
$obj->setBucket("my_bucket"); //bucket name here 


$filen = $image_file['path']; 

$mimetype = mime_content_type($filen); 


$chunkSizeBytes = 1 * 1024 * 1024; 
$client->setDefer(true); 
$status = false; 

$filetoupload = array('name' => $sfilename, 'uploadType' => 'resumable'); 

$request = $storage->objects->insert("my_bucket",$obj,$filetoupload); 

$media = new Google_Http_MediaFileUpload($client, $request, $mimetype, null, true, $chunkSizeBytes); 
$media->setFileSize(filesize($filen)); 
$handle = fopen($filen, "rb"); 

while (!$status && !feof($handle)) { 
    $chunk = fread($handle, $chunkSizeBytes); 
    $status = $media->nextChunk($chunk); 
} 

$result = false; 
if($status != false) { 
    $result = $status; 
} 

fclose($handle); 
// Reset to the client to execute requests immediately in the future. 
$client->setDefer(false); 
return true; 

}

我在上傳使用上面的代碼圖像成功,但現在卡在獲取圖像和HTML預覽它。 (我想裁剪圖像然後再上傳)。對於這點我嘗試以下操作:

方法一:

$image = file_get_contents("https://storage.cloud.google.com/my_bucket/shiva.jpg"); 
echo $image; 

使用these docs。其中我在我的html中獲得一個登錄框,填寫我的Google憑據並重定向到圖像。但不要在我的HTML代碼中獲取圖像預覽。

方法b: 我使用these docs試圖

https://www.googleapis.com/storage/v1/b/my_bucket/o/shiva.jpg 

。但我得到的輸出:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Anonymous users does not have storage.objects.get access to object my_bucket/shiva.jpg.", 
    "locationType": "header", 
    "location": "Authorization" 
    } 
    ], 
    "code": 401, 
    "message": "Anonymous users does not have storage.objects.get access to object my_bucket/shiva.jpg." 
} 
} 

方法C: 我想它使用下面的函數:

function get_user_image($image_file){ 
    $instance = &get_instance(); 
    // $client = google_set_client(); 
    // $storage = new Google_Service_Storage($client); 
    $sfilename = $image_file; //filename here 

    $storage = new Google\Cloud\Storage\StorageClient(['projectId' => $instance->config->item('google_project_id')]); 
    $bucket = $storage->bucket('my_bucket'); 
    $object = $bucket->object($sfilename); 

    $stream = $object->downloadAsString(); 
    $im = imagecreatefromstring($stream); 
    if ($im !== false) { 
    header('Content-Type: image/png'); 
    imagepng($im); 
    imagedestroy($im); 
    } 
    else { 
    echo 'An error occurred.'; 
    } 
} 

使用these docs

我堅持來回最後三天。我需要在html中將圖像顯示給用戶。請任何人指導我什麼我錯過?請給出正確的方法來完成這一點。

+0

你用方法C得到了什麼錯誤? –

+0

嗨@BrandonYarbrough謝謝你的迴應。在方法C的情況下,我可以在瀏覽器窗口中顯示圖像,但不能使用html。在html中,我得到這個'圖像/文件無法顯示它包含錯誤'。而如果我在調用這個函數之後放了'exit()'。它顯示我的形象。 – MKB

+0

這些匿名可見的圖像?爲什麼只使用像''這樣的基本圖像? –

回答

1

既然你是舒適與這些對象是匿名可見,以顯示他們的網站上的圖像最簡單的解決辦法是簡單地將它們標記爲公開訪問,然後將它們嵌入到HTML中,像這樣:

<IMG SRC="https://storage.googleapis.com/BUCKET_NAME/imageName.jp‌​eg" /> 
+0

嗨@brandon現在我正嘗試通過裁剪將圖像上傳到存儲區。但是,當我嘗試上傳裁剪圖像時,我得到這個錯誤:「HTTP包裝不支持可寫連接」。我無法理解發生了什麼?爲什麼我得到這個錯誤。你能幫我解決這個錯誤嗎? – MKB

+0

這聽起來像是你正在使用帶有HTTP URL的file_put_contents? –

+0

不,我正在使用函數'imagejpeg($ file_content,$ destination_path)'。它給這個錯誤。目標路徑與我從雲存儲中獲取的路徑相同。 – MKB

相關問題