swr@1.1.0

swr@1.1.0

swr (Smart Web Resouce) is the base for an high performance image server. It sends image requests including transformations indicated in the url.

By @mantradev
Depends on files
images

Install with:

$ mantrad download-component swr@1.1.0

Or install last version with:

$ mantrad download-component swr

README.md

swr Mantra component - Introduction

Smart Web Resource is a component to generate images on-the-fly.

Depends on "files" component you can find in oficial Mantra components repository.

The transformations of the image are indicated in the same url of the request, like these examples:

http://yoursite/swr/resize:w(120)/-/logos/img/mantralogo.png

This request will serve mantralogo.png with a size of 120 pixels (indicated in url plugin 'resize:w(120)').

Another example:

http://yoursite/swr/resize:w(120)/bw/-/logos/img/mantralogo.png

Will apply the same above transformation ("resize") plus "bw" transformation (converts the image to black and white).

A number of transformation plugins comes with this component and it is easy to add more. Transformation plugins are located at "/lib/plugins" folder.

The component expects you to define some properties in your project, described below.

For the image transformations, swr uses jimp module.

Because image transformation can be an intensive CPU process, all transformation requested are cached (if you indicated that in the configuration of the component in your project).

Transformation plugins

swr component integrates a number of transformation plugins. These plugins are located at "/lib/pluglins" folder and they use jimp library to perform image transformations.

In current version, these are the plugins used.

resize

This transformation plugin implements "resize" transformation:

http://yoursite/swr/resize:w(100)/-/mypicture.png

In the url command (besides "reside"), expects these params:

  • w(x): width x in pixels of the image to send.
  • h(y): height y in pixels of the image to send.

Both of the params are optional but at least one of them should be indecated (obviously...).

bw

This transformation plugin transforms an image in a gray scale:

http://yoursite/swr/bw/-/mypicture.png

With this url, mypicture.png will be sent in a gray scale.

Concatenation of plugins url commands

In any request, you can concatenate current available transformation plugins, getting a great level of image manipulation.

In this example, an image is served with 800 pixels height and in gray scale:

http://yoursite/swr/resize:h(800)/bw/-/mypicture.png

See that 'resize:h(800)/bw' indicates to swr component to use two transformations: "resize" (with 800 pixels height) and "bw" after the "resize" transformation.

Images server cache

Because the transformation of any image can be an intense CPU process, swr component caches all transformations requested.

If a client asks for:

http://yoursite/swr/resize:h(800)/bw/-/mypicture.png

, the image served will be sent after generating it for the first time.

The second (and the following times) that exactly the same image transformation is requested, will be sent and image cached previously. This gets a high performance in swr component.

Image resources that swr can serve

In any Mantra application, images can be located in different locations:

  • Inside "[component name]/img" folder, like this sample:
http://yoursite/swr/resize:w(120)/-/logos/img/mylogo.png

In this example, and image is expected to be located at "logos" component, inside "img" folder.

  • Inside any location at frontend folder, like this:
http://yoursite/swr/resize:w(100)/-/assets/images/linkedin-logo.png

In this case, swr will look for the image inside "<front end folder>/assets/images/linkedin-logo.png"

  • As a file ID because all images are stored with "file" Mantra component.

Finally, because all transformations files are stored using "file" component, you can also ask for an image given its file id, like this:

http://yoursite/swr/<file id>

Where <file id> is a 32 character string provided by "file" component for a given image file, like this: "1454883da39743afa9badbf9e13a4443".

Component properties

swr component expects a number of properties you can change in your project:

  • filesRepositoryLocation: local or absolute folder where swr defines its files repository.

  • aliases: an object indicating a number of swr aliases transformations.

  • removetemponapp": name of the Mantra application where swr should perform removing of old temporal files when "system.cleanup" event is raised by Mantra in the application.

  • cache: boolean indicating if cache images transformation (true is the recommended value).

Here there's a sample of a swr configuration you can include in your "ComponentsConfig" section of mantraconfig.json file for your project:

"swr": {
    "filesRepositoryLocation": "./filesrepository/swr",
    "aliases": {
        "avatarextrasmall": "resize:w(64),h(64)",
        "avatarsmall": "resize:w(120),h(120)",
        "avatarmedium": "resize:w(250),h(250)",
        "avatarlarge": "resize:w(420),h(420)",
        "avatarMenu": "resize:w(42),h(42)"
    },
    "removetemponapp": "mainapp",
    "cache": true
},

With this configuration:

  • The files repository will be located at "<project folder>/filesrepository/swr" location.

  • According to these aliases, requests like:

``url http://yoursite/swr/avatarsmall/-/assets/images/useravatar.png


, will be interpretated as:

``url
http://yoursite/swr/resize:w(120),h(120)/-/assets/images/useravatar.png

, as indicated in the configuration.

Great, isn't it!

MIT License

Copyright (c) 2022 www.mantrajs.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.