PHPackages                             ahmadmayahi/php-google-vision - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. ahmadmayahi/php-google-vision

ActiveLibrary[API Development](/categories/api)

ahmadmayahi/php-google-vision
=============================

An elegant wrapper around Google Vision

v1.0.0(4y ago)274651MITPHPPHP ^8.0CI failing

Since Oct 22Pushed 4y ago2 watchersCompare

[ Source](https://github.com/ahmadmayahi/php-google-vision)[ Packagist](https://packagist.org/packages/ahmadmayahi/php-google-vision)[ Docs](https://github.com/ahmadmayahi/php-google-vision)[ GitHub Sponsors](https://github.com/ahmadmayahi)[ RSS](/packages/ahmadmayahi-php-google-vision/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

 [![PHP Google Vision](https://raw.githubusercontent.com/ahmadmayahi/php-google-vision/main/art/php-google-vision.png)](https://raw.githubusercontent.com/ahmadmayahi/php-google-vision/main/art/php-google-vision.png)---

[![Latest Version on Packagist](https://camo.githubusercontent.com/72de2abdeb2a19a7667cd557eb0e2a1b7f934f86a426d23517150a5cb2349e7c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61686d61646d61796168692f7068702d676f6f676c652d766973696f6e2e737667)](https://packagist.org/packages/ahmadmayahi/php-google-vision)[![Total Downloads](https://camo.githubusercontent.com/03d7eac8b1e504647ed75c4f56523457d2497a9b9f922b0faee611163e82dd33/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61686d61646d61796168692f7068702d676f6f676c652d766973696f6e2e737667)](https://packagist.org/packages/ahmadmayahi/php-google-vision)[![Tests](https://github.com/ahmadmayahi/php-google-vision/actions/workflows/run-tests.yml/badge.svg)](https://github.com/ahmadmayahi/php-google-vision/actions/workflows/run-tests.yml)[![Check & fix styling](https://github.com/ahmadmayahi/php-google-vision/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/ahmadmayahi/php-google-vision/actions/workflows/php-cs-fixer.yml)
[![Test Coverage](https://camo.githubusercontent.com/315b5454700e526b72b15d53f72f36f92ad5afc1b4f1d0b0032501883b0660ea/68747470733a2f2f636f6465636f762e696f2f67682f61686d61646d61796168692f7068702d676f6f676c652d766973696f6e2f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d6861794d796e38744c49)](https://codecov.io/gh/ahmadmayahi/php-google-vision)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b711d62300c864b71e11e3aae899cffd5ee041b27fda801f1168760d911d5a34/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f61686d61646d61796168692f7068702d676f6f676c652d766973696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/ahmadmayahi/php-google-vision/?branch=main)[![Code Intelligence Status](https://camo.githubusercontent.com/d1b70113405d326b48115e39b9f8de0e850cb19c09e7aa5cfccd6bdaa9e9f884/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f61686d61646d61796168692f7068702d676f6f676c652d766973696f6e2f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d61696e)](https://scrutinizer-ci.com/code-intelligence)

---

**Requires PHP 8.0+**

For feedback, please [contact me](https://form.jotform.com/201892949858375).

This package provides an elegant wrapper around [Google Vision API](https://github.com/googleapis/google-cloud-php-vision) and more.

It's an effort to make Google Vision API easy and fun to work with.

Contents
========

[](#contents)

- [Installation](#installation)
- [Creating Google Service Account](#creating-google-service-account)
- [Configuration](#configuration)
- [Original Responses](#original-responses)
- [Integration with Laravel](#integration-with-laravel)
- [Image Text Detection (OCR)](#image-text-detection-ocr)
    - [Get Plain Text](#get-plain-text)
    - [Get Document](#get-document)
    - [Handwriting](#handwriting)
    - [PDF and Tiff](#pdf-and-tiff)
- [Crop Hints Detection](#crop-hints-detection)
    - [Detect Crop Hints](#detect-crop-hints)
    - [Draw box around hints](#draw-box-around-hints)
    - [Crop Image](#crop-image)
- [Face Detection](#face-detection)
    - [Draw box around faces](#draw-box-around-faces)
- [Detect Image Properties](#image-properties-detection)
- [Label Detection](#label-detection)
- [Landmark Detection](#landmark-detection)
- [Logo Detection](#logo-detection)
- [Object Localizer](#object-localizer)
    - [Detect Objects](#detect-objects)
    - [Draw Box Around Objects](#draw-box-around-objects)
    - [Draw Box Around Objects With Text](#draw-box-around-objects-with-text)
- [Web Detection](#web-detection)

Installation
------------

[](#installation)

You may install the package via composer:

```
composer require ahmadmayahi/php-google-vision
```

Creating Google Service Account
-------------------------------

[](#creating-google-service-account)

First, you must [create a Google service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts) and setup the configuration object.

Configuration
-------------

[](#configuration)

```
use AhmadMayahi\Vision\Config;

$config = (new Config())
    // Required: path to your google service account.
    ->setCredentials('path/to/google-service-account.json')

    // Optional: defaults to `sys_get_temp_dir()`
    ->setTempDirPath('/my/tmp');
```

Original Responses
------------------

[](#original-responses)

All the features come with `getOriginalResponse()` method which returns the original response that's returned by [PHP Google Vision Package](https://github.com/googleapis/google-cloud-php-vision):

```
use AhmadMayahi\Vision\Vision;

$response = Vision::init($config)
    ->file('/path/to/input/file.jpg')
    ->faceDetection()
    ->getOriginalResponse();
```

The `file()` method accepts the following types:

- Local file path: `path/to/your/file`.
- Google Storage path: `gs://path/to/file`.
- File resource, such as `fopen()`.
- `SplFileInfo`.
- `SplFileObject`.

Integration with Laravel
------------------------

[](#integration-with-laravel)

Open up the `AppServiceProvider` and add the following lines:

```
use AhmadMayahi\Vision\Vision;
use AhmadMayahi\Vision\Config;

public function register()
{
    $this->app->singleton(Vision::class, function ($app) {
        $config = (new Config())
            ->setCredentials(config('vision.service_account_path'));

        return Vision::init($config);
    });
}
```

Using Dependency Injection:

```
use AhmadMayahi\Vision\Vision;
use Illuminate\Http\Request;

class FaceDetectionController
{
    public function detect(Request $request, Vision $vision)
    {
        $vision = $vision
            ->file($request->face_file->path())
            ->faceDetection()
            ->detect();

        // ...
    }
}
```

You may also resolve the object using the `app` helper as follows:

```
use AhmadMayahi\Vision\Vision;

/** @var Vision $vision */
$vision = app(Vision::class);

$result = $vision
    ->file('path/to/file')
    ->faceDetection()
    ->detect();

// ...
```

Image Text Detection (OCR)
--------------------------

[](#image-text-detection-ocr)

### Get plain text

[](#get-plain-text)

The `plain()` method returns an object of type `AhmadMayahi\Vision\Data\ImageText`.

```
use AhmadMayahi\Vision\Vision;

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

if ($response) {
    $response->locale; // locale, for example "en"
    $response->text;   // Image text
}
```

Both `plain()` and `document()` methods return `null` if no text will be detected.

You may also get the plain text using `__toString()` magic method:

```
echo $response;
```

### Get Document

[](#get-document)

The `getDocument` returns an object of type `AhmadMayahi\Vision\Data\ImageText`.

```
use AhmadMayahi\Vision\Vision;

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

if ($response) {
    $response->locale; // locale, for example "en" for English
    $response->text;   // Image text
}
```

> The difference between `plain()` and `docuemnt()` is that the first one only retrieves the plain text (no bullets, signs, etc...), whereas the latter one tries to retrieve the entire document (including bullets, symbols, etc...).

### Handwriting

[](#handwriting)

The `document` method can also be used to detect handwriting in an image.

### PDF and Tiff

[](#pdf-and-tiff)

Coming soon.

Crop Hints Detection
--------------------

[](#crop-hints-detection)

[Crop Hints](https://cloud.google.com/vision/docs/detecting-crop-hints) suggests vertices for a crop region on an image.

### Detect Crop Hints

[](#detect-crop-hints)

```
use AhmadMayahi\Vision\Vision;

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

/** @var \AhmadMayahi\Vision\Data\CropHints $item */
foreach ($response as $item) {
    $item->bounds; // An array of \AhmadMayahi\Vision\Data\Vertex

    $item->confidence;

    $item->importanceFraction;
}
```

### Draw box around hints

[](#draw-box-around-hints)

You may use the `drawBoxAroundHints` method as follows:

```
use AhmadMayahi\Vision\Vision;
use AhmadMayahi\Vision\Enums\Color;

Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->cropHintsDetection()
    ->drawBoxAroundHints(Color::GREEN)
    ->toJpeg('out.jpg')
```

[![Draw box around hints](files/output/larry-and-sergey-crop-hints-draw-box-around-hints.jpg)](files/output/larry-and-sergey-crop-hints-draw-box-around-hints.jpg)

### Crop Image

[](#crop-image)

You may export the cropped image as follows:

```
use AhmadMayahi\Vision\Vision;

$response = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->cropHintsDetection()
    ->crop()
    ->toJpeg('out.jpg');
```

Original Image:

[![Original Image](files/input/larry-and-sergey.jpg)](files/input/larry-and-sergey.jpg)

Cropped Image:

[![Cropped Image](files/output/larry-and-sergey-cropped.jpg)](files/output/larry-and-sergey-cropped.jpg)

Face Detection
--------------

[](#face-detection)

[Face Detection](https://cloud.google.com/vision/docs/detecting-faces) detects multiple faces within an image along with the associated key facial attributes such as emotional state or `wearing headwear`.

The `detect` method returns a `Generator` of `AhmadMayahi\Vision\Data\Face`:

```
use AhmadMayahi\Vision\Vision;

$vision = Vision::init($config);

$faces = $vision
    ->file('/path/to/image.jpg')
    ->faceDetection()
    ->detect();

echo count($faces). ' faces found';

/** @var \AhmadMayahi\Vision\Data\Face $faceData */
foreach ($faces as $faceData) {
    $faceData->anger; // for example: POSSIBLE
    $faceData->isAngry(); // boolean

    $faceData->surprise;
    $faceData->isSurprised();

    $faceData->joy;
    $faceData->isJoyful();

    $faceData->blurred;
    $faceData->isBlurred();

    $faceData->headwear;
    $faceData->isHeadwear();

    $faceData->landmarking;

    $faceData->underExposed;

    $faceData->detectionConfidence;

    $faceData->bounds;
}
```

The `anger`, `surprise` and `joy` etc... return likelihoods ratings which are expressed as six different values:

- `UNKNOWN`.
- `VERY_UNLIKELY`.
- `UNLIKELY`.
- `POSSIBLE`.
- `LIKELY`.
- `VERY_LIKELY`.

> See [Likelihood](https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#likelihood).

You may get the results as array:

```
$faces = $vision
    ->file('/path/to/image.jpg')
    ->faceDetection()
    ->asArray();
```

Or as JSON:

```
$faces = $vision
    ->file('/path/to/image.jpg')
    ->faceDetection()
    ->asJson();
```

> `asArray` and `asJson` is supported in all the features that return `Generator`.

### Draw box around faces

[](#draw-box-around-faces)

```
use AhmadMayahi\Vision\Vision;
use AhmadMayahi\Vision\Enums\Color;

$analyzer = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->faceDetection()
    ->drawBoxAroundFaces(Color::MAGENTA)
    // Alternatively, you may use `toPng`, `toGif`, `toBmp` methods.
    ->toJpeg('faces.jpg');
```

> All the drawing methods return an object of type `AhmadMayahi\Vision\Support\Image`.

[![Larry Page and Sergey Brin Faces](files/output/larry-and-sergey.jpg)](files/output/larry-and-sergey.jpg)

> This feature doesn't support Google Storage yet.

Image Properties Detection
--------------------------

[](#image-properties-detection)

The [Image Properties](https://cloud.google.com/vision/docs/detecting-properties) feature detects general attributes of the image, such as dominant color.

The `detect` method returns a `Generator` of `AhmadMayahi\Vision\Data\ImageProperties`:

```
use AhmadMayahi\Vision\Vision;

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

/** @var \AhmadMayahi\Vision\Data\ImageProperties $item */
foreach ($properties as $item) {
    $item->red;

    $item->blue;

    $item->green;

    $item->pixelFraction;
}
```

Landmark Detection
------------------

[](#landmark-detection)

[Landmark Detection](https://cloud.google.com/vision/docs/detecting-landmarks) detects popular natural and human-made structures within an image.

```
use AhmadMayahi\Vision\Vision;

$landmarks = Vision::init($config)
    ->file('/path/to/baghdad.jpg')
    ->landmarkDetection()
    ->detect();

/** @var \AhmadMayahi\Vision\Data\Landmark $landmark */
foreach ($landmarks as $landmark) {
    $landmark->name;

    // An array containing the detected locations in latitude/longitude format.
    $landmark->locations;
}
```

Safe Search Detection
---------------------

[](#safe-search-detection)

[SafeSearch Detection](https://cloud.google.com/vision/docs/detecting-safe-search) detects explicit content such as adult content or violent content within an image.

The `detect` method returns an object of type `AhmadMayahi\Vision\Data\SafeSearch`:

```
use AhmadMayahi\Vision\Vision;

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

$result->adult;
$result->isAdult(); // boolean

$result->medical;
$result->isMedical(); // boolean

$result->violence;
$result->isViolence(); // boolean

$result->racy;
$result->isRacy(); // boolean

$result->spoof;
$result->isSpoof(); // boolean
```

Label Detection
---------------

[](#label-detection)

[Detect](https://cloud.google.com/vision/docs/labels) and extract information about entities in an image, across a broad group of categories.

The `detect` method returns an a `Generator` of labels:

```
use AhmadMayahi\Vision\Vision;

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

Logo Detection
--------------

[](#logo-detection)

[Detect](https://cloud.google.com/vision/docs/detecting-logos) and extract information about entities in an image, across a broad group of categories.

The `detect` method returns an `Generator` of logos:

```
use AhmadMayahi\Vision\Vision;

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

Object Localizer
----------------

[](#object-localizer)

[Object Localizer](https://cloud.google.com/vision/docs/object-localizer) detects and extract multiple objects in an image with Object Localization.

### Detect Objects

[](#detect-objects)

The `detect` method returns a `Generator` of `AhmadMayahi\Vision\Data\LocalizedObjectData`:

```
use AhmadMayahi\Vision\Vision;

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

/** @var AhmadMayahi\Vision\Data\LocalizedObject $obj */
foreach ($objects as $obj) {
    $obj->name;

    $obj->languageCode;

    $obj->mid;

    $obj->normalizedVertices;

    $obj->score;
}
```

### Draw Box Around Objects

[](#draw-box-around-objects)

You may draw box around objects using the `drawBoxAroundObjects` method:

```
use AhmadMayahi\Vision\Vision;
use AhmadMayahi\Vision\Enums\Color;

$objects = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->objectLocalizer()
    ->drawBoxAroundObjects()
    ->boxColor(Color::GREEN)
    ->toJpeg('out.jpg');
```

[![Larry Page and Sergey Brin faces](files/output/larry-and-sergey-objects.jpg)](files/output/larry-and-sergey-objects.jpg)

The `drawBoxAroundObjects()` takes an optional`callback` as a second parameter:

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

$objects = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->objectLocalizer()
    ->drawBoxAroundObjects()
    ->boxColor(Color::RED)
    ->callback(function(Image $outputImage, LocalizedObject $object) {
        // Get GD Image
        $outputImage->getImage();

        // Get object info
        $object->getName();
    })
    ->draw();
```

> This feature doesn't support Google Storage yet.

### Draw Box Around Objects With Text

[](#draw-box-around-objects-with-text)

You may want to draw box around objects and include the object's text as well:

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

$objects = Vision::init($config)
    ->file('/path/to/input/image.jpg')
    ->objectLocalizer()
    ->drawBoxAroundObjectsWithText()
    ->boxColor(Color::GREEN)
    ->textColor(Color::RED)
    ->font(Font::OPEN_SANS_BOLD_ITALIC)
    ->fontSize(12)
    ->draw()
    ->toJpeg('output.jpg');
```

[![Larry Page and Sergey Brin Objects](files/output/larry-and-sergey-objects-text.jpg)](files/output/larry-and-sergey-objects-text.jpg)

> This feature doesn't support Google Storage yet.

Web Detection
-------------

[](#web-detection)

[Web Detection](https://cloud.google.com/vision/docs/detecting-web) detects Web references to an image.

```
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;
```

The `detect` method returns either an object of tupe `AhmadMayahi\Vision\Data\WebData` or `null` value.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Ahmad Mayahi](https://github.com/ahmadmayahi)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~15 days

Total

2

Last Release

1691d ago

Major Versions

v0.9 → v1.0.02021-11-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/fdd47a6fbc9b1cf5c5fb2f85520e535b73c29ce99b5c2ac640ddac3fc9a3e987?d=identicon)[ahmad.mayahi](/maintainers/ahmad.mayahi)

---

Top Contributors

[![ahmadmayahi](https://avatars.githubusercontent.com/u/1689910?v=4)](https://github.com/ahmadmayahi "ahmadmayahi (120 commits)")

---

Tags

google-visiongoogle-vision-apiphp-ocraiOCRgoogle vision

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ahmadmayahi-php-google-vision/health.svg)

```
[![Health](https://phpackages.com/badges/ahmadmayahi-php-google-vision/health.svg)](https://phpackages.com/packages/ahmadmayahi-php-google-vision)
```

###  Alternatives

[wordpress/mcp-adapter

Adapter for Abilities API, letting WordPress abilities to be used as MCP tools, resources or prompts

1.3k124.0k4](/packages/wordpress-mcp-adapter)[deepseek-php/deepseek-php-client

deepseek PHP client is a robust and community-driven PHP client library for seamless integration with the Deepseek API, offering efficient access to advanced AI and data processing capabilities.

46784.5k5](/packages/deepseek-php-deepseek-php-client)[gemini-api-php/client

API client for Google's Gemini API

224255.2k5](/packages/gemini-api-php-client)[moe-mizrak/laravel-openrouter

Laravel package for OpenRouter (A unified interface for LLMs)

157156.6k2](/packages/moe-mizrak-laravel-openrouter)[wordpress/php-ai-client

A provider agnostic PHP AI client SDK to communicate with any generative AI models of various capabilities using a uniform API.

28245.9k23](/packages/wordpress-php-ai-client)[mozex/anthropic-laravel

Laravel integration for the Anthropic API: facade, config publishing, install command, testing fakes, messages, streaming, tool use, thinking, and batches.

72287.1k1](/packages/mozex-anthropic-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
