Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AVIF support for resize_image() function #2780

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/imageproc/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum Format {
Png,
/// WebP, The `u8` argument is WebP quality (in percent), None meaning lossless.
WebP(Option<u8>),
/// AVIF, The `u8` argument is AVIF quality (in percent), None meaning lossless.
Avif(Option<u8>),
}

impl Format {
Expand All @@ -32,6 +34,7 @@ impl Format {
"jpeg" | "jpg" => Ok(Jpeg(jpg_quality)),
"png" => Ok(Png),
"webp" => Ok(WebP(quality)),
"avif" => Ok(Avif(quality)),
_ => Err(anyhow!("Invalid image format: {}", format)),
}
}
Expand All @@ -44,6 +47,7 @@ impl Format {
Png => "png",
Jpeg(_) => "jpg",
WebP(_) => "webp",
Avif(_) => "avif",
}
}
}
Expand All @@ -58,6 +62,8 @@ impl Hash for Format {
Jpeg(q) => 1001 + q as u16,
WebP(None) => 2000,
WebP(Some(q)) => 2001 + q as u16,
Avif(None) => 3000,
Avif(Some(q)) => 3001 + q as u16,
};

hasher.write_u16(q);
Expand Down
16 changes: 16 additions & 0 deletions components/imageproc/src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use errors::{anyhow, Context, Result};
use libs::avif_parse::read_avif;
use libs::image::ImageReader;
use libs::image::{ImageFormat, ImageResult};
use libs::svg_metadata::Metadata as SvgMetadata;
use serde::Serialize;
use std::ffi::OsStr;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

/// Size and format read cheaply with `image`'s `Reader`.
Expand Down Expand Up @@ -44,6 +47,10 @@ impl ImageMetaResponse {
pub fn new_svg(width: u32, height: u32) -> Self {
Self { width, height, format: Some("svg"), mime: Some("text/svg+xml") }
}

pub fn new_avif(width: u32, height: u32) -> Self {
Self { width, height, format: Some("avif"), mime: Some("image/avif") }
}
}

impl From<ImageMeta> for ImageMetaResponse {
Expand Down Expand Up @@ -75,6 +82,15 @@ pub fn read_image_metadata<P: AsRef<Path>>(path: P) -> Result<ImageMetaResponse>
// this is not a typo, this returns the correct values for width and height.
.map(|(h, w)| ImageMetaResponse::new_svg(w as u32, h as u32))
}
"avif" => {
let avif_data =
read_avif(&mut BufReader::new(File::open(path)?)).with_context(err_context)?;
let meta = avif_data.primary_item_metadata()?;
return Ok(ImageMetaResponse::new_avif(
meta.max_frame_width.get(),
meta.max_frame_height.get(),
));
}
_ => ImageMeta::read(path).map(ImageMetaResponse::from).with_context(err_context),
}
}
19 changes: 18 additions & 1 deletion components/imageproc/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use std::path::{Path, PathBuf};
use config::Config;
use errors::{anyhow, Context, Result};
use libs::ahash::{HashMap, HashSet};
use libs::image::codecs::avif::AvifEncoder;
use libs::image::codecs::jpeg::JpegEncoder;
use libs::image::imageops::FilterType;
use libs::image::{EncodableLayout, ImageFormat};
use libs::image::GenericImageView;
use libs::image::{EncodableLayout, ExtendedColorType, ImageEncoder, ImageFormat};
use libs::rayon::prelude::*;
use libs::{image, webp};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -71,6 +73,21 @@ impl ImageOp {
};
buffered_f.write_all(memory.as_bytes())?;
}
Format::Avif(q) => {
let mut avif: Vec<u8> = Vec::new();
let color_type = match img.color().has_alpha() {
true => ExtendedColorType::Rgba8,
false => ExtendedColorType::Rgb8,
};
let encoder = AvifEncoder::new_with_speed_quality(&mut avif, 10, q.unwrap_or(100));
encoder.write_image(
&img.as_bytes(),
img.dimensions().0,
img.dimensions().1,
color_type,
)?;
buffered_f.write_all(&avif.as_bytes())?;
}
}

Ok(())
Expand Down
18 changes: 18 additions & 0 deletions components/imageproc/tests/resize_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ fn resize_image_png_webp() {
image_op_test("png.png", "scale", Some(150), Some(150), "webp", "webp", 150, 150, 300, 380);
}

#[test]
fn resize_image_png_avif() {
image_op_test("png.png", "scale", Some(150), Some(150), "avif", "avif", 150, 150, 300, 380);
}

#[test]
fn resize_image_webp_jpg() {
image_op_test("webp.webp", "scale", Some(150), Some(150), "auto", "jpg", 150, 150, 300, 380);
Expand Down Expand Up @@ -179,6 +184,19 @@ fn read_image_metadata_webp() {
);
}

#[test]
fn read_image_metadata_avif() {
assert_eq!(
image_meta_test("avif.avif"),
ImageMetaResponse {
width: 300,
height: 380,
format: Some("avif"),
mime: Some("image/avif")
}
);
}

#[test]
fn fix_orientation_test() {
fn load_img_and_fix_orientation(img_name: &str) -> DynamicImage {
Expand Down
Binary file added components/imageproc/tests/test_imgs/avif.avif
Binary file not shown.
4 changes: 2 additions & 2 deletions components/libs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ filetime = "0.2"
gh-emoji = "1"
glob = "0.3"
globset = "0.4"
image = "0.25"
image = {version = "0.25", default-features = true, features = ["avif"]}
lexical-sort = "0.3"
minify-html = "0.15"
nom-bibtex = "0.5"
Expand Down Expand Up @@ -44,7 +44,7 @@ unicode-segmentation = "1.2"
url = "2"
walkdir = "2"
webp = "0.3"

avif-parse = "1.3.2"

[features]
# TODO: fix me, it doesn't pick up the reqwuest feature if not set as default
Expand Down
1 change: 1 addition & 0 deletions components/libs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
pub use ahash;
pub use ammonia;
pub use atty;
pub use avif_parse;
pub use base64;
pub use csv;
pub use elasticlunr;
Expand Down
3 changes: 2 additions & 1 deletion docs/content/documentation/content/image-processing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ resize_image(path, width, height, op, format, quality)
- `"jpg"`
- `"png"`
- `"webp"`
- `"avif"`

The default is `"auto"`, this means that the format is chosen based on input image format.
JPEG is chosen for JPEGs and other lossy formats, and PNG is chosen for PNGs and other lossless formats.
- `quality` (_optional_): JPEG or WebP quality of the resized image, in percent. Only used when encoding JPEGs or WebPs; for JPEG default value is `75`, for WebP default is lossless.
- `quality` (_optional_): Quality of the resized image, in percent. Only used when encoding JPEGs, WebPs or AVIFs; for JPEG and AVIF default value is `75`, for WebP and AVIF default is lossless.

### Image processing and return value

Expand Down
1 change: 1 addition & 0 deletions zola-theme-jiaxiang.wang
Submodule zola-theme-jiaxiang.wang added at d069cb