🌟 Help others discover us, star our GitHub repo! 🌟

Mobile Menu

Next-generation Image Handling with Webiny File Manager

Ath TripathiTwitter
December 14, 2022

Today we will learn about one of the main features of Webiny, its file manager. We will go in depth about its use and demonstrate why it is superior to file managers in other content management systems. We will talk about how to use its features in detail, along with how to render images using the GraphQL API and in a simple HTML document.

Today we are going to cover the following topics:

  • What is the Webiny File Manager and what are its benefits?
  • How to do file manipulation in the file manager (uploading, deleting, renaming)
  • How to use the Webiny image editor for general tasks (cropping, flipping, filtering, and rotating)
  • How to render image data through the GraphQL API
  • How to set an image in an HTML file using the srcset attribute
  • Accessing image data from the API using third-party software like Insomnia
  • Mitigating cumulative layout shift (CLS) issues

What Is Webiny?

Webiny is an open-source content management system designed for enterprises. It uses serverless architecture for cost-effective performance and better scalability.

Serverless architecture is a way to develop services without worrying about infrastructure. Simply write your code and deploy it to the cloud. It has lower latency, lower cost, quick deployment, high speed, and less mental fuss. Building on top of those services, Webiny makes building advanced applications easier, quicker, and more enjoyable.

Some features of Webiny which make it a really great CMS for projects:

These features, along with the file manager, make a great starting point for a wide range of uses.

Introduction to Webiny File Manager and Its Benefits:

Webiny includes a scalable and serverless asset management system. It is a built-in Webiny app that you can easily access via the UI in a web browser, or via it’s API. While dealing with file data types, the file manager is used for inputting and uploading files. The infrastructure helps to keep a single version of each image in your project or web pages and provide it to different users on the basis of their devices.

Dynamic serverless image editing allows you to create images on the fly and serve them to multiple devices without much headache or issue. You can flip, filter, crop images, and perform many other functions for different use cases.

Let's talk about some of it's key features.

1. Structure and Organization:

You can add different tags to a single image. These tags help you to find that image even if you have hundreds of image assets. Along with tags, you also get a searching feature, which is powered by elastic search in the DynamoDB + ElasticSearch version of Webiny.

Webiny is built to be modified, so you can tweak React components to make changes in the File Manager UI, upload and delete files via the API, and you can also make changes to the GraphQL schema for delivering images.

2. Large Data Handling:

Webiny uses an S3 bucket for storing all files; this architecture is mainly for scalability and easy handling of data. The S3 bucket is Amazon’s cloud storage resource, which is like files and folders and stores simple data and descriptive metadata.

3. Image Editor:

The built-in image editor is my favorite feature of the file manager. You can crop, flip, rotate, and apply filters to images. It is a collection of features that can help you save time, resources, and effort in a variety of tasks.

4. Image API:

The best part of the image API is that the CDN caches the images. So you will be getting a CDN link for images, and by default, every uploaded image is set to some preset sizes.

This is a graphQL API, so you can choose very specific data to render images in a UI.

How to Upload an Image in the File Manager

I am assuming that you are done with creating your Webiny project and deploying it. I also assume that you also made yourself an admin user, and now you are on the home page of your application.

It will look like this:

capture

Now click on the upper left hamburger icon, and it will open a sidebar. For manageability, we will be creating both "groups" and "models." Click on the hamburger icon, and on the left sidebar, under "Content Model," click on the group:

2 1

Now you will be redirected to the Group page, where you will see a button that says "Save Group." Enter the data as shown in the photo below and click on "Save content model group":

3 1

A new group with the name "Photo" will be created; now click on the hamburger icon again and choose models from the option that was below the group:

4 1

You will arrive at the model's page; click the "New model" button, and a popup will appear; fill in the details (in my case, I filled in the name "Image," and chose the group "Photo":

5

You will reach the model editing page, where you will see a UI like this:

6

Now we will be needing to drop the Files and Text data type; hold "FILES" and drop it in the blue dotted box. A new popup will appear as follows:

7

In Label, put "File name," toggle the "Image only" option, and click on the Save Files button.

Now click on the Save button on the top right corner to save the model:

8

Now you are ready to start uploading your files. For that, click on the hamburger icon again and scroll down. You will see a star icon and a "PHOTO" group written below it. You will find "Images." Click on it:

9 1 1

On clicking it, you will reach a new page. Click on the "NEW ENTRY" button, and it will be something like this:

10 1

Click on the area covered in the red box and choose an image; it will open a new window; then click on "Upload":

11

Now click on “SAVE AND PUBLISH” and you successfully uploaded your first file on Webiny.

Duplicating, Renaming, and Deleting File in the File Manager:

