File: /var/www/html/laravel/public/gpt-image-processing.php
<?php
$params = json_decode(file_get_contents('php://input'), true);
header('Content-Type: application/json');
$input = '';
$status = 'success';
$temperature = 0;
$max_tokens = 4096;
$input = '';
$data = [];
$messages = [];
$apiKey = $params['key'];
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
];
//if (!empty(trim($params['prompt']))) {
// $messages = [['role' => 'system', 'content' => $params['prompt']]];
//}
if (!empty($params['image'])) {
$messages[] = [
'role' => 'user',
'content' => [
[ "type" => "text", "text" => $params['prompt'] ],
["type" => "image_url", "image_url" => ['url' => "data:image/png;base64," . $params['image']]],
],
];
}
$url = 'https://api.openai.com/v1/chat/completions';
$data = array_merge($data, [
'model' => $params['model'],
'messages' => $messages,
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result = curl_error($ch);
}
curl_close($ch);
echo $result;