Generate an image with ChatGPT
L(i)VID

Generate an image with ChatGPT

Part 4 is dedicated to generating an image via ChatGPT using its “dall-e-3” model. The image will be generated programmatically. Once generated, we'll retrieve the URL so that we can insert it into an HTML page. We'll do this in PHP.

Code: generateImage()

    function generateImage( $material )
    {
        $apiKey = 'YOUR_API_KEY'

        $url = 'https://api.openai.com/v1/images/generations';

        $data = [ 'prompt'  => $material    ,
                        'model'   => 'dall-e-3'     ,
                        'n'           => 1                  ,
                        'quality'  => 'hd'              ,
                        'style'     => 'natural'        ,
                        'size'      => '1024x1024'  ,
                 ];

        $ch = curl_init($url);

        curl_setopt( $ch,CURLOPT_URL                        , $url);
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true);
        curl_setopt( $ch,CURLOPT_POST                      , true);
        curl_setopt( $ch,CURLOPT_POSTFIELDS           , json_encode( $data ) );
        curl_setopt( $ch,CURLOPT_HTTPHEADER         , [ 'Content-Type: application/json',
                                                                                      'Authorization: Bearer ' . $apiKey
                                                                                    ] );

        $result = curl_exec( $ch );
        curl_close($ch);

        $response = json_decode( $result,true );

        if ( isset( $response['error'] ) )
        {
            return ( null );
        }
        else
        {
            return ( $response['data'][0]['url'] );
        }
    }        

The function takes 1 parameter : $material

$material is the prompt to create your image. Example: "Generate an ultra-realistic image of a futuristic web radio studio. It's painted in shades of deep red and white. A South Asian female radio host is doing her show, fully immersed in her work. The room is filled with highly detailed and sophisticated broadcasting equipment, which includes microphones, headphones, control panels, and computer screens. The overall setting has a modern futuristic vibe."

The function is called by the following code:

$material = "Generate an ultra-realistic image of a futuristic web radio studio. It's painted in shades of deep red and white. A South Asian female radio host is doing her show, fully immersed in her work. The room is filled with highly detailed and sophisticated broadcasting equipment, which includes microphones, headphones, control panels, and computer screens. The overall setting has a modern futuristic vibe.";

// No error handling in this simple example
echo "<p><img src='" . generateImage( $material ) . "' /></p>;        

The result has been a URL that points to the image that got just created:

An image generated by ChatGPT in the programmatic mode from within PHP

This is also the image I used to illustrate this article.

Of course ...

This call to ChatGPT presupposes your have already an account and an API Key, which is precisely the subject covered in the second article of this series.

Conclusion

We have one more weapon in our arsenal: we can generate images programmatically by invoking ChatGPT. Our function's code is written in PHP, but you'll have no trouble transforming it into Python, node.js, JavaScript, etc.

See you soon for more examples and ... above all ... no complications: keep things simple, but no simpler than they need to be.

Previous article - Next article



要查看或添加评论,请登录

Patrick Boens的更多文章

  • Using templates to mimic sophisticated object

    Using templates to mimic sophisticated object

    The Power of Templates in IllicoDB3: Revolutionizing Database Structure In the realm of traditional databases, the…

  • More, Faster, Easier...

    More, Faster, Easier...

    In my previous article, I showed you how to create a small todo management app in record time. Now, based on this first…

  • AI at the Developers' Bedside

    AI at the Developers' Bedside

    Let's dive into the world of AI-powered app creation, showcasing just how effortless it can be to bring your ideas to…

    1 条评论
  • AI and IT

    AI and IT

    Here's the question I asked to "Sonar Huge", the AI model of Perplexity.ai: With the advent of AI taking by storm the…

  • The Imperative of AI in Digital Transformation Projects: A Wake-Up Call for Businesses

    The Imperative of AI in Digital Transformation Projects: A Wake-Up Call for Businesses

    In today's rapidly evolving digital landscape, companies that continue to approach their digital transformation and…

  • Data Migration Projects

    Data Migration Projects

    "Mise en bouche" My first interesting encounter with a data migration project dates back to 1999. The tech world was…

  • IllicoDB3 and AI

    IllicoDB3 and AI

    I have mentioned in the past that IllicoDB3 allows the use of artificial intelligence to generate code related to…

  • IllicoDB3 : The New Features

    IllicoDB3 : The New Features

    As I mentioned in a previous article, IllicoDB3 has evolved significantly, and part of its evolution stems from…

  • IllicoDB3 Evolutions

    IllicoDB3 Evolutions

    A lot has transpired since the ten articles from Quitus, particularly regarding the Small Business Suite—a business…

  • Continuation and Conclusion of MVP #1

    Continuation and Conclusion of MVP #1

    We have reached the final stage of our journey into establishing components and, more importantly, how they interact…

社区洞察

其他会员也浏览了