PHP Class Google\Cloud\Vision\Image

Please review Pricing before use, as a separate charge is incurred for each feature performed on an image. When practical, caching of results is certainly recommended. The Cloud Vision API supports a variety of image file formats, including JPEG, PNG8, PNG24, Animated GIF (first frame only), and RAW. Cloud Vision sets upper limits on file size as well as on the total combined size of all images in a request. Reducing your file size can significantly improve throughput; however, be careful not to reduce image quality in the process. See Best Practices - Image Sizing for current file size limits. Example: [snippet=default] use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $vision = $cloud->vision(); $imageResource = fopen(__DIR__ .'/assets/family-photo.jpg', 'r'); $image = $vision->image($imageResource, [ 'FACE_DETECTION' ]); [snippet=direct] Images can be directly instantiated. use Google\Cloud\Vision\Image; $imageResource = fopen(__DIR__ .'/assets/family-photo.jpg', 'r'); $image = new Image($imageResource, [ 'FACE_DETECTION' ]); [snippet=string] Image data can be given as a string use Google\Cloud\Vision\Image; $imageData = file_get_contents(__DIR__ .'/assets/family-photo.jpg'); $image = new Image($imageData, [ 'FACE_DETECTION' ]); [snippet=gcs] Files stored in Google Cloud Storage can be used. use Google\Cloud\Vision\Image; $file = $cloud->storage()->bucket('my-test-bucket')->object('family-photo.jpg'); $image = new Image($file, [ 'FACE_DETECTION' ]); [snippet=max] This example sets a maximum results limit on one feature and provides some image context. use Google\Cloud\Vision\Image; $imageResource = fopen(__DIR__ .'/assets/family-photo.jpg', 'r'); $image = new Image($imageResource, [ 'FACE_DETECTION', 'LOGO_DETECTION' ], [ 'maxResults' => [ 'FACE_DETECTION' => 1 ], 'imageContext' => [ 'latLongRect' => [ 'minLatLng' => [ 'latitude' => '-45.0', 'longitude' => '-45.0' ], 'maxLatLng' => [ 'latitude' => '45.0', 'longitude' => '45.0' ] ] ] ]); [snippet=shortcut] The client library also offers shortcut names which can be used in place of the longer feature names. use Google\Cloud\Vision\Image; $imageResource = fopen(__DIR__ .'/assets/family-photo.jpg', 'r'); $image = new Image($imageResource, [ 'faces', // Corresponds to FACE_DETECTION 'landmarks', // Corresponds to LANDMARK_DETECTION 'logos', // Corresponds to LOGO_DETECTION 'labels', // Corresponds to LABEL_DETECTION 'text', // Corresponds to TEXT_DETECTION 'safeSearch', // Corresponds to SAFE_SEARCH_DETECTION 'imageProperties' // Corresponds to IMAGE_PROPERTIES ]);
See also: https://cloud.google.com/vision/docs/image-best-practices Best Practices
See also: https://cloud.google.com/vision/docs/pricing Pricing
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php Class Usage Examples

Public Methods

Method Description
__construct ( resource | string | StorageObject $image, array $features, array $options = [] ) Create an image with all required configuration.
requestObject ( boolean $encode = true ) : array Return a formatted annotate image request.

Private Methods

Method Description
imageObject ( boolean $encode ) : array Create an image object.
maxResult ( string $feature ) : mixed Identify and return a maxResults value for a given feature, if maxResults is specified.
normalizeFeatures ( array $features ) : array Normalizes short feature names to identifiers compatible with the vision API and adds maxResults if specified.

Method Details

__construct() public method

Create an image with all required configuration.
public __construct ( resource | string | StorageObject $image, array $features, array $options = [] )
$image resource | string | Google\Cloud\Storage\StorageObject An image to configure with the given settings. This parameter will accept a resource, a string of bytes, or an instance of {@see \Google\Cloud\Storage\StorageObject}.
$features array A list of cloud vision [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#type) to apply to the image. Google Cloud Platform Client Library provides a set of abbreviated names which can be used in the interest of brevity in place of the names offered by the cloud vision service. These names are `faces`, `landmarks`, `logos`, `labels`, `text`, `safeSearch` and `imageProperties`.
$options array { Configuration Options @type array $maxResults A list of features and the maximum number of results to return. Keys should correspond to feature names given in the `$features` array, and values should be of type int. In all cases where `$maxResults` does not contain a value for a feature, all results will be returned. In cases where a `$maxResults` value is specified, the cloud vision service will return results up to the `$maxResults` value, or the full results, whichever is fewer. @type array $imageContext See [ImageContext](https://cloud.google.com/vision/reference/rest/v1/images/annotate#imagecontext) for full usage details. }

requestObject() public method

This method is used internally by {@see \Google\Cloud\Vision\VisionClient} and is not generally intended for use outside of that context. Example: use Google\Cloud\Vision\Image; $imageResource = fopen(__DIR__ .'/assets/family-photo.jpg', 'r'); $image = new Image($imageResource, [ 'FACE_DETECTION' ]); $requestObj = $image->requestObject();
See also: https://cloud.google.com/vision/reference/rest/v1/images/annotate#annotateimagerequest AnnotateImageRequest
public requestObject ( boolean $encode = true ) : array
$encode boolean [optional] If set to true, image bytes will be base64-encoded (required for json/rest requests)
return array