【openai 绘画 对接】chatgpt绘画

 public function ai_draw_image($item = [])
    {
        if ($item) {
            $type_info = ChatDrawType::where(['id' => $item['style_id']])->find();
            $prompt = $type_info['title'] . $type_info['memo'] . ':' . $item['title'];
//重点  开始
            $params = [
                'size' => $item['size'], 
                'prompt' => $prompt
            ];
            $url = "https://XXXXXXX.com/newapi/Chatai/aisend_image?prompt=" . $prompt . '&size=' . $item['size'] . '&isdev=1';
            $res = curl_post($url, $params);
            $data = json_decode($res, true);
//重点结束
            if ($data['msg'] == 'success') {
                if (!isset($data['data']['data'][0]['b64_json'])) {
                    return [0, $data['data']['error']['message']];
                }
                $base64_img = $data['data']['data'][0]['b64_json'];
                $base64_img = 'data:image/png;base64,' . $base64_img;
                $base64_img = trim($base64_img);
                $up_dir = 'public/chatdraw/' . date('Ymd') . '/';//存放在当前目录的upload文件夹下
                if (!file_exists($up_dir)) {
                    mkdir($up_dir, 0777, true);
                }
                if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
                    $type = $result[2];
                    if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                        $new_file = $up_dir . date('YmdHis_') . '.' . $type;
                        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))) {
                            $img_path = str_replace('../../..', '', $new_file);
                            $file = $img_path;//本地文件路径
                        } else {
                            return [0, '图片上传失败'];
                        }
                    } else {
                        //文件类型错误
                        return [0, '图片上传类型错误'];
                    }
                } else {
                    //文件错误
                    return [0, '文件错误'];
                }
                return [1, $file];
            } else {
                return [0, '网络错误'];
            }
        }
    }

需要科学上网

public function aisend_image()
    {
        $params = I('get.');
        $send = self::send_image_msg($params);
    }

    public static function send_image_msg($data)
    {
        $url = "https://api.openai.com/v1/images/generations";
        $params = [
            'model' => 'image-alpha-001',
            'prompt' => $data['prompt'],
            "num_images" => 1,
            "size" => $data['size'],
            "response_format" => "b64_json"
        ];
        $res = curl_post($url, $params, $data['isdev']);

        $res = json_decode($res, 1);

        api_return(1, 1, $res);
        return $res;
    }
if (!function_exists('curl_post')) {
    /**
     * curl post 请求
     * @param string $url 请求地址
     */
    function curl_post($url, $post_data, $isdev = 1)
    {
        /*$post_data = array(
            "uid" => "1111",
            "username" => "lunar",
            "nickname" => "吾独望月",application/json;charset=utf-8
        );*/
        if ($isdev == 1) {
            $headers = array(
                "content-type: application/json;charset=utf-8",//设置请求头  请求的内容格式,编码
                "Accept: application/json",   //Accept字段头确定客户端可以接收的媒体类型
                'Authorization:Bearer sk-*********************************',
            );
        } else {
            $headers = array(
                "content-type: application/json;charset=utf-8",//设置请求头  请求的内容格式,编码
                "Accept: application/json",   //Accept字段头确定客户端可以接收的媒体类型
                'Authorization:Bearer sk-*********************************',
            );
        }

        $post_data = json_encode($post_data);
        $ch = curl_init();//初始化cURL

        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串并输出到屏幕上

        curl_setopt($ch, CURLOPT_POST, 1);//Post请求方式

        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//Post变量

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 跳过证书检查

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// 从证书中检查SSL加密算法是否存在

        $output = curl_exec($ch);//执行并获得HTML内容

        curl_close($ch);//释放cURL句柄

        return $output;
    }
}

你可能感兴趣的:(chatgpt)