Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@imgix/gatsby-transform-url
Advanced tools
A function to transform imgix urls into data for gatsby-image
@imgix/gatsby-transform-url
is a library to transform imgix urls to a format compatible with gatsby-image.
This library allows imgix urls to be used with gatsby-image. This library transforms imgix urls into a format that is compatible with gatsby-image. This can generate either fluid or fixed images. With this library you can either display images that already exist on imgix, or proxy other images through imgix.
Unfortunately, due to limitations of Gatsby, this library does not support the placeholder/blur-up feature yet. When Gatsby removes this limitation, we plan to implement this for this library. In the meantime, our other Gatsby plugins will support blur-up/placeholder images, so if this feature is critical to you, please consider using one of those.
Firstly, this library requires an imgix account, so please follow this quick start guide if you don't have an account.
Then, install this library with the following commands, depending on your package manager.
npm install @imgix/gatsby-transform-url
yarn add @imgix/gatsby-transform-url
The following code will render a fluid image with gatsby-image. This code should already be familiar to you if you've used gatsby-image in the past.
import Img from 'gatsby-image';
import { buildFluidImageData } from '@imgix/gatsby-transform-url';
// Later, in a gatsby page/component.
<Img
fluid={buildFluidImageData(
'https://assets.imgix.net/examples/pione.jpg',
{
// imgix parameters
ar: 1.61, // required
auto: ['format', 'compress'], // recommended for all images
// pass other imgix parameters here, as needed
},
{
sizes: '50vw', // optional, but highly recommended
},
)}
/>;
ar
is required, since gatsby-image requires this to generate placeholders. This ar
will also crop the rendered photo from imgix to the same aspect ratio. If you don't know the aspect ratio of your image beforehand, it is virtually impossible to use gatsby-image in this format, so we either recommend using another of our plugins, or using an img
directly.
Setting auto: ['format', 'compress']
is highly recommended. This will re-format the image to the format that is best optimized for your browser, such as WebP. It will also reduce unnecessary wasted file size, such as transparency on a non-transparent image. More information about the auto parameter can be found here.
A full list of imgix parameters can be found here.
Although sizes
is optional, it is highly recommended. It has a default of 100vw
, which means that it might be loading an image too large for your users. Some examples of what you can set sizes as are:
500px
- the image is a fixed width. In this case, you should use fixed mode, described in the next section.(min-width: 1140px) 1140px, 100vw
- under 1140px, the image is as wide as the viewport. Above 1140px, it is fixed to 1140px.A full example of a fluid image in a working Gatsby repo can be found on CodeSandbox.
The following code will render a fixed image with gatsby-image. This code should already be familiar to you if you've used gatsby-image in the past.
import Img from 'gatsby-image';
import { buildFixedImageData } from '@imgix/gatsby-transform-url';
// Later, in a gatsby page/component.
<Img
fluid={buildFixedImageData('https://assets.imgix.net/examples/pione.jpg', {
// imgix parameters
w: 960, // required
h: 540, // required
auto: ['format', 'compress'], // recommended for all images
// pass other imgix parameters here, as needed
})}
/>;
The imgix parameters w
and h
are required, since these are used by gatsby-image to display a placeholder while the image is loading. Other imgix parameters can be added below the width and height.
Setting auto: ['format', 'compress']
is highly recommended. This will re-format the image to the format that is best optimized for your browser, such as WebP. It will also reduce unnecessary wasted file size, such as transparency on a non-transparent image. More information about the auto parameter can be found here.
A full list of imgix parameters can be found here.
An example of this mode in a full working Gatsby repo can be found on CodeSandbox.
buildFixedImageData
function buildFixedImageData(
/**
* An imgix url to transform, e.g. https://yourdomain.imgix.net/your-image.jpg
*/
url: string,
/**
* A set of imgix parameters to apply to the image.
* Parameters ending in 64 will be base64 encoded.
* A full list of imgix parameters can be found here: https://docs.imgix.com/apis/url
* Width (w) and height (h) are required.
*/
imgixParams: { w: number; h: number } & IImgixParams,
/**
* Options that are not imgix parameters.
* Optional.
*/
options?: {
/**
* Disable the ixlib diagnostic param that is added to every url.
*/
includeLibraryParam?: boolean;
},
): {
width: number;
height: number;
src: string;
srcSet: string;
srcWebp: string;
srcSetWebp: string;
};
buildFluidImageData
export function buildFluidImageData(
/**
* An imgix url to transform, e.g. https://yourdomain.imgix.net/your-image.jpg
*/
url: string,
/**
* A set of imgix parameters to apply to the image.
* Parameters ending in 64 will be base64 encoded.
* A full list of imgix parameters can be found here: https://docs.imgix.com/apis/url
* The aspect ratio (ar) as a float is required.
*/
imgixParams: {
/**
* The aspect ratio to set for the rendered image and the placeholder.
* Format: float or string. For float, it can be calculated with ar = width/height. For a string, it should be in the format w:h.
*/
ar: number | string;
} & IImgixParams,
/**
* Options that are not imgix parameters.
* Optional.
*/
options?: {
/**
* Disable the ixlib diagnostic param that is added to every url.
*/
includeLibraryParam?: boolean;
/**
* The sizes attribute to set on the underlying image.
*/
sizes?: string;
},
): {
aspectRatio: number;
src: string;
srcSet: string;
srcWebp: string;
srcSetWebp: string;
sizes: string;
};
ixlib
Param on Every Request?For security and diagnostic purposes, we tag all requests with the language and version of library used to generate the URL.
To disable this, set includeLibraryParam
in the third parameter to false
when calling one of the two functions this library exports. For example, for buildFluidImageData
:
<Img
fluid={buildFixedImageData(
'https://assets.imgix.net/examples/pione.jpg',
{
w: 960,
h: 540,
},
{
includeLibraryParam: false, // this disables the ixlib parameter
},
)}
/>
FAQs
A function to transform imgix urls into data for gatsby-image
We found that @imgix/gatsby-transform-url demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.