# Midjourney PHP Library for Discord API Image Generation

This PHP library provides a simple interface for generating images using the Midjourney Bot through the Discord API.

![Midjourney PHP Library for Discord API](/.github/img/demo.gif)

![](https://img.shields.io/packagist/dt/ferranfg/midjourney-php)
![](https://img.shields.io/packagist/v/ferranfg/midjourney-php)
![](https://img.shields.io/packagist/l/ferranfg/midjourney-php)

## Installation

You can install this library using Composer. Run the following command in your project directory:

`composer require ferranfg/midjourney-php`

## Usage

### Basic usage

To generate an image using the Midjourney Bot, you first need to create an instance of the `Midjourney` class:

```php
use Ferranfg\MidjourneyPhp\Midjourney;

$midjourney = new Midjourney($discord_channel_id, $discord_user_token);

$message = $midjourney->generate('An astronaut riding a horse');

return $message->upscaled_photo_url;
```

### Constructor

- `$discord_channel_id` - Replaces this value with the Channel ID where the Midjourney Bot is installed. You can get the Channel ID right-clicking on the channel and **Copy Channel ID**.

    Remember that you can invite the Midjourney Bot to your own server to organize your work https://docs.midjourney.com/docs/invite-the-bot

- `$discord_user_token` - Automatic user accounts (self-bots) are not allowed by Discord and can result in an account termination if found, so use it at your own risk.

    To get your user token, visit [https://discord.com/channels/@me](https://discord.com/channels/@me) and open the **Network** tab inside the **Developers Tools**. Find between your XHR requests the `Authorization` header.

    ![Discord User Token](/.github/img/authorization.jpg)

### Methods

#### `$midjourney->imagine($prompt)`

This method generates an image using a prompt and returns an object that represents the message containing the generated image. The `$prompt` parameter is a string that will be used to generate the image.

```php
$imagine_object = $midjourney->imagine('An astronaut riding a horse');
```

#### `$midjourney->getImagine($prompt)`

This method returns a previously generated object that represents the message containing the image generated by the given prompt. The `$prompt` parameter is the string used to generate the image.

```php
$imagine_object = $midjourney->getImagine('An astronaut riding a horse');
```

#### `$midjourney->upscale($imagine_object, $upscale_index)`

This method upscales an image contained in the given object and returns the URL of the upscaled image. The `$imagine_object` parameter is the object returned from the `imagine` / `getImagine` methods. The `$upscale_index` parameter is an integer between 0 and 3 that represents the option provided by the MJ bot we want to upscale.

```php
$upscaled_image_url = $midjourney->upscale($imagine_object, 2);
```

#### `$midjourney->getUpscale($imagine_object, $upscale_index)`

This method returns the URL of a previously upscaled image generated by the given message and with the given option of upscaling. The `$imagine_object` parameter is the object returned from the `imagine` / `getImagine` methods. The `$upscale_index` parameter is an integer between 0 and 3 that represents the option provided to the MJ bot we upscaled.

```php
$upscaled_image_url = $midjourney->getUpscale($imagine_object, 2);
```