新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用PHP將女友照片轉(zhuǎn)成可愛的動漫頭像!
在網(wǎng)上看到一篇將女朋友照片轉(zhuǎn)成動漫頭像的博文(java實現(xiàn)),emmmmm, 我不會Python, 又不會Java,更重要的是沒有女朋友! 所以我決定用萬能的php實現(xiàn)將別人女朋友照片轉(zhuǎn)成動漫頭像!
首先注冊個百度AI,然后進(jìn)到人像動漫化開通控制臺(好像是前500次免費(fèi)調(diào)用接口)。
到我的控制臺創(chuàng)建應(yīng)用
然后把Api Key和Serect Key 記下來, 等下需要用來獲取AccessToken
獲取AccessToken
1.先封裝一個curl請求方法
2.獲取
AccessTokenrequire_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)有效時間2592000秒, 可以存到緩存中 // 對返回的數(shù)據(jù)沒做過段判斷, 需要的請自行判斷處理 $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; } }通過
getAccessToken()方法獲取AccessTokengetAccessToken()); // 百度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 ""; } } (new Demo())->index();
最后貼上別人的女朋友o(╥﹏╥)o
名稱欄目:用PHP將女友照片轉(zhuǎn)成可愛的動漫頭像!
標(biāo)題網(wǎng)址:http://fisionsoft.com.cn/article/dhpsgdi.html


咨詢
建站咨詢
