face++ 人脸融合 测试代码

<?php

namespace app\index\controller;

use GuzzleHttp\Client;
use Intervention\Image\ImageManager;
use Intervention\Image\ImageManagerStatic as Image;

class Index
{
    /**
     * @var mixed
     */
    private $client = null;

    /**
     * @var mixed
     */
    private $manager = null;

    /**
     * @var string
     */
    private $protocol = 'https';

    /**
     * @var string
     */
    private $base_uri = 'api-cn.faceplusplus.com';

    /**
     * @var string
     */
    private $api_key = '';

    /**
     * @var string
     */
    private $api_secret = '';

    /**
     * @var string
     */
    private $template_file = './test/template.jpg';

    /**
     * @var string
     */
    private $template_rectangle = '';

    /**
     * @var string
     */
    private $merge_file = './test/merge.jpg';

    /**
     * @var string
     */
    private $image_file = './test/template.jpg';

    /**
     * @var string
     */
    private $image_save = './test/save.jpg';

    /**
     * @var string
     */
    private $base64 = '';

    /**
     * @var int
     */
    private $merge_rate = 65;

    /**
     * @param Client $client
     * @param ImageManager $manager
     */
    public function __construct(Client $client, ImageManager $manager)
    {
        $this->client  = $client;
        $this->manager = $manager;
    }

    public function index()
    {
        $this->detect();
        $this->merge();
        $this->image();
    }

    public function image()
    {
        $image = $this->manager->make($this->base64)->save($this->image_save);
        $img   = file_get_contents($this->image_save, true);
        header("Content-Type: image/jpeg;text/html; charset=utf-8");
        echo $img;
        exit;

        // $stream = $this->manager->make($this->base64)->stream('jpg', 80);
        // $stream->send();
        // header('location:' . $this->image_save);
    }

    public function merge()
    {

        $uri = $this->protocol . '://' . $this->base_uri . '/imagepp/v1/mergeface';
        $res = $this->client->request('POST', $uri, [
            'verify'    => false,
            'multipart' => [
                [
                    'name'     => 'api_key',
                    'contents' => $this->api_key,
                ],
                [
                    'name'     => 'api_secret',
                    'contents' => $this->api_secret,
                ],
                [
                    'name'     => 'template_file',
                    'contents' => fopen($this->template_file, 'r'),
                ],
                [
                    'name'     => 'merge_file',
                    'contents' => fopen($this->merge_file, 'r'),
                ],
                [
                    'name'     => 'template_rectangle',
                    'contents' => $this->template_rectangle,
                ],
                [
                    'name'     => 'merge_rate',
                    'contents' => $this->merge_rate,
                ],
            ],
        ]);

        $body         = $res->getBody();
        $contents     = $body->getContents();
        $response     = json_decode($contents, true);
        $this->base64 = $response['result'];
        // dump($response);
    }

    public function detect()
    {
        $uri = $this->protocol . '://' . $this->base_uri . '/facepp/v3/detect';
        $res = $this->client->request('POST', $uri, [
            'verify'    => false,
            'multipart' => [
                [
                    'name'     => 'api_key',
                    'contents' => $this->api_key,
                ],
                [
                    'name'     => 'api_secret',
                    'contents' => $this->api_secret,
                ],
                [
                    'name'     => 'image_file',
                    'contents' => fopen($this->image_file, 'r'),
                ],
            ],
        ]);

        $body     = $res->getBody();
        $contents = $body->getContents();
        $response = json_decode($contents, true);
        $face     = $response['faces'][0]['face_rectangle'];
        // dump($face);
        // die();
        $this->template_rectangle = $face['top'] . ',' . $face['left'] . ',' . $face['width'] . ',' . $face['height'];
        // dump($this->template_rectangle);
    }
}

此处评论已关闭