Working with Cloud Vision API from PHP

I have been very excited by the Cloud Vision API recently put into Beta by Google Cloud Platform. I haven’t had a chance to play with it much, and I wanted to fool around with it from PHP on App Engine (or vanilla PHP for that matter), but there is no documentation for PHP yet.

So here is a little primer on how to do this from PHP on App Engine.

First you have to complete a few prerequisites:

Once you do this you’re ready to start developing. Because I am running PHP on App Engine I want the App Engine SDK for PHP.

I’m going to use the GUI to run this app, but you can use the command line just as easily.


The first thing I need to do is write a php.ini that properly allows use of cURL and has a good limit on uploaded files.

Then I set up a page named creds.php to hold my API key for Cloud Storage and my Cloud Storage Bucket name.

Then I create a form page named index.php that creates an App Engine Upload URL for me. (If I wanted to not use App Engine, I could just skip the call to Cloud Storage Tools and post directly to the next file in the example: process.php.)

Then process.php does the hard work of taking the uploaded file, converting it to base64 and uploading to the Cloud Vision API.

Finally I have to create an app.yaml to serve up the two pages.

Use GoogleAppEngineLauncher to start your app.

You should get this.


Assuming you upload a picture from the top of the Eiffel Tower looking at the Champs de Mars, you’ll get something like this:


There you go, bare bones but simple Cloud Vision example in PHP.

If you want to dig deeper into the Cloud Vision API you can

The code for all of this is available on GitHub

12 thoughts on “Working with Cloud Vision API from PHP

  1. hi great tutoriel but i have this error :
    “responses”: [
    “error”: {
    “code”: 3,
    “message”: “image-annotator::Malformed request.: Image processing error!”

  2. Hi Terrence,

    Thanks for this post it helped me a lot. I tried your code for getting the dominant colors of an image and it worked for me but with some minor issues. There are colors that are obviously in the image but weren’t returned by the API response. So I thought that by just increasing the value of the maxResults parameters it will eventually include most of the colors in the API response but it didn’t. No matter what the value I set on the parameter, it always returns only 10 colors in the response (even if I even set it to 1) and no matter what image i pass as parameter. I am not sure if there is something with google’s imageProperty functionality that caused the issue. I just want to confirm if you or anyone from your readers have experienced it too.

  3. Hi there! Thanks you for the guiding, but I got a question… with this code is possible to do an image recognition from a image path? If that’s possible how would be the code?

    Thanks for texting me back 🙂

  4. Would this work with an image being uploaded from Android? I’ve just modified the code to workaround with that, but I’m getting a bad request code 400.

Leave a Reply

Your email address will not be published. Required fields are marked *