图片
Parcel 内置了对调整图片大小、转换和优化图片的支持。可以从 HTML、CSS、JavaScript 或任何其他文件类型引用图片。
¥Parcel has built in support for resizing, converting, and optimizing images. Images can be referenced from HTML, CSS, JavaScript, or any other file type.
调整图片大小和转换图片
#¥Resizing and converting images
Parcel 包含一个开箱即用的图片转换器,它允许你调整图片大小、将其转换为不同的格式或调整质量以减小文件大小。这可以在引用图片时使用查询参数或使用 configuration 文件来完成。
¥Parcel includes an image transformer out of the box, which allows you to resize images, convert them to a different format, or adjust the quality to reduce file size. This can be done using query parameters when referencing the image, or using a configuration file.
图片转换器依赖于 锋利的 图片转换库,该库将在需要时作为开发依赖自动安装到你的项目中。
¥The image transformer relies on the Sharp image transformation library, which will be automatically installed as a dev dependency into your project when needed.
你可以使用的查询参数是:
¥The query parameters you can use are:
-
width
- 将图片大小调整为的宽度¥
width
– The width to resize the image to -
height
- 调整图片大小的高度¥
height
– The height to resize the image to -
quality
- 你想要的图片质量百分比,例如?quality=75
¥
quality
– The image quality percentage you want, for example?quality=75
-
as
- 将图片转换为的文件格式,例如:?as=webp
¥
as
– File format to convert the image to, for example:?as=webp
图片格式
#¥Image formats
支持以下图片格式,通过 as
查询参数作为输入和输出:
¥The following image formats are supported, both as input and as output via the as
query parameter:
-
jpeg
/jpg
- JPEG 是一种得到广泛支持的有损图片格式。它通常用于照片,并提供相当好的压缩,但不支持透明度或无损压缩。¥
jpeg
/jpg
- JPEG is a very widely supported lossy image format. It's often used for photos, and offers reasonably good compression, but does not support transparency or lossless compression. -
png
- 可移植网络图形 (PNG) 是一种无损图片格式。PNG 通常比 JPEG 或其他有损图片格式大得多,但支持透明度并提供更高的精细细节质量。¥
png
- Portable Network Graphics (PNG) is a lossless image format. PNGs are typically much larger than JPEGs or other lossy image formats, but support transparency and offer much higher quality for fine details. -
webp
- WebP 支持有损和无损压缩以及动画和透明度。它在所有现代浏览器中都是 supported,并且在与 JPEG 和 PNG 相同的质量下提供更好的压缩。¥
webp
– WebP supports both lossy and lossless compression as well as animation and transparency. It's supported in all modern browsers, and offers better compression for the same quality as JPEGs and PNGs. -
avif
- AVIF 是一种基于 AV1 视频编解码器的新型有损图片格式,与 JPEG 和 WebP 相比,它提供了显着的压缩和质量改进。目前最新版本的 Chrome 和 Firefox 中的版本为 supported。¥
avif
– AVIF is a new lossy image format based on the AV1 video codec which offers significant compression and quality improvements over JPEG and WebP. It's currently supported in the latest versions of Chrome and Firefox.
还支持以下格式作为输入,但浏览器通常不支持:tiff
、heic
/heif
和 raw
。
¥The following formats are also supported as inputs, but are not generally supported by browsers: tiff
, heic
/ heif
, and raw
.
如果你 设置自定义 libvips 构建,也支持 GIF,但是,由于文件较大,不鼓励使用 GIF。使用视频格式代替。
¥GIFs are also supported if you setup a custom libvips build, however, using GIFs is discouraged due to their large file sizes. Use a video format instead.
有关选择正确图片格式的更多指导,请参阅 web.dev 上的指南。
¥For more guidance on choosing the right image formats, see the guide on web.dev.
JavaScript
#要从 JavaScript 引用图片,请使用 URL
构造函数。有关更多详细信息,请参阅 JavaScript 文档中的 URL 依赖。
¥To reference an image from JavaScript, use the URL
constructor. For more details, see URL dependencies in the JavaScript docs.
HTML
#要引用 HTML 中的图片,请使用 <img>
或 <picture>
元素。可以使用不同的查询参数多次引用同一图片,以创建不同格式或大小的多个版本。有关详细信息,请参阅 HTML 文档。
¥To reference an image from HTML, use the <img>
or <picture>
element. The same image can be referenced multiple times with different query parameters to create multiple versions in different formats or sizes. See the HTML docs for more details.
配置
#¥Configuration
除了查询参数之外,Parcel 还支持使用配置文件来定义适用于项目中所有图片的选项。例如,你可以按特定质量设置重新编码所有图片以减小文件大小,或者为每种输出格式定义更高级的选项。
¥In addition to query parameters, Parcel also supports using a configuration file to define options that apply to all of the images in your project. For example, you could re-encode all images at a certain quality setting to reduce file size, or define more advanced options for each output format.
要设置项目中所有图片的质量,请在项目中创建 sharp.config.json
文件并定义 quality
字段。这将重新编码每个图片,而不仅仅是查询参数引用的图片。
¥To set the quality across all images in your project, create a sharp.config.json
file in your project and define the quality
field. This will re-encode every image, not just ones referenced with query params.
你还可以为每种格式定义更多高级选项。所有采用 sharp.config.json
中定义的选项格式的图片都将被重新编码。请参阅支持选项 此处 的完整列表。
¥You can also define more advanced options per format. All images in formats with options defined in sharp.config.json
will be re-encoded. See the full list of supported options here.
图片优化
#¥Image optimization
Parcel 还默认在生产模式下对 JPEG 和 PNG 进行无损图片优化,从而在不影响图片质量的情况下减小图片的大小。这不需要任何查询参数或配置即可使用。但是,由于优化是无损的,因此与使用 quality
查询参数或使用现代格式(例如 WebP 或 AVIF)相比,可能减少的大小可能会更少。
¥Parcel also includes lossless image optimization for JPEGs and PNGs by default in production mode, which reduces the size of images without affecting their quality. This does not require any query parameters or configuration to use. However, since the optimization is lossless, the size reduction possible may be less than if you use the quality
query param, or use a modern format such as WebP or AVIF.
禁用图片优化
#¥Disabling image optimization
要在生产模式下禁用 JPEG 和 PNG 的默认图片优化,请将以下内容添加到 .parcelrc 配置文件中:
¥To disable the default image optimization for JPEGs and PNGs in production mode, add the following to your .parcelrc configuration file: