Computer Vision PHP Example Code

Computer Vision Using PHP
PHP Computer Vision Tutorial
asticaVision PHP ‐ REST API

Implement state of the art computer vision AI into your PHP application with the asticaVision REST API. Providing a seamless way to leverage powerful computer vision models and algorithms in your PHP application.

Whether you're building an e-commerce platform that needs to recognize products from images, developing a security system that requires accurate facial recognition, or creating an augmented reality application that demands robust object detection, asticaVision has you covered.

Try Online

1. Advanced Functionality:
Use PHP to perform computer visions tasks securely on your server, without a browser or GPU. This enables the automation of facial recognition, object detection, text detection, image description, automatic content moderation, and image categorization.

  • Detect faces, age and gender
  • Detect and read text (OCR)
  • Categorize and tag user submitted images
  • Automatically moderate uploaded images
  • Recognize objects in detail (brand, or species)
  • Describe and write long-form content from images

2. Simple Integration:
The PHP Rest API can be easily integrated into your web and mobile applications with just a few lines of code. This simple yet powerful solution will transform your applications into advanced image analysis and understanding tools in no time.

3. No PHP Modules Required
The example codes can be used run computer vision on any PHP server with curl available. The asticaVision API is intended to work identically on all platforms. Bring your integration to new platforms and programming languages, without libraries or external modules

PHP Vision AI: Get Started
Facial Recognition and Object Detection
Detect Faces and Objects with PHP

Here is an example code demonstrating basic use of computer vision with javascript.

//base64 or url (https) of image
$asticaAPI_input = 'https://astica.ai/example/asticaVision_sample.jpg';
//comma separated list of parameters
$asticaAPI_visionParams = 'gpt, describe, describe_all, objects, faces';
// If using "gpt" or "gpt_detailed" visionParam,
// optional custom prompt and length
// describe the image, answer the math question, etc
$asticaAPI_gpt_prompt = '';
$asticaAPI_prompt_length = 95;
//If using "objects_custom" visionParam,
// supply a list of comma-separated keywords

$objects_custom_kw = '';
//Set Model Version
$asticaAPI_modelVersion = '2.5_full';  
//set your key
$asticaAPI_key = 'YOUR API KEY'; // Generate: https://astica.ai/api-keys/
//Define CURL Timeout
$asticaAPI_timeout = 35; //seconds
//Set vision AI endpoint
$asticaAPI_endpoint = 'https://vision.astica.ai/describe';
//Build Model Input
$asticaAPI_payload = [
        'tkn' => $asticaAPI_key,
        'modelVersion' => $asticaAPI_modelVersion,
        'input' => $asticaAPI_input,
        'visionParams' => $asticaAPI_visionParams,
        'gpt_prompt' => $asticaAPI_gpt_prompt,
        'prompt_length' => $asticaAPI_prompt_length,
        'objects_custom_kw' => $objects_custom_kw
    ];
//Perform PHP Computer Vision
$result = asticaAPI(
    $asticaAPI_endpoint,
    $asticaAPI_payload,
    $asticaAPI_timeout
);
//Read Vision Detection Output
print_r($result);

This PHP code will allows you to send an image URL or base64 of a file from the web server. The results of computer vision will be printed on the screen. With this PHP code, you can also effectively detect objects and faces, or generate image captions. There are plenty of other features available to explore and pre-built code examples to try out.

View Full API Parameters
Notice:
  • If a website blocks hotlinking, any request with a URL input will result in a failed response.
Vision AI Code Sample
Computer Vision with PHP
Full Computer Vision Function

You can implement computer vision capabilities by using PHP CURL.

Using PHP to Detect Objects - Sample Code

This example demonstrates how to make perform computer vision using PHP. Like with other languages, you can send an input as either HTTPS URL or a base64 encoded string. The $asticaAPI_visionParams variable an be modified with the desired parameters to specify exact computer vision capabilities, or 'all' can be used to generate all outputs.

<?php    
    $asticaAPI_key = 'YOUR API KEY'; //visit https://astica.ai
    $asticaAPI_timeout = 30; // seconds  Using "gpt" or "gpt_detailed" will increase response time.

    $asticaAPI_endpoint = 'https://vision.astica.ai/describe';
    $asticaAPI_modelVersion = '2.5_full';  //1.0_full, 2.0_full, 2.1_full or 2.5_full
    
    if(1 == 2) {
        //Input Method 1: https URL of a jpg/png image (faster)
        $asticaAPI_input = 'https://astica.ai/example/asticaVision_sample.jpg'; 
    } else {
        //Input Method 2: base64 encoded string of a local image (slower)          
        $image_path = 'image.jpg';
        $image_data = file_get_contents($image_path);
        $asticaAPI_input = base64_encode($image_data);
    }
    
    //comma separated options; leave blank for all; note "gpt" and "gpt_detailed" are slower.
    //see all: https://astica.ai/vision/documentation/#parameters
    $asticaAPI_visionParams = 'gpt, describe, describe_all, tags, objects, faces'; 
    $asticaAPI_visionParams = ''; 
    $asticaAPI_gpt_prompt = ''; // only used if visionParams includes "gpt" or "gpt_detailed"
    $asticaAPI_prompt_length = '90'; // number of words in GPT response
    $objects_custom_kw = ''; // only used if visionParams includes "objects_custom" (v2.5_full or higher)
    

    /*        
        '2.5_full' supported visionParams: https://astica.ai/vision/documentation/#parameters
            describe
            describe_all
            gpt (or) gpt_detailed 
            text_read
            objects
            objects_custom
            objects_color
            categories
            moderate
            tags
            color
            faces
            celebrities
            landmarks
            brands        
    */

    // Define payload array
    $asticaAPI_payload = [
        'tkn' => $asticaAPI_key,
        'modelVersion' => $asticaAPI_modelVersion,
        'input' => $asticaAPI_input,
        'visionParams' => $asticaAPI_visionParams,
        'gpt_prompt' => $asticaAPI_gpt_prompt,
        'prompt_length' => $asticaAPI_prompt_length,
        'objects_custom_kw' => $objects_custom_kw
    ];
    
    // Call API function and store result
    $result = asticaAPI($asticaAPI_endpoint, $asticaAPI_payload, $asticaAPI_timeout);
    print_r($result);
    
    // Define API function
    function asticaAPI($endpoint, $payload, $timeout = 15) {
        $ch = curl_init();
        $payload = json_encode($payload);
        // Set cURL options
        curl_setopt_array($ch, [
            CURLOPT_URL => $endpoint,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_CONNECTTIMEOUT => $timeout,
            CURLOPT_TIMEOUT => $timeout,
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($payload),
                'Accept: application/json'
            ]
        ]);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);        
        $result = json_decode($response, true);
        if(!isset($result['status'])) {
            $result = json_decode(json_decode($response), true);            
        }
        return $result;
    }
?>

Save the code into a PHP file and upload it to your server. When you replace your API key with one generated from your account, the script can be run to display the computer vision output.

astica ai Discover More AI

Experiment with different kinds of artificial intelligence. See, hear, and speak with astica.

Return to Dashboard
You can return to this page at any time.
Success Just Now
Copied to clipboard
Success Just Now
Submission has been received.
Success Just Now
Account preferences have been updated.