Let's generate images using OpenAI's dall-e-3 service. When using PHP, the open-source openai-php/client library facilitates the process. Check it out at https://github.com/openai-php/client.
In this article, we will learn how easy it is to generate an image with OpenAI and PHP.
Source code: https://github.com/medhatelmasry/openai-dalle3-php
Prerequisites
In order to proceed, you will need the following:
- Subscription with OpenAI - If you do not have a subscription, go ahead and register at https://openai.com/.
- PHP - You need to have PHP version 8.2 (or higher) installed on your computer. You can download the latest version from https://www.php.net/downloads.php.
- Composer – If you do not have Composer yet, download and install it for your operating system from https://getcomposer.org/download/.
Getting Started
In a suitable working directory, create a folder named openai-dalle3-php with the following terminal window command:
mkdir openai-dalle3-php
Change into the newly created folder with:
cd openai-dalle3-php
Using Composer, install the openai-php/client package by running this command:
composer require openai-php/client
We will be reading our ApoenAPI key from the .env text file. We need to install this package in order to do that.
composer require vlucas/phpdotenv
Getting an API KEY from OpenAI
With your OpenAI credentials, login into https://openai.com/. Click on API.
In the left navigation, click on "API keys".
Click on the "+ Create new secret key" button.
Give the new key a name. I named it 20240115 representing the date when it was created. You may wish to give it a more meaningful or creative name. Once you are happy with the name, click on the "Create secret key" to generate the key.
Click on the copy button beside the key and paste the API-Key somewhere safe as we will need to use it later on. Note that you cannot view this key again. Click on Done to dismiss the dialog.
We will create a text file named .env in our openai-dalle3-php folder with the following content:
OPENAI_API_KEY=put-your-openai-api-key-here
Set the API-Key as the value of OPENAI_API_KEY. This may look like this:
OPENAI_API_KEY=sk-OOghjTs8GsuHQklTCWOeT3BasdGJAjklBr3tr8ViZKv21BRN
Let's get coding
We can generate images by obtaining a URL to the newly created image, or by getting the Base-64 representation of the image. We will try both ways.
1) Get URL of the image
In the openai-dalle3-php folder, create a file named image-url.php with the following content:
<?phprequire_once __DIR__ . '/vendor/autoload.php';$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);$dotenv->load();$client = OpenAI::client($_ENV['OPENAI_API_KEY']);$response = $client->images()->create(['model' => 'dall-e-3','prompt' => 'A panda flying over Paris at night','n' => 1,'size' => '1024x1024','response_format' => 'url',]);foreach ($response->data as $data) {$data->url;$data->b64_json; // null}// display the imageecho '<img src="' . $data->url . '" />';?>
In the above code, note the following:
- we read in the API Key from .env file and pass it in the OpenAI::client($_ENV['OPENAI_API_KEY']); statement
- we request a URL response with an image size 1024 x 1024
- we prompt the dall-e-3 service to generate an image of 'A panda flying over Paris at night'.
To run the app, start the PHP web server to listen on port number 8888 with the following command in the openai-dalle3-php folder.
php -S localhost:8888
You can view the resulting image that gets created by OpenAI by pointing your browser to the following URL:
http://localhost:8888/image-url.php
This is the image that got created for me:
Every time you run the app you will likely get a different image.
2) Get Base64 encoding of the image
Create another file named image-b64.php with the following content:
<?php
require_once __DIR__ . '/vendor/autoload.php';$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);$dotenv->load();$client = OpenAI::client($_ENV['OPENAI_API_KEY']);$response = $client->images()->create(['model' => 'dall-e-3','prompt' => 'A panda flying over Paris at night','n' => 1,'size' => '1024x1024','response_format' => 'b64_json',]);foreach ($response->data as $data) {$data->url; // null$data->b64_json;}// display base64 encoded imageecho '<img src="data:image/jpeg;base64,' . $data->b64_json. '" />';?>
The only changes that were made are in the following lines of code:
(1) 'response_format' => 'b64_json',
Whereas previously, we requested a URL, this time we request base-64 encoding.
(2) echo '<img src="data:image/jpeg;base64,' . $data->b64_json. '" />';
When rendering the image, we use base64 rendering instead of an image URL.
Point your browser to http://localhost:8888/image-b64.php. This is what I experienced:
Conclusion
There are may services that you can consume from OpenAP, like chat completion, text completion, embeddings, etc.. Now that you know how things work, go ahead and try some of the other services.
No comments:
Post a Comment