Using Baidu AI to Convert Photos to Anime Avatars with PHP
This tutorial demonstrates how to register for Baidu AI, obtain an AccessToken, and use a custom PHP Curl wrapper to call the Baidu image‑to‑anime API, converting a regular photo into an anime‑style avatar, with full code examples.
The author wanted to create an anime‑style avatar from a photo using PHP, even though they lacked Python or Java experience.
First, register for a Baidu AI account, enable the "Portrait Anime" service, and note the provided API Key and Secret Key, which are needed to request an AccessToken.
Next, obtain the AccessToken by sending a POST request to Baidu's OAuth token endpoint (https://aip.baidubce.com/oauth/2.0/token) with the grant_type, client_id (API Key), and client_secret (Secret Key). The token is valid for 30 days and can be cached.
Implement a reusable Curl class in PHP to handle POST requests:
<?php
class Curl {
public function post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init(); // initialize curl
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);
if ($error = curl_error($curl)) {
die($error);
}
curl_close($curl);
return $data;
}
}
?>Create an AccessToken class that uses the Curl helper to request and store the token:
<?php
require_once 'Curl.php';
class AccessToken {
private $apiKey = '';
private $secretKey = '';
private $requestToeknUrl = "https://aip.baidubce.com/oauth/2.0/token";
private $accessToken;
public function __construct() {
$this->accessToken = ($this->requestAccessToken())['access_token'];
}
public function requestAccessToken() {
$url = $this->requestToeknUrl;
$postData['grant_type'] = 'client_credentials';
$postData['client_id'] = $this->apiKey;
$postData['client_secret'] = $this->secretKey;
$o = "";
foreach ($postData as $k => $v) {
$o .= "{$k}=" . urlencode($v) . "&";
}
$postData = trim($o, '&');
$result = (new Curl())->post($url, $postData);
return json_decode($result, true);
}
public function getAccessToken() {
return $this->accessToken;
}
}
?>Finally, use a Demo class to read a local image, base64‑encode it, call Baidu's selfie_anime endpoint with the token, decode the JSON response, and output the resulting anime image:
<?php
require_once 'AccessToken.php';
class Demo {
public function index() {
$accessToken = (new AccessToken())->getAccessToken();
$url = 'https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=' . $accessToken;
$img = file_get_contents('C:\Users\Admin\Desktop\6a56f099861bf4c470e5d24f7017b1a.jpg');
$img = base64_encode($img);
$bodys = array('image' => $img);
$result = (new Curl())->post($url, $bodys);
$result = json_decode($result, true);
echo "<img src=\"data:image/jpg;base64,{$result['image']}\" />";
}
}
(new Demo())->index();
?>Running the script displays the transformed anime avatar, as shown in the accompanying screenshots.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.