php调用gemini pro vision实现图片理解api进行聊天示例代码

代码语言:php

所属分类:其他

代码描述:php调用gemini pro vision实现图片理解api进行聊天示例代码,可上传本地图片给gemini进行理解分析聊天。

代码标签: php 调用 gemini pro vision 图片 理解 api 聊天 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php

// API Key and image path
$googleApiKey = 'your_google_api_key_here'; // Replace with your Google API key
$imagePath = 'path_to_your_local_image.jpg'; // Replace with the path to your local image

// Read the image content and encode it to base64
$imageData = file_get_contents($imagePath);
if ($imageData === false) {
    die("Failed to read the image file.");
}

$base64ImageData = base64_encode($imageData);

// Prepare the JSON payload
$requestPayload = json_encode([
  "contents" => [
    [
      "parts" => [
        ["text" => "What is this picture?"],
        [
          "inline_data" => [
            "mime_type" => "image/jpeg",
            "data" => $base64ImageData
          ]
        ]
      ]
    ]
  ]
]).........完整代码请登录后点击上方下载按钮下载查看

网友评论0