← Back to blog

How to use Google Vision in PHP efficiently

| PHP

Google Vision provides image analysis services based on AI.

It offers PHP support through the official google-cloud-php-vision package, but the amount of code required to analyze a single image is excessive.

For example, using the web detection feature requires something like this:

$imageAnnotator = new ImageAnnotatorClient();

# Annotate the image
$image = file_get_contents($path);
$response = $imageAnnotator->webDetection($image);
$web = $response->getWebDetection();

// Print best guess labels
printf('%d best guess labels found' . PHP_EOL,
    count($web->getBestGuessLabels()));
foreach ($web->getBestGuessLabels() as $label) {
    printf('Best guess label: %s' . PHP_EOL, $label->getLabel());
}
print(PHP_EOL);

// Print pages with matching images
printf('%d pages with matching images found' . PHP_EOL,
    count($web->getPagesWithMatchingImages()));
foreach ($web->getPagesWithMatchingImages() as $page) {
    printf('URL: %s' . PHP_EOL, $page->getUrl());
}
print(PHP_EOL);

// Print full matching images
printf('%d full matching images found' . PHP_EOL,
    count($web->getFullMatchingImages()));
foreach ($web->getFullMatchingImages() as $fullMatchingImage) {
    printf('URL: %s' . PHP_EOL, $fullMatchingImage->getUrl());
}
print(PHP_EOL);

// Print partial matching images
printf('%d partial matching images found' . PHP_EOL,
    count($web->getPartialMatchingImages()));
foreach ($web->getPartialMatchingImages() as $partialMatchingImage) {
    printf('URL: %s' . PHP_EOL, $partialMatchingImage->getUrl());
}
print(PHP_EOL);

// Print visually similar images
printf('%d visually similar images found' . PHP_EOL,
    count($web->getVisuallySimilarImages()));
foreach ($web->getVisuallySimilarImages() as $visuallySimilarImage) {
    printf('URL: %s' . PHP_EOL, $visuallySimilarImage->getUrl());
}
print(PHP_EOL);

// Print web entities
printf('%d web entities found' . PHP_EOL,
    count($web->getWebEntities()));
foreach ($web->getWebEntities() as $entity) {
    printf('Description: %s, Score %s' . PHP_EOL,
        $entity->getDescription(),
        $entity->getScore());
}

This example was taken from php-docs-sample.

On top of that, testing this code requires mocking many objects, which is a painful process.

That is why I created a wrapper around Google Vision API that eliminates the boilerplate.

PHP Google Vision is an elegant wrapper around Google Vision API. The goal is to make Google Vision easy and clean to work with.

Here is the same web detection feature using the wrapper:

use AhmadMayahi\Vision\Vision;
use AhmadMayahi\Vision\Enums\Color;
use AhmadMayahi\Vision\Enums\Font;
use AhmadMayahi\Vision\Support\Image;
use AhmadMayahi\Vision\Data\LocalizedObject;

$response = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->webDetection()
    ->detect();

$response->fullMatchingImages;

$response->partialMatchingImages;

$response->bestGuessLabels;;

$response->pagesWithMatchingImages;

$response->visuallySimilarImages;

$response->webEntities;

Install the package via composer:

composer require ahmadmayahi/php-google-vision

You also need to create a service account through your Google Cloud account.

If you are not familiar with service accounts, visit the Google Cloud documentation.

For more information about the package, visit GitHub.

Summary

  • Google Vision's official PHP package -- requires too much boilerplate code for simple image analysis tasks.
  • PHP Google Vision wrapper -- reduces the code to a fluent, readable interface while keeping the same functionality.
  • Testing -- the wrapper makes mocking straightforward instead of requiring you to mock deeply nested objects.
  • Service account -- required for authentication; create one through Google Cloud IAM.
Share