PHP Requires Chilkat v11.4.0+
Chilkat2 PHP
Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads an image file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.
Chilkat PHP Downloads
<?php
use Chilkat2\Ai;
use Chilkat2\BinData;
$ai = new Ai();
// Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
$ai->Provider = 'openai';
// Use your provider's API key.
$ai->ApiKey = 'MY_API_KEY';
// We can upload directly from a file in the filesystem, or from a Chilkat BinData.
$contentType = 'image/jpeg';
$localFilePath = 'qa_data/jpg/starfish.jpg';
$filenameOnServer = 'starfish.jpg';
// Upload directly from a file.
try {
$file_id = $ai->UploadFile($localFilePath, $contentType);
echo 'File ID: ', $file_id, "\n";
echo "File uploaded.\n";
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
echo "AI File Upload Failed.\n";
}
// Upload from the contents of a Chilkat BinData
$bd = new BinData();
try {
$bd->LoadFile($localFilePath);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
try {
$file_id = $ai->UploadFileBd($bd, $filenameOnServer, $contentType);
echo 'File ID: ', $file_id, "\n";
echo "File uploaded.\n";
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
echo "AI File Upload Failed.\n";
}