Join Carmen as we apply advanced transformations to our images leveraging the Sharp API.
Speaker 0: Hello, everybody. My name is Carmen, and welcome to Sharp Focus, the show where we truly capture how it is that allows you to transform your images and truly make them shine. Now in previous episodes, we've seen how Directus manages your images for you, how you can set permissions, how you can use the SDK to manipulate the width, the height, the fit, the quality, the performance, the format of your images, and much more. But in this episode, we're gonna get into some advanced stuff, my friends. We're gonna be looking at how Directus exposes the Node JS Sharp API to allow you to do some truly wild transformations.
I'm talking things like rotating images, resizing them, shifting them, tilting them, applying color tints, negation. Oh gosh. There's so much. But don't take my word for it. Let's just hop into some code.
Let me just grab my camera here and let's get right into it. See you there. Here we are in my directus project dedicated to my photography, and we'll be taking a look at one of my photos from my recent trip to Chile. In this episode, we'll be working with this photo from Rapa Nui or Easter Island. And as said, we'll be working with advanced transformations.
In order to have advanced control over your files, Directus exposes the full Sharp Node JS API, which has a bevy of options to allow us to work with our images. I'm talking things like rotating, flipping images, flopping images, sharpening, blurring, flattening, negate, color negation, color manipulation, channel manipulation, and many other options. So let's play around with some of these. We'll be working once again with our Nuxt applications, applying some of these transformations. Now you can see that I've applied the width attribute, but we can expand on this using the transforms property, which is an array of Sharp API calls.
And these are applied as array pairs of property and value. So for example, I'm gonna use the resize property, which allows me to resize my image and make it 1,000 pixels wide. Thereby replacing the width property. And I'm just showing this as something that's possible to do. We're not gonna see much of a difference yet, but let's make things a little bit more interesting.
Let's add another advanced transformation. How about rotation? Let's rotate it by 90 degrees. Fabulous. Let's try some other stuff and you can see, I can concatenate several transformations.
For example, let's make it blurry. Let's blur it by 45%. Yep. That's a bit much. Let's blur it by about 15%.
Cool. Let's make it 5%. Just give it a little bit of an enigma here. Fantastic. Finally, we'll do something just a touch more enigmatic, which is to use the negate transformation.
We'll negate it. And you see didn't pass any properties and it already reversed the colors. Yep. It's totally possible to concatenate several operations and you can apply them in different orders for different effects. For example, now that I've resized it to be a 1000 pixels wide after I've rotated, you can see the image is wider.
But what if we wanted to have that height back? Well, the resize transformation does take several parameters. For example, let's make it 500 pixels high. Cool. But, yeah, as said, advanced transformations and other manipulations can be applied in combination.
So what you can do, for example, is say I want to reduce the quality of the ultimate image. Let's make it 25%. Save that. And now I've got a blurry negated rotated image. Alrighty.
Well, I don't know about the rest of y'all, but I'm always so impressed with how much you can do using these advanced transformations. And as you can see, it can get pretty cool, all powered by Directus' API and SDK. So what's next in store? What we're gonna cover in the next and final episode is how you can apply all of the stuff we can been doing, but with only presets, which means not having to repeat yourself. And as software developers love to keep stuff D R Y, don't repeat yourself.
So let's find out how we'll do that in the next episode. But until then, keep those cameras handy, and I'll see you next time.