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.