Home >Backend Development >PHP Tutorial >PHP and Google Cloud Vision integration for image and visual data processing
PHP is a widely used open source server-side programming language. It is popular for 2D graphics processing and image rendering technology in website development. To implement processing of image and visual data, we can use Google Cloud Vision API along with PHP.
Google Cloud Vision API is a flexible computer vision API that helps developers build various machine vision applications more easily. It supports image tagging, facial recognition, text recognition, image search, logo recognition and other functions, and has a wide range of applications.
In this article, we will take a deep dive into how to use the Google Cloud Vision API in our PHP application to better handle our vision data and images.
Step 1: Create a Google Cloud Platform account and project
To use the Google Cloud Vision API, we need to create a Google Cloud Platform account and a project. You can visit the Google Cloud Console (https://console.cloud.google.com/) and create a new project. In the APIs and Services section of the console, enable the Google Cloud Vision API and create the appropriate credentials.
Step 2: Install the Google Cloud PHP client library
The Google Cloud PHP client library allows us to easily interact with Google Cloud's API. We can install the client library through the Composer package manager. Execute the following command for quick and easy installation:
$ composer require google/cloud
Step 3: Set Google Cloud Vision API Credentials
Before using the Google Cloud Vision API, we need to set the API credentials to perform in the cloud Authentication and authorization. We can set the API credentials using the following code:
<?php require __DIR__ . '/vendor/autoload.php'; use GoogleCloudVisionV1ImageAnnotatorClient; // 引入GCP凭据 putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json'); $client = new ImageAnnotatorClient(); ?>
Step 4: Call the Google Cloud Vision API
Before using the Google Cloud Vision API, we need to upload the image to be processed to Google Cloud Storage . We can upload files into Google Cloud Storage using PHP's file upload function.
<?php require __DIR__ . '/vendor/autoload.php'; use GoogleCloudStorageStorageClient; $storage = new StorageClient([ 'projectId' => 'my-project-id' ]); $bucketName = 'my-bucket-name'; $bucket = $storage->bucket($bucketName); $source = fopen('/path/to/local/image.jpg', 'r'); $bucket->upload($source, [ 'name' => 'image.jpg', ]); ?>
We can then call the Google Cloud Vision API to process this image. In order to prevent uploading files and processing images from consuming a lot of time each time, we can choose to store the processed image data in Google Cloud Datastore first, which can improve the speed and performance of image processing.
<?php require __DIR__ . '/vendor/autoload.php'; use GoogleCloudVisionV1ImageAnnotatorClient; use GoogleCloudDatastoreDatastoreClient; $datastore = new DatastoreClient([ 'projectId' => 'my-project-id' ]); $bucketName = 'my-bucket-name'; $imageName = 'image.jpg'; $imageAnnotator = new ImageAnnotatorClient(); $image = file_get_contents(sprintf('gs://%s/%s', $bucketName, $imageName)); $response = $imageAnnotator->annotateImage([ 'image' => [ 'source' => [ 'gcsImageUri' => sprintf('gs://%s/%s', $bucketName, $imageName), ], ], 'features' => [ ['type' => 'TEXT_DETECTION'], ['type' => 'LABEL_DETECTION'], ], ]); $ocrResult = $response->getTextAnnotations()[0]->getDescription(); $key = $datastore->key('ImageResults', sprintf('image_%s', $imageName)); $task = $datastore->entity($key, [ 'ocrResult' => $ocrResult, ]); $datastore->insert($task); ?>
Step 5: Get the return result of Google Cloud Vision API
After completing the image processing, we can use the following code to get the return result of Google Cloud Vision API and print it to our In Web application:
<?php require __DIR__ . '/vendor/autoload.php'; use GoogleCloudDatastoreDatastoreClient; $datastore = new DatastoreClient([ 'projectId' => 'my-project-id' ]); $bucketName = 'my-bucket-name'; $imageName = 'image.jpg'; $key = $datastore->key('ImageResults', sprintf('image_%s', $imageName)); $result = $datastore->lookup($key); ?>
The above code will return the OCR recognition results and image tags in array form, and we can display them in the Web application for users to view.
So far, we have taken PHP as an example to introduce in detail how to use the Google Cloud Vision API to implement image and visual data processing. With the help of the powerful functions of Google Cloud Vision API and PHP, we can process large amounts of visual data more conveniently and efficiently, bringing richer functions and experiences to our web applications.
The above is the detailed content of PHP and Google Cloud Vision integration for image and visual data processing. For more information, please follow other related articles on the PHP Chinese website!