How to Integrate Google Gemini AI into Laravel with the Gemini PHP Package
This guide shows how to use the Gemini PHP package in a Laravel application to call Google Gemini AI APIs, including text generation with Gemini Pro and multimodal image analysis with Gemini Pro Vision, complete with code examples and resource links.
By using the Gemini PHP package, you can access Google’s Gemini AI API directly from a Laravel application. The package wraps the Gemini PHP Client ( https://github.com/google-gemini-php/client ) and provides a convenient facade for interacting with the available models.
use Gemini\Laravel\Facades\Gemini;
$result = Gemini::geminiPro()->generateContent('Hello');
// Get the generated text
echo $result->text(); // Reply: Hello! How can I assist you today?Gemini AI also offers Gemini Pro Vision, which can process text, images, and video simultaneously. You can submit an image to the model and ask questions about its content.
$result = Gemini::geminiProVision()
->generateContent([
'这是一张什么图片?',
new Blob(
mimeType: MimeType::IMAGE_JPEG,
data: base64_encode(
file_get_contents('https://storage.googleapis.com/generativeai-downloads/images/scones.jpg')
)
)
]);
echo $result->text();
/*
The picture shows a table with a white tablecloth. On the table are two cups of coffee, a bowl of blueberries, a silver spoon, and some flowers. There are also some blueberry scones on the table.
*/The package also includes examples for streaming responses, testing product authenticity, and more.
You can reference the package on GitHub: https://github.com/google-gemini-php/laravel . For further details, consult the official Gemini documentation and API reference at https://ai.google.dev/docs and the Gemini portal at https://gemini.google.com .
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
