Home Backend Development PHP Tutorial Simple tutorial: How to connect PHP to Baidu image definition recognition interface?

Simple tutorial: How to connect PHP to Baidu image definition recognition interface?

Aug 25, 2023 pm 01:40 PM
php image Baidu clarity docking

Simple tutorial: How to connect PHP to Baidu image definition recognition interface?

Simple tutorial: How to connect PHP to Baidu image definition recognition interface?

Introduction:
Baidu image clarity recognition interface is a powerful image processing function provided by Baidu AI open platform. Through this interface, we can use Baidu AI's powerful algorithm to judge the clarity of an image and derive a corresponding score. This tutorial will show how to use PHP code to connect to Baidu's image clarity recognition interface.

Environment preparation:

  1. PHP development environment (version requirement: PHP 5.6)
  2. Baidu AI open platform account and Access Token (for how to obtain, please refer to the official documentation: https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu)
  3. Image file (this tutorial will take an image file named "test.jpg" as an example)

Step 1: Obtain the Base64 encoding of the image through POST request

<?php
    function imgToBase64($imgPath) {
        $imgInfo = getimagesize($imgPath);
        $fp = fopen($imgPath, 'rb');
        if ($fp) {
            $imgData = fread($fp, filesize($imgPath));
            $base64Data = base64_encode($imgData);
            return 'data:' . $imgInfo['mime'] . ';base64,' . $base64Data;
        } else {
            return false;
        }
    }
    
    $imgPath = 'test.jpg';
    $base64Data = imgToBase64($imgPath);
    if (!$base64Data) {
        echo '图像文件读取失败';
        exit;
    }
?>
Copy after login

Step 2: Construct the HTTP request data and send the request

<?php
    $url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/clearness';
    $access_token = 'your_access_token';
    
    // 构造请求数据
    $requestData = array(
        'image' => $base64Data,
    );
    $requestBody = http_build_query($requestData);
    
    // 发送POST请求
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded',
        'Content-Length: ' . strlen($requestBody),
        'Access-Token: ' . $access_token,
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    
    // 解析响应结果
    $result = json_decode($response, true);
    if (isset($result['error_code'])) {
        echo '请求出错:' . $result['error_msg'];
        exit;
    }
    
    // 输出清晰度评分
    echo '清晰度评分:' . $result['result']['score'];
?>
Copy after login

Step 3: Run the code and view the results
Save the above code as a PHP file and make sure the correct Access Token has been filled in. Run the PHP file in the command line or browser to get the image clarity score.

Summary:
This tutorial shows how to use PHP to connect to Baidu image definition recognition interface. By using this interface, we can easily judge the clarity of an image, so as to further analyze and process the image quality. I hope this tutorial can provide some help to everyone's development work in image processing.

The above is the detailed content of Simple tutorial: How to connect PHP to Baidu image definition recognition interface?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

deepseek web version entrance deepseek official website entrance

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

CakePHP Creating Validators

See all articles