duanni5726 2017-02-14 02:22
浏览 67
已采纳

如何从桶中获取图像以将其预览给用户

I am working on Google Cloud Storage in which I am trying to crop and upload an image. In this I've uploaded the image and fetching it back to crop it. I have used following methods to do so:

Method 1:

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

using these docs. But in this I get class not found. I tried including the file for example:

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

but the I get class not found for

use google\appengine\CreateEncodedGoogleStorageKeyRequest;

ans etc. I checked the CreateEncodedGoogleStorageKeyRequest under the appengine folder. I found it missing there. I don't know whats going on.

Method 2: I tried uploading the file using the following code.

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;

}

I got succeed in uploading the image using above code but now stuck in getting the image and previewing it in html. (I want to crop the image and then upload again). For which I tried following:

Method a:

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

using these docs. In which I get a login box in my html where I fill my Google credentials and get redirected to image. But don't get the image preview in my html code.

Method b: I tried

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

using these docs. But I get output :

    {
 "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."
 }
}

Method c: I tried it using the following function:

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.';
  }
}

using these docs

I am stuck fro last three days. I need to display the image to user in html. Please anyone guide me what am I missing? Please give the proper way to accomplish this.

展开全部

  • 写回答

1条回答 默认 最新

  • dqy27359 2017-02-15 09:34
    关注

    Since you are comfortable with these objects being anonymously visible, the easiest solution to display them as images on a website would be simply to mark them as publicly accessible and then to embed them in HTML like so:

    <IMG SRC="https://storage.googleapis.com/BUCKET_NAME/imageName.jp‌​eg" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部