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

@vtex/gatsby-plugin-thumbor

v1.5.21-avon.0

Published

A plugin for integrating gatsby-plugin-image with [thumbor](http://thumbor.org/), an open-source smart on-demand image cropping, resizing and filters, so you can make your own smart image handling service.

Downloads

1,618

Readme

gatsby-plugin-thumbor

A plugin for integrating gatsby-plugin-image with thumbor, an open-source smart on-demand image cropping, resizing and filters, so you can make your own smart image handling service.

Install

yarn add @vtex/gatsby-plugin-thumbor gatsby-plugin-image

How to use

For using this plugin, you will need to add both gatsby-plugin-image and gatsby-plugin-thumbor to your gatsby-config.js.

// In your gatsby-config.js
module.exports = {
  plugins: [
    // other plugins ...
    {
      resolve: 'gatsby-plugin-image'
    },
    {
      resolve: `@vtex/gatsby-plugin-thumbor`
      options: {
        // Your thumbor server url
        server: 'https://mythumborserver.com',
      }
    }
  ],
}

Then, in your react component you can simply use the hooks exported by gatsby-plugin-thumbor with the components exported by gatsby-plugin-image

// In your React component
import React, { FC } from 'react'
import { useThumborImageData } from '@vtex/gatsby-plugin-thumbor'
import { GatsbyImage } from 'gatsby-plugin-image'

const MyComponent: FC = () => {
  const image = useThumborImageData({
    baseUrl: 'http://example.org/image.jpg',
    options: {
      // ...other thumbor options
      fitIn: true,
    }
  })

  return <GatsbyImage alt="example image" image={image} />
}

How it works

Thumbor is an image processing service that enables you to process any image on the web. According to gatsby-plugin-image documentation, thumbor is a URL-based image optimization service. This means that, for processing an image, we only need to generate an URL with the right parameters, and thumbor will do the heavy lifting for us.

A thumbor compatible URL can be split into two sections. The first one is a thumbor specific URL with parameters for image resizing, postprocessing, etc, and a second section containing a data source where thumbor must fetch the original image from. To illustrate. Let's suppose your thumbor service is located at mythumborserver.com domain and that you need to resize to 250px/250px an image located at example.com/image.png. To resize image.png you can do the following request:

GET https://mythumborserver.com/unsafe/250x250/http://example.com/image.png

The aforementioned request returns the processed image. gatsby-plugin-image uses this URL in HTML's img tag elements to add images to the final HTML.

As mentioned before, Thumbor has lots of different parameters that tell it how to process and convert images. gatsby-plugin-thumbor tightly integrates with gatsby-plugin-image to give you control over these parameters in a simple interface, while generating a thumbor compatible URLs. The interface chosen is the extra options parameters in gatsby-plugin-image functions.

Securing your thumbor service

You may now be wondering why there is an /unsafe segment in thumbor's generated URL. This is because thumbor accepts requests from anyone on the internet to process and resize images. This makes the thumbor service prone to DDoS attacks.

Thumbor has a solution for this on their docs. They say you should sign the URLs so the thumbor service knows when an image is from a reliable source or if it's someone messing with your server. This is the best way for protecting your thumbor server.

However, this plugin offers an alternative to this solution. Usually, when serving gatsby, you are using a CDN (gatsby-plugin-netlify) or Nginx (@vtex/gatsby-plugin-nginx). These services can be used as a reverse proxy server. Adding a reverse proxy to your thumbor service may protect it from malicious users because then we can only allow a small subset of all possible transformations to your images, decreasing the attack space on your thumbor service. This protection is achieved with two parameters accepted by the plugin, sizes and basePath. A config using these parameters looks like this:

// In your gatsby-config.js
module.exports = {
  plugins: [
    // other plugins ...
    {
      resolve: 'gatsby-plugin-image'
    },
    {
      resolve: `@vtex/gatsby-plugin-thumbor`
      options: {
        // Your thumbor server url
        server: 'https://mythumborserver.com',
        basePath: '/images',
        sizes: [
          '1080x1080',
          '720x720',
        ]
      }
    }
  ],
}

Now, instead of creating URLs like https://mythumborserver.com/unsafe/1080x1080/http://example.com/image.png, the plugin will create URLs for {basePath}/1080x1080/http://example.com/image.png, serve thumbor images on the same domain as your site, and will only allow resizing to 1080x1080 and 720x720 images.

Behind the scenes, what this plugin is doing is calling a createRedirect function between /{basePath}/{size} => https://mythumborserver.com/unsafe/{size}

Examples

Aside from useThumborImageData, this plugin exports useUrlBuilder, useGetThumborImageData, getThumborImageData and urlBuilder. These functions are thin wrappers to their counterparts on gatsby-plugin-image only adding the plugin's options, like the thumbor server URL. You can use them normally in your react code. For instance, if you need the getImageData function you can:

// In your React component
import React, { FC } from 'react'
import { useGetThumborImageData } from '@vtex/gatsby-plugin-thumbor'
import { GatsbyImage, withArtDirection } from 'gatsby-plugin-image'

const MyComponent: FC = () => {
  // gatsby-plugin-image's getImageData function
  const getImageData = useGetThumborImageData()
  const img1 = getImageData({baseUrl: 'http://example.org/img1.jpg'})
  const img2 = getImageData({baseUrl: 'http://example.org/img2.jpg'})

  // The images are totally compatible with gatsby-plugin-image
  const image = withArtDirection(img1, {
    media: '(max-width: 40em)',
    image: img2
  })

  return <GatsbyImage alt="example image" image={image} />
}

How to contribute

Feel free to open issues in our repo. Also, there is a general contributing guideline in there