Tuesday, June 7, 2016

Dynamic Image Resize using google cloud storage(GCS) and getServing URL

you need to have 
Google cloud storage bucket name
and
following Code



Simply just use my GIT repo as follows  just change your projectID and bucket name 

USE this git repo

https://github.com/koolkarni/resize-image-google-cloud-service-GetServingURL-PHP.git

OR

index.php:


<?php
//var_dump($_FILES['uploaded_files']['tmp_name']);
syslog(LOG_WARNING, "Request came");
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;
syslog(LOG_WARNING, "Imported Cloud Storage Tools");
//var_dump( $_GET);
$object_url=$_GET["image"];
$size=intval($_GET["size"]);
syslog(LOG_WARNING, "Object URL $object_url");
syslog(LOG_WARNING, "Size $size");

$bucket="gs://YOUR-PROJECT-ID.appspot.com/bucket_name/";
$object_image_url = CloudStorageTools::getImageServingUrl($object_url,['size' => $size, 'crop' => false]);
syslog(LOG_WARNING, "Output Url $object_image_url");
header("location: $object_image_url");

closelog();
?>



app.yaml:

runtime: php55
api_version: 1

handlers:
- url: /.*
script: index.php



upload these files in GIT hub repo 

once you have the code in git download it into your google cloud development-->Repo  by following the steps below:


Step1















 Step2





























Step3














Step4













Step5


























Step6











Once your code is in repo. open the GC console then 
run the following command in GC shell

 go to the project dir (i.e the folder with projectID)

gcloud source repos clone image1 --project=Prj1234


where image1: is your repo name Prj1234 is your project code

now traverse inside your image1 folder then follow the following steps


gcloud preview app deploy app.yaml







to check your output hit the URL like this where image1.jpg is file from 
your cloud storage































2 comments:

  1. ( ! ) Fatal error: Class 'google\appengine\api\cloud_storage\CloudStorageTools' not found in E:\wamp\www\php-test\firebase\Firebase-php\php\quickstart2.php on line 16



    Hi i have getting this error please guide me something wrong with my code. i have no idea about that

    ReplyDelete
  2. ERROR: (gcloud) Invalid choice: 'preview'.
    Usage: gcloud [optional flags]
    group may be alpha | app | auth | beta | bigtable | builds |
    components | composer | compute | config | container |
    dataflow | dataproc | datastore | debug |
    deployment-manager | dns | domains | endpoints |
    firebase | functions | iam | iot | kms | logging | ml |
    ml-engine | organizations | projects | pubsub | redis |
    services | source | spanner | sql | topic
    command may be docker | feedback | help | info | init | version
    For detailed information on this command and its flags, run:
    gcloud --help

    ReplyDelete