If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Next JS comes with an image response class that allows you to dynamically create images. The image response is designed to be returned as its own HGTP response containing an image payload, and you can generate this image using JS six. To demonstrate, we create a new API endpoint and we bring in the image response class. From next js, we export a simple get handler and we are going to return an image response as a part of the response from this API endpoint. The image response class takes a JSX element as its first parameter.
00:31
We are going to return a simple D and start it using CSS properties. We ensure that this route dev takes up the entire space provided by the parent and we set up a flexbox layout, which is going to center its content for our content. We are going to render a simple dev that displays a sample message. The second parameter to the image response constructed takes a configuration object, for example, HA setting the width and the height of the generated image. Let's demonstrate this generated image within the browser. As you can see, we get a simple image of size 300 without text displayed in the center. We can actually use this image anywhere else
01:05
within our application as well. For example, within a route page, we render out a simple image element and point the source to this new API endpoint. And now if you visit this page within the browser, we can see our sample image being displayed nicely within a page. Note that image response is not a complete browser implementation of HTML and CSS and is instead a stripped down engine inspired by React Native. The underlying engine is a project called curi, which is free and open source on GitHub and it calls itself an enlightened library to convert HTML and CSS to SVG.
01:37
This shouldn't mean much in terms of feature availability, but it is something to be aware of in case you run into issues. As a matter of fact, the course completion certificates at Bullion Art are generated on the fly using image response, and it is helpful to create utility components to make it easier to build advanced layouts. We will create our components in a new TypeScript file and the first component that we will create is called an image response route. This is going to set up the root iv, which is going to take up the entire space from the parent, set up a nice flex box column layout, and additionally have position relative so we can absolutely position any
02:10
children using this parent. It is common to have a nice background in the generated image and for this purpose we create this component called image response. Full background image. It takes a source image prop and then generates an image element that is absolutely positioned to take up the entire space provided by the parent to displace some centage text. Within the generated image, we create a new component, which is going to use the CSS Flex box to center align its children. At this point, feel free to build any other components that you need for your specific use case. One more utility that we will build is a function that takes a local path to an image
02:42
and then converts it into a basic 64 encoded image so that it can be immediately passed as an SRC attribute for an image element. This way image response doesn't need to make an external HTP request in order to load a local image file without brand spanking new utilities created. Let's Demonstrate them within an HTP endpoint. We start off by returning an image response that contains the image response route, followed by the image response, full background image for which we'll load an image from a public folder, and then we display a nice centered text within the generated image.
03:13
The background image that we are using is 10 80 by 10 80, and that is the size of the image that we will generate with image response. Let's take a look at our generated image. Within the browser, you can see we have a nice background with some nice scented text. The image response class allows you to pass in a set of custom fonts, which enables expressive topography. We will load our custom fonts using a new utility file. Unfortunately, next JS does not expose the type definitions for its font options, but we can use the skills we learned in our TypeScript course. To infer that type.
03:44
From the image response constructor, we use the TypeScript built-in constructive parameters utility to fetch the constructor for image response. Then we pick its second parameter that is the options object. Next, we convert it to non knowable to remove undefined from one of the options. Then we read the fonts property and once more we convert it to non knowable and then read the type of the items within that array. By using a lookup type. If these concepts seem scary, you can master all of them. Within our text strip course, the font that we will be loading is called galion, and then we create a utility to load this font from our public folder
04:17
and then return an object that matches the type expected by xj, which we have stored in font options with the font loader created. Let's use this within our image response. Within our API route, we load the font name along with the font loader for our sample text. We set the font family to the name of the font, and then we provide the font file as a part of the configuration object for image response. Let's demonstrate this within the browser, and as you can see, our simple text is now stylized using our custom font. In order to provide custom content for your generated image, you can pass in parameters to your API route.
04:51
We can read URL parameters from the incoming next request. URL property. We create a new URL object and from its search pyramids we get the message per and if the search parameter is not provided, we default to no message. Next, instead of displaying sample text, we use the past in message with a search parameter wide in. Let's demonstrate this within the browser. If you don't provide a message parameter, we get no message, and if you provide a message, for example, below world, then that is the message that we see in the generated image. A simple way to debug layout issues with image response is
05:24
to pass in true for the debug property within the options object. For image response, we provide debug and set its value to true. This will add a nice border around the different elements that image response is going to use to generate the image. Let's demonstrate this. Within the browser, within the generated image, you can see a nice border around the different portions of the text, along with the very thin red border around the background image. If any of these borders are too thin for you, you can always explicitly add borders around the specific elements that you want to debug.