Now we learned how to upload files now let’s learn about file manipulation. Let’s start with renaming the image.

Come to this page again:

12 1

Click on the image and the file manager interface will open:

13

When you hover over the image you will see a gear icon, click on it and it will open a right sidebar, where you can change the image name:

11 1

In the red box, I marked write any name and click anywhere, it will change the name. Like following:

12 2

Now let's duplicate the image. For that click on the gear icon again and edit image button:

22

It will open a new page and just click on save without doing anything, it will duplicate your file:

edited3.png

It will have duplicated the image:

18

Now for deleting, again click on the gear icon and click on the delete button:

edited2.png

It will delete the image. Now we are all done with basic file manipulation, now let's jump to the image editing part.

Editing Images in the Image Editor:

Getting back to the same page:

21

Click on the gear icon and choose the edit button:

22

Click on the pencil icon and it will open a new window, we will start with cropping:

23Cropping

Now  click on the cropping option between the red box for cropping the image, now you will be on a new page:

24Crop

Click on apply button then the save button and it will save the cropped image.

Next, we will flip the image:

24Flipping

Click on the function inbox and you will reach a new page:

25flip

Click on “FLIPX” or “FLIPY” and then “APPLY” and hit save. (i do FLIPX, which will show all results in end.)

Let's go with Rotate now, click on the function in the red box:

26

Now you will see a new page, you will get a range of input(i am rotating at 90 degrees):

26rot2

Now again click on Apply button and then save.

Now let’s move on to the last function and most interesting one, which is the filter, so click on the function between the red box:

27

Now you will see a bunch of filters there and you can tweak them and see real-time changes on the image:

27filter2

Now exactly as before just click on apply and save. Now let’s see all our results:

Background

Here are all our results, hope you understood how each function for image editing work.

Connecting to and Fetching Images From the GraphQL API:

Now that we already know about image and file manipulation, let's learn how to get those images through the GraphQL API.

First let’s create the API key for authentication purposes so our third-party software or application can access our models through graphQL APIs, so first click on the hamburger menu icon, and on the left sidebar scroll down and under "settings," find "API keys":

28Api

Now you will be on the API keys page, now click on the “NEW API KEY” button and fill in the following in Name and description:

29

Now scroll down and under Permissions and under Content select “All locales”:

30

Now scroll down and under “Headless CMS” set the access level to “custom access” and select “read”:

31

Now scroll down more and click on  “Save API KEY”. After you have done that scroll upward and under the token copy the token:

Background (8).png

Now after it is all done go to the API playground and shift to the Read API tab:

Background (7).png

That link in the red box will be where we are going to send the request.

Now open any your API inspection software of choice, such as Postman; I am going to use Insomnia.

Now set the POST request to the link and switch to authentication and set it to "Bearer" and paste the token that I asked you to copy:

Background (8).png

Now in the data structure set it to graphQL and put these queries:

{ listImages { data { fileName } } }

Before making a query, make sure to play in the API playground. I'm not going to explain how to make queries in Playground, but it should be the same as above if you followed exactly like mine, now paste it in as follows:

Background (5).png

Now click on send and you will get the following results:

Background (4).png

Now I think we are done with the connection with API now let’s learn about setting images using srcset in HTML.

Setting Image in HTML:

Now let’s create an HTML file where we would render our Webiny pics in our file while using the srcset attribute.

Make sure to upload different sizes of images. I am using one image at 499x320 pixels, and the a copy at 998x640 pixels.

We will be needing the URL that we get in our experiment above:

Background (4).png

Copy the given link into the red box and we will use it. Now create an HTML file and write the code below:

<!DOCTYPE html> <html> <head> </head> <body> <h2>IMAGE:</h2> <body> <img alt="planets" src="https://xxxxxx.cloudfront.net/files/9l9ftcskb-bg.jpg" srcset=" https://d22c5199iq663l.cloudfront.net/files/9l9ftcskb-bg.jpg 1x, https://xxxxx.cloudfront.net/files/8lafh4zr8-download.v1.png 2x " /> </body> </html>

We are using src as a fallback because some older browsers don’t understand the newer srcset api.

It is simple to understand we set the width of the image using sizes and etc, here is the result:

23Cropping

Using this method, you can use the file manager to store image for a range of uses, including embedding in online documentation and for use in CRMs or other tools your organization already uses.

Mitigating Content Layout Shift Issues:

First, let's understand what a CLS issue is. It is a layout shift of the image when content like an image or text moves around when it loads in. The simplest way to avoid CLS for images is by adding aspect ratios and using width and height attributes in our HTML.

Now we will simply make some changes to our image model and add the default width and height of the image manually.

First, go to the  model list and choose edit model:

cls

Click on edit model and add two number fields:

