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;
$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;
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;
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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.