腾讯人脸融合接口的使用

最近一个程序用到了腾旭的人脸融合接口,类似于天天p图的功能

接口参考文档https://ai.qq.com/doc/facemerge.shtml

下面是一个demo

<?php
error_reporting(0);

/**
 * Class Common
 * by sdxjwkq
 */
class Common{
    protected function getReqSign($params /* 关联数组 */, $appkey /* 字符串*/)
    {
        // 1. 字典升序排序
        ksort($params);

        // 2. 拼按URL键值对
        $str = '';
        foreach ($params as $key => $value)
        {
            if ($value !== '')
            {
                $str .= $key . '=' . urlencode($value) . '&';
            }
        }

        // 3. 拼接app_key
        $str .= 'app_key=' . $appkey;

        // 4. MD5运算+转换大写,得到请求签名
        $sign = strtoupper(md5($str));
        return $sign;
    }
    protected function doHttpPost($url, $params)
    {
        $curl = curl_init();

        $response = false;
        do
        {
            // 1. 设置HTTP URL (API地址)
            curl_setopt($curl, CURLOPT_URL, $url);

            // 2. 设置HTTP HEADER (表单POST)
            $head = array(
                'Content-Type: application/x-www-form-urlencoded'
            );
            curl_setopt($curl, CURLOPT_HTTPHEADER, $head);

            // 3. 设置HTTP BODY (URL键值对)
            $body = http_build_query($params);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

            // 4. 调用API,获取响应结果
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_NOBODY, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $response = curl_exec($curl);
            if ($response === false)
            {
                $response = false;
                break;
            }

            $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($code != 200)
            {
                $response = false;
                break;
            }
        } while (0);

        curl_close($curl);
        return $response;
    }
}
class facemerge extends Common{
    private $path="./z.jpg";//图片文件路径
    public function index() {
        $path   =
        $data   = file_get_contents($this->path);
        $base64 = base64_encode($data);
        // 设置请求数据
        $appkey = 'vLHhvee687gUW6Cr';
        $params = array(
            'app_id'     => 'xxxxxx',
            'image'      => $base64,
            'model'      => '32',
            'time_stamp' => strval(time()),
            'nonce_str'  => strval(rand()),
            'sign'       => '',
        );
        $params['sign'] = $this->getReqSign($params, $appkey);
        // edump($params);
        // 执行API调用
        $url = 'https://api.ai.qq.com/fcgi-bin/ptu/ptu_facemerge';
        $response = $this->doHttpPost($url, $params);
        $img = json_decode($response,true);
        if($img['ret']!='4096'){

            $img = "<img src='data:image/jpg;base64,".$img['data']['image']."'/>'";
            // $img = base64_decode($img);
            echo $img;
        }else{
            echo 1;
        }
    }
}
$faceFusion=new facemerge();
$faceFusion->index();

效果如下


api提供了多个模板选择,也可以自定义


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值