Create Music Art Covers Using AI in Next.js

Divine Orji

Art is an expression of creativity, and the way it is created constantly evolves. In the past, it had to be done manually, leaving room for errors and a lot of stress. Now, you can create art at the click of a button.

In this article, you will learn how to create a custom music album cover art using text and any image of your choice and enhance it using Cloudinary’s Neural Art Style Transfer add-on.

Cloudinary is a cloud-based media management service that stores, transforms, optimizes, and delivers images and videos to your websites and applications. It also provides a variety of add-ons to enhance its capabilities and provide new and exciting ways to manage your media files.

CodeSandbox & GitHub Repo

You can view a complete demo of this article on CodeSandbox below:

https://codesandbox.io/embed/nextjs-music-art-demo-f87vjd?fontsize=14&hidenavigation=1&theme=dark

1<CodeSandbox id="nextjs-music-art-demo-f87vjd" title="Next.js music art cover"/>

Explore its source code on GitHub, or check out a Vercel-hosted live version.

Pre-requisites

To follow along with the steps presented in this article, you should have:

  • Good knowledge of functional programming with JavaScript and React.
  • A Cloudinary account. Create a free one here.
  • Experience working with NextJS will make this article easier to follow, but it is not required.

Set up the project

In your terminal, navigate to your preferred directory and run the command below to download and set up the starter files for this project:

1yarn create next-app nextjs-music-art-demo -e https://github.com/dpkreativ/nextjs-music-art-starter

After its successful setup, open the project in your preferred code editor to view its source code. Run the command below to view it in your browser on localhost:3000

1yarn dev

Register Cloudinary’s Neural Artwork Style Transfer add-on

In your browser, log into your Cloudinary dashboard and click on the “Add-ons” tab to view the add-ons available on Cloudinary, then scroll down till you find and click on “Neural Artwork Style Transfer”:

Click on “Free” to subscribe to the free plan, which allows you to create up to 15 artworks each month:

Allow unsigned add-on transformations on Cloudinary.

In your Cloudinary dashboard, click on the “Settings” icon on the right side of the navigation bar. Click on the “Security” tab and scroll down till you locate “Unsigned add-on transformations allowed”:

Ensure that the “Neural Artwork Style Transfer” box is checked, then scroll down and click on the “Save” button.

Install and set up Cloudinary in your project

In your project’s terminal, run the command below to install Cloudinary:

1yarn add cloudinary

After its successful installation, store environment variables by creating a .env.local file in the root directory of your project and update it with the code below:

1CLOUDINARY_NAME=<YOUR CLOUDINARY CLOUD NAME COMES HERE>
2CLOUDINARY_KEY=<YOUR CLOUDINARY API KEY COMES HERE>
3CLOUDINARY_SECRET=<YOUR CLOUDINARY API SECRET COMES HERE>

Open your Cloudinary dashboard in your browser and copy your Cloud Name, API Key, and API Secret. Update the values in .env.local with the Cloudinary credentials you got.

Create an album art using your uploaded image and text

In your code editor, create a uploadAlbumApi.js file in the pages/api/ folder and update it with the code below:

1const cloudinary = require('cloudinary').v2;
2
3cloudinary.config({
4 cloud_name: process.env.CLOUDINARY_NAME,
5 api_key: process.env.CLOUDINARY_KEY,
6 api_secret: process.env.CLOUDINARY_SECRET,
7});
8
9export default (req, res) => {
10 const text = req.body.text;
11 const image = req.body.image;
12 return cloudinary.uploader.upload(
13 image,
14 {
15 transformation: [
16 {
17 overlay: {
18 font_family: 'Montserrat',
19 font_size: 144,
20 text: text,
21 },
22 },
23 { flags: 'layer_apply', gravity: 'north_east' },
24 ],
25 },
26 (error, result) => {
27 if (error) return res.status(500).json({ Error: error });
28 return res.status(200).json({ data: { ...result } });
29 }
30 );
31};

In the code block above, you did the following:

  • Imported cloudinary and set the value of its config parameters - cloud_name, api_key, api_secret - with data from your .env.local file.
  • Retrieved the text and image data from the request payload.
  • Used the Cloudinary upload method to upload the image, applying the text as an overlain transformation.
  • Handled any error from the upload operation and returned the data on a successful image upload to Cloudinary.

In your pages/index.js file, update the handleAlbumSubmit() function with the code below:

