|
| 1 | +--- |
| 2 | +title: Working With Images In Gatsby |
| 3 | +--- |
| 4 | + |
| 5 | +# Working With Images In Gatsby |
| 6 | + |
| 7 | +Optimizing images is a challenge on any website. To utilize best practices for performance across devices, you need multiple sizes and resolutions of each image. Luckily, Gatsby has several useful [plugins](/docs/plugins/) that work together to do that for images on [page components](/docs/building-with-components/#page-components). |
| 8 | + |
| 9 | + |
| 10 | +The recommended approach is to use [GraphQL queries](/docs/querying-with-graphql/) to get images of the optimal size or resolution, then, display them with the [`gatsby-image`](/packages/gatsby-image/) component. |
| 11 | + |
| 12 | + |
| 13 | +## Query Images With GraphQL |
| 14 | + |
| 15 | +Querying images with GraphQL allows you to access the image's data as well as perform transformations with [Sharp](https://github.com/lovell/sharp), a high-performance image processing library. |
| 16 | + |
| 17 | +You'll need a few plugins for this: |
| 18 | +* [`gatsby-source-filesystem`](/packages/gatsby-source-filesystem/) plugin allows you to [query files with GraphQL](docs/querying-with-graphql/#images) |
| 19 | +* [`gatsby-plugin-sharp`](/packages/gatsby-plugin-sharp) powers the connections between Sharp and Gatsby Plugins |
| 20 | +* [`gatsby-transformer-sharp`](/packages/gatsby-transformer-sharp/) allows you to create multiples images of the right sizes and resolutions with a query |
| 21 | + |
| 22 | +If the final image is of a fixed size, optimization relies on having multiple resolutions of the image. If it is responsive, that is it stretches to fill a container or page, optimization relies on having different sizes of the same image. See the [Gatsby Image documentation for more information](/packages/gatsby-image/#two-types-of-responsive-images). |
| 23 | + |
| 24 | +You can also use arguments in your query to specify exact, minimum, and maximum dimensions. See the [Gatsby Image documentation for complete options](/packages/gatsby-image/#two-types-of-responsive-images). |
| 25 | + |
| 26 | +This example is for an image gallery where images stretch when the page is resized. It uses the `sizes` method and the size fragment to grab the right data to use in `gatsby-image` component and arguments to set the maximum width as 400px and maximum height as 250px. |
| 27 | +```jsx |
| 28 | +export const query = graphql` |
| 29 | + query indexQuery { |
| 30 | + fileName:file(relativePath: { eq: "images/myimage.jpg" }) { |
| 31 | + childImageSharp { |
| 32 | + sizes(maxWidth: 400, maxHeight: 250) { |
| 33 | + ...GatsbyImageSharpSizes |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | +`; |
| 39 | +``` |
| 40 | + |
| 41 | +## Optimizing Images With Gatsby Image |
| 42 | + |
| 43 | +[`gatsby-image`](/packages/gatsby-image/) is a plugin that automatically creates React components for optimized images that: |
| 44 | + |
| 45 | + |
| 46 | +> * Loads the optimal size of image for each device size and screen resolution |
| 47 | +> * Holds the image position while loading so your page doesn't jump around as images load |
| 48 | +> * Uses the "blur-up" effect i.e. it loads a tiny version of the image to show while the full image is loading |
| 49 | +> * Alternatively provides a "traced placeholder" SVG of the image |
| 50 | +> * Lazy loads images, which reduces bandwidth and speeds the initial load time |
| 51 | +> * Uses [WebP](https://developers.google.com/speed/webp/) images, if browser supports the format |
| 52 | +
|
| 53 | + |
| 54 | +Here is an image component that uses the query from the previous example: |
| 55 | + |
| 56 | +``` |
| 57 | +<Img sizes={data.fileName.childImageSharp.sizes} /> |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +## Using Fragments To Standardize Formatting |
| 62 | + |
| 63 | +What if you have a bunch of images and you want them all to use the same formatting? |
| 64 | + |
| 65 | +A custom fragment is an easy way to standardize formatting and re-use it on multiple images: |
| 66 | + |
| 67 | +``` |
| 68 | +export const squareImage = graphql` |
| 69 | +fragment squareImage on File { |
| 70 | + childImageSharp { |
| 71 | + sizes(maxWidth: 200, maxHeight: 200) { |
| 72 | + ...GatsbyImageSharpSizes |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +`; |
| 77 | +``` |
| 78 | + |
| 79 | +The fragment can then be referenced in the image query: |
| 80 | + |
| 81 | +``` |
| 82 | +
|
| 83 | +export const query = graphql` |
| 84 | + query imageGallery { |
| 85 | + image1:file(relativePath: { eq: "images/image1.jpg" }) { |
| 86 | + ...squareImage |
| 87 | + } |
| 88 | +
|
| 89 | + image2:file(relativePath: { eq: "images/image2.jpg" }) { |
| 90 | + ...squareImage |
| 91 | + } |
| 92 | +
|
| 93 | + image3:file(relativePath: { eq: "images/image3.jpg" }) { |
| 94 | + ...squareImage |
| 95 | + } |
| 96 | + } |
| 97 | +`; |
| 98 | +
|
| 99 | +``` |
| 100 | + |
| 101 | + |
0 commit comments