用PHP将女友照片转成可爱的动漫头像!

在网上看到一篇将女朋友照片转成动漫头像的博文(java实现),emmmmm, 我不会Python, 又不会Java,更重要的是没有女朋友! 所以我决定用万能的PHP实现将别人女朋友照片转成动漫头像!

首先注册个百度AI,然后进到人像动漫化开通控制台(好像是前500次免费调用接口)。

b9e1e92495a775875cede2608a7e135.png

到我的控制台创建应用

1bdeca49d4083f6a2a2f705987ffcb7.png

然后把Api KeySerect Key 记下来, 等下需要用来获取AccessToken

e8f185f2921a40b58d87949cd5551e3.png

获取AccessToken

1.先封装一个curl请求方法

<?php
class Curl
{
    public function post($url = &#39;&#39;, $param = &#39;&#39;)
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($curl);//运行curl
        if ($error = curl_error($curl)) {
            die($error);
        }
        curl_close($curl);
        return $data;
    }
}

2.获取AccessToken

require_once &#39;Curl.php&#39;;

class AccessToken
{
	// Api Key
	private $apiKey= &#39;&#39;; 
	// Secret Key
	private $secretKey = &#39;&#39;;

	private $requestToeknUrl = "https://aip.baidubce.com/oauth/2.0/token";
	private $accessToken;

	public function __construct()
	{
		// 默认有效时间2592000秒, 可以存到缓存中
		// 对返回的数据没做过段判断, 需要的请自行判断处理 
		$this->accessToken = ($this->requestAccessToken())[&#39;access_token&#39;];
	}

	public function requestAccessToken(){
		$url = $this->requestToeknUrl;
	    $postData[&#39;grant_type&#39;]       = &#39;client_credentials&#39;;
	    $postData[&#39;client_id&#39;]      = $this->apiKey;
	    $postData[&#39;client_secret&#39;] = $this->secretKey;
	    $o = "";
	    foreach ( $postData as $k => $v ) 
	    {
	    	$o.= "{$k}=" . urlencode( $v ). "&" ;
	    }
	    $postData = trim($o, &#39;&&#39;);
	    
	    $result = (new Curl())->post($url, $postData);
	    return json_decode($result, true);
	}
	
	public function getAccessToken()
	{
		return $this->accessToken;
	}
}

通过getAccessToken()方法获取AccessToken

<?php
// require_once &#39;Curl.php&#39;;
require_once &#39;AccessToken.php&#39;;

class Demo
{
	public function index()
	{
		// 获取AccessToken
		$accessToken = (new AccessToken())->getAccessToken());
		// 百度AI接口
		$url = &#39;https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=&#39; . $accessToken;
		// 图片路径
		$img = file_get_contents(&#39;C:\Users\Admin\Desktop\6a56f099861bf4c470e5d24f7017b1a.jpg&#39;);
		// base64编码的图片, 可以是本地图片或网络上传的, 只要能转成base64编码就可以了
		$img = base64_encode($img);
		$bodys = array(
		    &#39;image&#39; => $img
		);
		$result = (new Curl())->post($url, $bodys);
		$result = json_decode($result, true);
		// data:image/jpg;base64,
		echo "<img  src=\"data:image/jpg;base64,{$result[&#39;image&#39;]}\" />";
	}
}
(new Demo())->index();

最后贴上别人的女朋友o(╥﹏╥)o

36d15a353e5808f486a14cba424ba6a.png

68f7a1a41918881eb1e84a846135c1c.png

推荐学习:《PHP视频教程》

以上就是用PHP将女友照片转成可爱的动漫头像!的详细内容,更多请关注其它相关文章!