1// === ONSUBMIT FUNCTIONS ===
2 /* Handle Album Submit:
3 - This function uploads your image and text data to create your album art.
4 */
5 const handleAlbumSubmit = async (e) => {
6 e.preventDefault();
7 setLoadingMyAlbumArt(true);
8 try {
9 const { data } = await fetch('/api/uploadAlbumApi', {
10 method: 'POST',
11 body: JSON.stringify(myAlbumArtData),
12 headers: {
13 'Content-Type': 'application/json',
14 },
15 }).then((res) => res.json());
16 setMyAlbumArtData({ ...myAlbumArtData, text: '', image: '' });
17 setMyAlbumArtPreview(data);
18 setLoadingMyAlbumArt(false);
19 } catch (error) {
20 console.log({ Error: error });
21 setLoadingMyAlbumArt(false);
22 }
23 };

In the code above, you

  • Used e.preventDefault() to prevent the page from reloading and setLoadingMyAlbumArt() to true, which triggers the loading state of the submit button.
  • Used a try-catch block to send a fetch() request to your uploadAlbumApi, with myAlbumArtData as its body. It returns a JSON response containing your data object.
  • Cleared the input fields using empty strings in setMyAlbumArtData() and stored the contents of your data object using setMyAlbumArtPreview.
  • Turned off the loading state of the submit button using setLoadingMyAlbumArt(false), and in the catch block, you logged errors to the console.

At this point, you can create an album art through Cloudinary by uploading an image and a text. Test it out on your browser:

Apply Neural Style Transfer on album art

In your code editor, create a uploadPopArtApi.js file in the pages/api/ folder and update it with the code below:

1const cloudinary = require('cloudinary').v2;
2cloudinary.config({
3 cloud_name: process.env.CLOUDINARY_NAME,
4 api_key: process.env.CLOUDINARY_KEY,
5 api_secret: process.env.CLOUDINARY_SECRET,
6});
7export default (req, res) => {
8 const image = req.body;
9 return cloudinary.uploader.upload(image, (error, result) => {
10 if (error) return res.status(500).json({ Error: error });
11 return res.status(200).json({ data: { ...result } });
12 });
13};

Similar to the previous image upload API function you created above, this function uploads an image to Cloudinary, only this time, without image transformations.

In pages/index.js, create the handlePopArtSubmit() function with the code below:

1const handlePopArtSubmit = async (e) => {
2 e.preventDefault();
3 setLoadingPopArt(true);
4 try {
5 const { data } = await fetch('./api/uploadPopArtApi', {
6 method: 'POST',
7 body: popArtPreview,
8 }).then((res) => res.json());
9 setPopArtData(data);
10 setLoadingPopArt(false);
11 } catch (error) {
12 console.log({ Error: error });
13 setLoadingPopArt(false);
14 }
15 };

In the code above:

  • You set the loading state of the Apply Neural Transfer button to true.
  • You made a POST request to uploadPopArtApi, with popArtPreview as the request’s body.
  • Your response is a data object in json format, which you destructured into the { data } variable.
  • You then add the data gotten from the API response to the popArtData state.

Scroll down your pages/index.js file till you see the code below:

1{/* === SECTION THREE: DISPLAY AI-GENERATED ALBUM ART ===
2 In this section below:
3 - You're creating a dynamic image URL with Neural Art Style Transfer parameters applied.
4 - The URL is set as the image src and displays the AI-generated album art.
5*/}
6<section className="md:col-span-2">
7 <div className="w-full h-72 border-blue-400 border-2 rounded-lg">
8 {popArtData !== undefined ? (
9 <img
10 src={`https://res.cloudinary.com/dpkreativ/image/upload/l_${popArtData.public_id}/e_style_transfer,fl_layer_apply/${myAlbumArtPreview.public_id}.${myAlbumArtPreview.format}`}
11 alt="final image" className="object-contain h-full w-full"
12 />
13 ) : (
14 <span className="self-center flex flex-col space-y-3 p-5">
15 <p className="italic">Final image here</p>
16 </span>
17 )}
18 </div>
19</section>

In the code above, using the Cloudinary dynamic URL transformation parameters, you added a neural style transfer layer from the new image popArtData to the original image myAlbumArtPreview.

Here is the final demo:

Conclusion

In this article, you learned how to create and customize your music cover art using NextJS, Cloudinary’s transformation parameters, and neural style transfer AI. You can explore it further by browsing through Cloudinary’s official documentation on image transformations or neural style transfer add-ons.

Resources

Divine Orji

Software Engineer and Technical Writer

I am a software engineer passionate about building fast, scalable apps with beautiful user interfaces.