如何从桶中获取图像以将其预览给用户 [英] How to get the image from bucket to preview it to the user

查看:302
本文介绍了如何从桶中获取图像以将其预览给用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Google云端存储,我正尝试裁剪并上传图片。在这里我已经上传了图片并将其提取回来进行剪裁。我使用了以下方法:

方法1:

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

使用这些文档。但在这个我没有找到课堂。例如,我尝试了包含文件:

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

$ options = ['size'=> 400,'crop'=>真正];
$ image_file =gs://my_bucket/shiva.jpg;
$ cloud_tools =新的CloudStorageTools;
$ img = $ cloud_tools-> getImageServingUrl($ image_file,$ options);

但是我没有找到类。

 使用google\appengine\CreateEncodedGoogleStorageKeyRequest; 

ans等我检查了下面的 CreateEncodedGoogleStorageKeyRequest appengine文件夹。我发现它在那里失踪。我不知道发生了什么。

方法2:
我尝试使用以下代码上传文件。 p>

  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_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); $!
$ b while(!$ status&&!feof($ handle)){
$ chunk = fread($ handle,$ chunkSizeBytes);
$ status = $ media-> nextChunk($ chunk);
}

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

fclose($ handle);
//重置到客户端以在将来立即执行请求。
$ client-> setDefer(false);
返回true;

}



我成功上传使用上述代码的图像,但现在停留在获取图像,并在html中预览它。 (我想裁剪图像然后再上传)。为此,我尝试了以下方法:

方法一:

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

使用这些文档。其中我在我的html中获得一个登录框,填写我的Google凭据并重定向到图像。但不要在我的html代码中获得图像预览。



方法b:
我试过

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

使用这些文档。但是我得到了输出:

  {
error:{
errors:[
{
domain:global,
reason:required,
message:匿名用户没有storage.objects.get访问对象my_bucket /biva.jpg,
locationType:header,
location:Authorization
}
],
code:401 ,
message:匿名用户没有storage.objects.get访问对象my_bucket / shiva.jpg。
}
}

方法c:
我使用下面的函数来试用它:

 函数get_user_image($ image_file){
$ instance =& amp ; 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'发生错误。
}
}

使用这些文档

我前三天被困住了。我需要在html中将图像显示给用户。请任何人指导我什么我错过?请给出正确的方法来完成这项工作。

解决方案

由于您对这些匿名显示的对象感到满意,因此最简单的解决方案他们在网站上的图像只是将它们标记为可公开访问,然后将它们嵌入到HTML中,如下所示:

 << ; IMG SRC =https://storage.googleapis.com/BUCKET_NAME/imageName.jp eg/> 


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.

解决方案

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