Welcome to the first lesson of our course, "Creating Images with Gemini and PHP." In this course, you will explore the fascinating world of AI-driven image generation using Google's Gemini API and its gemini-3.1-flash-image model.
Our journey begins with understanding how to set up the environment and generate a simple image. This foundational lesson will set the stage for more advanced topics in subsequent units.
Before we dive into generating images, it's crucial to set up our environment correctly. First, ensure you have access to the Gemini API by retrieving your API key. This key is essential for authenticating your requests to the API. You can set this key in your .env file as GEMINI_API_KEY.
For this lesson, you'll need to use Composer, PHP's package manager, to install the necessary libraries. You can install the guzzlehttp/guzzle library for making HTTP requests.
Note that the environment will be fully set up for the practice section, so you can focus on the exercises without worrying about the setup.
When you execute the script, the terminal will display the path to your newly created image. Since we used the prompt "A serene sunset over a mountain range," the Gemini API returns base64 data which our PHP script converts into a .png file.
Generated Image:

In this lesson, you learned how to set up your environment, configure the Gemini API client, and generate a simple image using the gemini-3.1-flash-image model and the generateContent API. We also explored how to process and save the generated image using PHP.
This foundational knowledge will be crucial as you progress through the course. As you move on to the practice exercises, I encourage you to experiment with different prompts and configurations to see the diverse range of images you can create.
This hands-on practice will solidify your understanding and prepare you for more advanced topics in the upcoming lessons.
