duanni5726 2017-02-14 10: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 17: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" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog