
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
gatsby-transformer-sharp
Advanced tools
Gatsby transformer plugin for images using Sharp
The gatsby-transformer-sharp package is a Gatsby plugin that allows you to transform images using the Sharp image processing library. It provides a variety of image processing functionalities such as resizing, cropping, and creating responsive images.
Resize Images
This feature allows you to resize images to specified dimensions. The code sample demonstrates how to resize images to 200x200 pixels using the resize method.
exports.createPages = ({ graphql }) => {
return graphql(`
{
allFile {
edges {
node {
childImageSharp {
resize(width: 200, height: 200) {
src
}
}
}
}
}
}
`).then(result => {
console.log(result.data.allFile.edges);
});
};
Generate Thumbnails
This feature allows you to generate fixed-size thumbnails. The code sample demonstrates how to create thumbnails of 125x125 pixels using the fixed method.
exports.createPages = ({ graphql }) => {
return graphql(`
{
allFile {
edges {
node {
childImageSharp {
fixed(width: 125, height: 125) {
src
}
}
}
}
}
}
`).then(result => {
console.log(result.data.allFile.edges);
});
};
Create Responsive Images
This feature allows you to create responsive images that adapt to different screen sizes. The code sample demonstrates how to create fluid images with a maximum width of 800 pixels using the fluid method.
exports.createPages = ({ graphql }) => {
return graphql(`
{
allFile {
edges {
node {
childImageSharp {
fluid(maxWidth: 800) {
...GatsbyImageSharpFluid
}
}
}
}
}
}
`).then(result => {
console.log(result.data.allFile.edges);
});
};
Sharp is a high-performance image processing library for Node.js. It provides a wide range of image manipulation functionalities such as resizing, cropping, and format conversion. Unlike gatsby-transformer-sharp, which is specifically designed for Gatsby, Sharp can be used in any Node.js application.
Imagemin is an image optimization tool that compresses images to reduce their file size. It supports various image formats and can be used to optimize images during the build process. While it focuses on optimization rather than transformation, it can be used alongside gatsby-transformer-sharp for a complete image processing solution.
Jimp is an image processing library for Node.js that provides functionalities such as resizing, cropping, and adding filters. It is similar to Sharp but is written entirely in JavaScript, making it easier to use in environments where native modules are not supported.
Creates ImageSharp
nodes from image types that are supported by the
Sharp image processing library and provides
fields in their GraphQL types for processing your images in a variety of ways
including resizing, cropping, and creating responsive images.
npm install --save gatsby-transformer-sharp
// In your gatsby-config.js
plugins: [
`gatsby-transformer-sharp`,
]
It recongnizes files with the following extensions as images.
Each image file is parsed into a node of type ImageSharp
.
FAQs
Gatsby transformer plugin for images using Sharp
The npm package gatsby-transformer-sharp receives a total of 138,513 weekly downloads. As such, gatsby-transformer-sharp popularity was classified as popular.
We found that gatsby-transformer-sharp demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.