npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@creative-web-solution/picture-filter

v1.1.3

Published

Resize and other filters for images using Sharp. Useful to create images variations for srcset.

Downloads

35

Readme

Picture filter

This module use Sharp.

This generic bundle only works with the website socle.

Install

On installation (npm i), this bundle will copy some files if they do not already exist:

  • config/packages/pictureFilter.json
  • config/routes/1-pictureFilter.js
  • resources/fake-picture-filter-images/no-image.jpg
  • resources/fake-picture-filter-images/no-image.png
  • resources/fake-picture-filter-images/no-image.webp

Route

The file config/routes/1-pictureFilter.js is created when the package is installed.

You can change the number at the start of the filename to manage the order of the routes creation.

Configuration

Configuration in parameters.json.

Those 2 parameters are not mandatory, they are false by default.

{
    ...
    "picture_filter_allow_insecure_remote": true,
    "picture_filter_fake_in_dev": true,
    ...
}

See below for their purpose.

Configuration in config/packages/pictureFilter.json.

The file config/packages/pictureFilter.json is created when the package is installed.

Set the destination folder in (Mandatory):

It will be relative to the public folder.

{
    "pictureFilter": {
        "exportFolder": "path/to/destination/folder",
        ...
    }
}

Set default export param:

{
    "defaultExportParams": {
        "png": {
            "compressionLevel": 5, // 0 -> 9
            "progressive": false
        },
        "jpeg": {
            "quality": 70, // 0 -> 100
            "progressive": false
        },
        "webp": {
            "quality": 70, // 0 -> 100
            "alphaQuality": 100, // 0 -> 100
            "lossless": false
        }
    },
    ...
}

Set actions (Mandatory):

In the actions array you can add all the function of Sharp in the order you want.

{
    "filters": {
        "myWonderfullFilter": {
            "actions": [
                {
                    "name": "SharpFunctionName",
                    "params": {
                        "param1": "value1",
                        "param2": "value2",
                        ...
                    }
                },
                {
                    "name": "SharpFunctionName2",
                    "params": [ "param1", "param2", ... ]
                },
                ...
            ]
        },
        ...
    }
}

Scale

A scale function was added in addition of all Sharp action. It can take 2 parameters:

  • x: size factor of the width
  • y: size factor of the height
{
    "filters": {
        "halfSize": {
            "actions": [
                {
                    "name": "scale",
                    "params": {
                        "x": 0.5
                    }
                }
            ]
        }
    }
}

If you have certificate issue with the remote server, you can add picture_filter_allow_insecure_remote: true in the parameters.json. It's not recommended.

Fake pictures

The files resources/fake-picture-filter-images/no-image.* are created when the package is installed.

In production mode, some fake pictures are sent in case where the wanted picture can't be exported.

You can customize these pictures by changing the three in resources/fake-picture-filter-images. DO NOT change their name. Their size should be at least 1920px x 1080px.

You can force the display of these pictures in development mode by adding picture_filter_fake_in_dev: true in parameters.json

Example

{
    "exportFolder": "",
    // Set the default for all export format
    "defaultExportParams": {
        "png": {
            "compressionLevel": 5, // 0 -> 9
            "progressive": false
        },
        "jpeg": {
            "quality": 70, // 0 -> 100
            "progressive": false
        },
        "webp": {
            "quality": 70, // 0 -> 100
            "alphaQuality": 100, // 0 -> 100
            "lossless": false
        }
    },
    "filters": {
        "sample": {
            "actions": [
                // Resize a picture
                {
                    "name": "resize",
                    "params": {
                        "width": 320,
                        "height": 320,
                        "fit": "cover", // cover, contain, fill, inside or outside
                        "position": "centre", // centre, top, right top, right, right bottom, bottom, left bottom, left, left top
                        "background": { "r": 0, "g": 0, "b": 0, "alpha": 1 }
                    }
                },
                // Set quality for this filter only
                {
                    "name": "jpeg",
                    "params": {
                        "quality": 90,
                    }
                }
            ]
        }
    }
}

Twig extension

Use the getSrcset twig extension (core/Twig/Extension/PictureFunction.js) to create a srcset for <picture> and <img srcset="..." >:

<picture>
    <source srcset="{{ getSrcset( asset('assets/images/img-1.jpg'), { "320w": "widen_320", "640w": "widen_640", "1024w": "widen_1024" }) }}" media="(max-width: 1023px)" sizes="100vw" >
    <source srcset="{{ getSrcset( asset('assets/images/img-2.jpg'), { "1024w": "widen_1024", "1280w": "widen_1280", "1440w": "widen_1440" }) }}" media="(min-width: 1024px)" sizes="100vw" >
    <img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" alt="">
</picture>

You can use it with remote images:

<picture>
    <source srcset="{{ getSrcset( 'https://www.distant-domain.com/images/image-small.jpg', { "320w": "widen_320", "640w": "widen_640", "1024w": "widen_1024" }) }}" media="(max-width: 1023px)" sizes="100vw" >
    <source srcset="{{ getSrcset( 'https://www.distant-domain.com/images/image-big.jpg', { "1024w": "widen_1024", "1280w": "widen_1280", "1440w": "widen_1440" }) }}" media="(min-width: 1024px)" sizes="100vw" >
    <img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" alt="">
</picture>

It will download the image and save it in var/cache/picture-filter-cache.

Use the getSrc twig extension (core/Twig/Extension/PictureFunction.js) to create an url from a source and one filter:

<img src="{{ getSrc( asset('assets/images/img-1.jpg'), "widen_320" ) }}" alt="">