2022-02-12

用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 = '', $param = '')
    {
        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 '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()
	{
		// 默认有效时间2592000秒, 可以存到缓存中
		// 对返回的数据没做过段判断, 需要的请自行判断处理 
		$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()方法获取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编码的图片, 可以是本地图片或网络上传的, 只要能转成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

36d15a353e5808f486a14cba424ba6a.png

68f7a1a41918881eb1e84a846135c1c.png

推荐学习:《PHP视频教程

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

php中文网APP下载

声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除

  • 相关标签:php
  • https://www.php.cn/php-weizijiaocheng-488768.html

    发表回复

    Your email address will not be published. Required fields are marked *