cls2

Add the following details in the field:

cls3

Similarly, add one for height and everything will look like the following:

cls4

Hit on save and you are done with defining aspect ratios.

Now get to the entry page of the image from the left sidebar:

9 1 1 And then add the width and height as follow:

Capture.PNG

Now you can quickly get the width and height of the image to avoid the CLS issue from API:

Background (2).png

Like above we are getting width and height also as the parameter, we can use them when we call API from a JS script and then add default width and height as follows:

<!DOCTYPE html> <html> <head> </head> <body> <h2>IMAGE:</h2> <img width="499" height="320" srcset="https://xxxxx.cloudfront.net/files/9l9ftcskb-bg.jpg 120w" alt="" /> </body> </html>

Result for this:

res2.PNG

By creating a content model for your image we can solve the layout shift issues by defining the aspect ratio of the image. This method can be used to create alternative text for screen readers too.

File Managers Compared: Webiny vs Strapi

Strapi, an open-source headless CMS is a name that often comes into my mind when we talk about CMS. Strapi's file manager provides you with some features similar to Webiny, including multiple file uploading, live editing, responsive formatting etc.

Both CMS have many similarities between them along with some key differences in their offerings and working style which can be big factors when deciding the CMS for your project.

We are going to compare factors like hosting, storage services, CDN, scalability, and multi-tenancy.

Let’s now talk about them in detail.

1. Hosting and Setting Up Image Storage

Webiny is hosted on AWS, making the application completely serverless. Create your project file and deploy it without any configuration by using a simple deployment command. You won’t need to worry about configuring the database or the image hosting. All services are built-in.

Strapi, on the other hand, can be hosted by any provider. With Strapi, setting up different databases and storage services will cost you extra money and time. If we talk about hosting on AWS, you will have to create an S3 bucket separately that stores files that can be served through your application. You will need to connect the AWS RDS service for managing and hosting databases separately. Suppose you want to use another service, like MongoDB. In that case, it will again need extra configuration.

2. CDN Services

In Webiny, when you upload any image, it is automatically cached in the CDN as it is a built-in feature of Webiny. With the Webiny image serving API, you can get the CDN link for any image that you upload. It uses Amazon’s CloudFront for this purpose.

To provide efficient image delivery with Strapi, you will need to set up a CDN service yourself, like Cloudinary or CloudFront, separately.

3. Multiple Project Support

With Webiny, you can host the backend once and use it for many projects. This is because Webiny supports multi-tenancy without extra configuration.

Strapi at the time of writing doesn’t support multi-tenancy. Because of this, you need to host different backends separately for different projects.

4. Scalability and Other Indirect Costs

When you are developing a large project, you will be needing to manage large amounts of data and files. In case of scaling the project, you will be needing to hire DevOps and engineers for handling your infrastructure. This means extra time on human resources and more time building configuration tools. Webiny has the advantage of built-in scaling, both to increase it’s capacity and to reduce it to zero automatically, facilitating efficient use of funds and time. On the other hand, scaling Strapi needs to be managed by your internal team, whether it’s increasing Kubernetes pods, allocating extra resources, or adding extra layers of read/write capacity.

5. Security

Webiny utilizes the AWS user authentication system AWS Cognito, by default. It doesn’t depend on it’s own internal tools, like Strapi does by default. Webiny also has pre-built configuration for Okta and Auth0 which is available on the Enterprise plan.

Leveraging existing systems for authentication means that security is delegated to the experts who work on these issues 24/7, and avoids having to quickly patch a system once a security flaw is detected.

There are other benefits as well, such as AWS CloudFront’s built-in DDoS protection, which means even whilst under attack, the system can still continue to function.

Summary:

We hope this article has helped you decide why to choose Webiny, the benefits of it’s infrastructure, and how it saves costs and resources. We’ve also learned about the file manager and its built-in tools.

If you would like to learn more about Webiny or talk to one of our advisers, please contact us today.


This article was written by a contributor to the Write with Webiny program. Would you like to write a technical article like this and get paid to do so? Check out the Write with Webiny GitHub repo.

Find more articles on the topic of:contributed articles

About Webiny

Webiny is an open source serverless CMS that offers you all the enterprise-grade functionalities, while keeping your data within the security perimeter of your own infrastructure.

Learn More

Newsletter

Want to get more great articles like this one in your inbox. We only send one newsletter a week, don't spam, nor share your data with 3rd parties.

Webiny Inc © 2024
Email
  • We send one newsletter a week.
  • Contains only Webiny relevant content.
  • Your email is not shared with any 3rd parties.
Webiny Inc © 2024
By using this website you agree to our privacy policy
Webiny Chat

Find us on Slack

Webiny Community Slack