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 to resize_image() #1347

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion components/imageproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
lazy_static = "1"
regex = "1.0"
tera = "1"
image = "0.23"
image = { version = "0.23.13", features = ["avif"] }
rayon = "1"

errors = { path = "../errors" }
Expand Down
17 changes: 16 additions & 1 deletion components/imageproc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fs::{self, File};
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};

use image::imageops::FilterType;
use image::{avif::AvifEncoder, imageops::FilterType, ColorType};
use image::{GenericImageView, ImageOutputFormat};
use lazy_static::lazy_static;
use rayon::prelude::*;
Expand Down Expand Up @@ -140,6 +140,8 @@ pub enum Format {
Jpeg(u8),
/// PNG
Png,
/// AVIF
Avif(u8),
}

impl Format {
Expand All @@ -156,6 +158,7 @@ impl Format {
},
"jpeg" | "jpg" => Ok(Jpeg(quality)),
"png" => Ok(Png),
"avif" => Ok(Avif(quality)),
_ => Err(format!("Invalid image format: {}", format).into()),
}
}
Expand All @@ -182,6 +185,7 @@ impl Format {
match *self {
Png => "png",
Jpeg(_) => "jpg",
Avif(_) => "avif",
}
}
}
Expand All @@ -194,6 +198,7 @@ impl Hash for Format {
let q = match *self {
Png => 0,
Jpeg(q) => q,
Avif(q) => 101 + q,
};

hasher.write_u8(q);
Expand Down Expand Up @@ -303,6 +308,16 @@ impl ImageOp {
Format::Jpeg(q) => {
img.write_to(&mut f, ImageOutputFormat::Jpeg(q))?;
}
Format::Avif(q) => {
let mut avif: Vec<u8> = Vec::new();
AvifEncoder::new_with_speed_quality(&mut avif, 1, q).write_image(
&img.as_bytes(),
img.dimensions().0,
img.dimensions().1,
ColorType::Rgb8,
)?;
std::io::Write::write_all(&mut f, &avif)?;
}
}

Ok(())
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 @@ -28,10 +28,11 @@ resize_image(path, width, height, op, format, quality)
- `"auto"`
- `"jpg"`
- `"png"`
- `"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 quality of the resized image, in percent. Only used when encoding JPEGs; default value is `75`.
- `quality` (_optional_): Quality of the resized image, in percent. Only used when encoding JPEGs or AVIFs; default value is `75`.

### Image processing and return value

Expand Down