這篇文章主要介紹“如何用PHP將女友照片轉(zhuǎn)成可愛(ài)的動(dòng)漫頭像”,在日常操作中,相信很多人在如何用PHP將女友照片轉(zhuǎn)成可愛(ài)的動(dòng)漫頭像問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何用PHP將女友照片轉(zhuǎn)成可愛(ài)的動(dòng)漫頭像”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
專注于為中小企業(yè)提供網(wǎng)站建設(shè)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)倉(cāng)山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
首先注冊(cè)個(gè)百度AI,然后進(jìn)到人像動(dòng)漫化開(kāi)通控制臺(tái)(好像是前500次免費(fèi)調(diào)用接口)。

到我的控制臺(tái)創(chuàng)建應(yīng)用

然后把Api Key和Serect Key 記下來(lái), 等下需要用來(lái)獲取AccessToken

獲取AccessToken
1.先封裝一個(gè)curl請(qǐng)求方法
<?php
class Curl
{
public function post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定網(wǎng)頁(yè)
curl_setopt($curl, CURLOPT_HEADER, 0);//設(shè)置header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($curl);//運(yùn)行curl
if ($error = curl_error($curl)) {
die($error);
}
curl_close($curl);
return $data;
}
}2.獲取AccessToken
require_once 'Curl.php';
class AccessToken
{
// Api Key
private $apiKey= '';
// Secret Key
private $secretKey = '';
private $requestToeknUrl = "https://aip.baidubce.com/oauth/2.0/token";
private $accessToken;
public function __construct()
{
// 默認(rèn)有效時(shí)間2592000秒, 可以存到緩存中
// 對(duì)返回的數(shù)據(jù)沒(méi)做過(guò)段判斷, 需要的請(qǐng)自行判斷處理
$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;
}
}通過(guò)getAccessToken()方法獲取AccessToken
<?php
// require_once 'Curl.php';
require_once 'AccessToken.php';
class Demo
{
public function index()
{
// 獲取AccessToken
$accessToken = (new AccessToken())->getAccessToken());
// 百度AI接口
$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');
// base64編碼的圖片, 可以是本地圖片或網(wǎng)絡(luò)上傳的, 只要能轉(zhuǎn)成base64編碼就可以了
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$result = (new Curl())->post($url, $bodys);
$result = json_decode($result, true);
// data:image/jpg;base64,
echo "<img src=\"data:image/jpg;base64,{$result['image']}\" />";
}
}
(new Demo())->index();最后貼上別人的女朋友o(╥﹏╥)o


到此,關(guān)于“如何用PHP將女友照片轉(zhuǎn)成可愛(ài)的動(dòng)漫頭像”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
本文題目:如何用PHP將女友照片轉(zhuǎn)成可愛(ài)的動(dòng)漫頭像
當(dāng)前鏈接:http://www.chinadenli.net/article20/geipco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、小程序開(kāi)發(fā)、搜索引擎優(yōu)化、定制網(wǎng)站、微信公眾號(hào)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)