PHP code example of ahmadmayahi / php-google-vision

1. Go to this page and download the library: Download ahmadmayahi/php-google-vision library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ahmadmayahi / php-google-vision example snippets


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');

use AhmadMayahi\Vision\Vision;

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

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);
    });
}

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();
            
        // ...
    }
}

use AhmadMayahi\Vision\Vision;

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

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

// ...

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
}

echo $response;

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
}

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

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')

use AhmadMayahi\Vision\Vision;

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

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

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

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

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');

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

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

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

use AhmadMayahi\Vision\Vision;

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

use AhmadMayahi\Vision\Vision;

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

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

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');

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();

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');

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;
bash
composer