Home > Backend Development > PHP Tutorial > Use PHP to turn your girlfriend's photos into cute anime avatars!

Use PHP to turn your girlfriend's photos into cute anime avatars!

藏色散人
Release: 2023-04-10 21:48:01
forward
6006 people have browsed it

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

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

Use PHP to turn your girlfriends photos into cute anime avatars!

到我的控制台创建应用

Use PHP to turn your girlfriends photos into cute anime avatars!

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

Use PHP to turn your girlfriends photos into cute anime avatars!

获取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;
    }
}
Copy after login

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;
	}
}
Copy after login

通过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;]}\" / alt="Use PHP to turn your girlfriend's photos into cute anime avatars!" >";
	}
}
(new Demo())->index();
Copy after login

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

Use PHP to turn your girlfriends photos into cute anime avatars!

Use PHP to turn your girlfriends photos into cute anime avatars!

推荐学习:《PHP视频教程

The above is the detailed content of Use PHP to turn your girlfriend's photos into cute anime avatars!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template