Introduction to the Image Manager

Welcome to the second lesson of our course on building an image generation service with PHP. In our previous lesson, we created the PromptManager class to handle the formatting of prompts for our image generation process. Now, we'll build upon that foundation by creating the ImageManager class, which will be responsible for handling the images once they're generated.

The ImageManager serves as a crucial component in our application architecture. While the PromptManager prepares the instructions for image generation, the ImageManager takes care of what happens after an image is created. Its primary responsibilities include:

  1. Storing generated images along with their associated prompts
  2. Converting images to a web-friendly format (base64)
  3. Providing access to the collection of stored images

This component is essential because web applications cannot directly work with raw image data. By converting images to base64 strings, we can easily embed them in HTML or send them as JSON responses in our API. Additionally, keeping track of which prompt generated which image allows us to maintain a history of generations and potentially reuse successful prompts.

In the next lesson, we'll call the Gemini API using the generateContent flow with gemini-3.1-flash-image to actually produce images, and the ImageManager will take care of everything that happens after that.

Let's dive into building this important piece of our image generation service.

Setting Up the Image Manager Class

To begin, we'll create a new file called ImageManager.php in our app/models directory. This file will contain our ImageManager class. First, let's set up the basic structure and understand the imports we'll need:

<?php

class ImageManager {
    private $images = [];

    public function __construct() {
        $this->images = [];
    }
}

Our ImageManager class starts with a simple constructor that initializes an empty array called $images. This array will store associative arrays containing information about each image, including:

  • A unique identifier
  • The prompt that generated the image
  • The base64-encoded image data

This simple data structure allows us to keep track of all generated images and their associated metadata in memory. In a production application, you might want to use a database instead, but for our learning purposes, this in-memory storage works well.

Converting Images to Base64 Format
Building Image Collection Methods

Now that we can convert images to base64, let's implement the methods for adding images to our collection and retrieving the stored images.

The addImage Method
The getImages Method
public function getImages() {
    return $this->images;
}

This method simply returns the entire array of image entries, allowing other parts of our application to access all stored images and their associated metadata.

Testing the Image Manager
Summary and Practice Preview

In this lesson, we've built the ImageManager class, a crucial component of our image generation service. This class handles the storage and processing of generated images, converting them to a web-friendly format and maintaining a collection of all images along with their associated prompts.

Let's review what we've learned:

  1. We created a class structure with a simple in-memory storage mechanism.
  2. We implemented a method to convert image data to base64 format, making it suitable for web applications.
  3. We built methods to add images to our collection and retrieve all stored images.
  4. We created a simple test script to verify the functionality of our ImageManager.

The ImageManager complements the PromptManager we built in the previous lesson. While the PromptManager prepares the instructions for image generation, the ImageManager handles the results of that generation process. Together, these components form the foundation of our image generation service.

In the upcoming practice exercises, you'll have the opportunity to work with the ImageManager class, testing its functionality with different types of image data and exploring how it integrates with the rest of our application. You'll also get to experiment with error handling and see how the class behaves in various scenarios.

In our next lesson, we'll build upon this foundation by implementing the ImageGeneratorService, which will call the Gemini generateContent API using gemini-3.1-flash-image to generate images based on our prompts, then extract the binary image bytes from the response and pass them into the ImageManager. This service will bring together both the PromptManager and ImageManager classes we've created so far.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal