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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址