This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use image::{image_dimensions, ImageReader};
|
use image::image_dimensions;
|
||||||
use indoc::formatdoc;
|
use indoc::formatdoc;
|
||||||
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
|
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
|
||||||
use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet};
|
use syntect::{highlighting::ThemeSet, html::highlighted_html_for_string, parsing::SyntaxSet};
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
use image::ImageFormat;
|
#[allow(dead_code)]
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
pub enum ExportFormat {
|
pub enum ExportFormat {
|
||||||
Jpeg,
|
Jpeg,
|
||||||
@@ -25,12 +24,4 @@ impl ExportFormat {
|
|||||||
ExportFormat::Png => "image/png",
|
ExportFormat::Png => "image/png",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn get_image_format(&self) -> ImageFormat {
|
|
||||||
match self {
|
|
||||||
ExportFormat::Jpeg => ImageFormat::Jpeg,
|
|
||||||
ExportFormat::Avif => ImageFormat::Avif,
|
|
||||||
ExportFormat::Svg => ImageFormat::Jpeg, // TODO what now?
|
|
||||||
ExportFormat::Png => ImageFormat::Png,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,14 @@
|
|||||||
use std::{fs::create_dir_all, path::Path};
|
use std::{
|
||||||
|
fs::{create_dir_all, File},
|
||||||
|
io::BufWriter,
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
use image::{imageops::FilterType, DynamicImage};
|
use image::{
|
||||||
|
codecs::{jpeg::JpegEncoder, png::PngEncoder},
|
||||||
|
imageops::FilterType,
|
||||||
|
DynamicImage,
|
||||||
|
};
|
||||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
@@ -33,8 +41,26 @@ pub fn generate_images(
|
|||||||
create_dir_all(parent_dir).unwrap();
|
create_dir_all(parent_dir).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = resized.save_with_format(&save_path, format.get_image_format());
|
let encode = {
|
||||||
match result {
|
let buffered_file_write = &mut BufWriter::new(File::create(&save_path).unwrap()); // always seekable
|
||||||
|
match format {
|
||||||
|
ExportFormat::Jpeg => {
|
||||||
|
let encoder = JpegEncoder::new_with_quality(buffered_file_write, 87);
|
||||||
|
resized.write_with_encoder(encoder)
|
||||||
|
}
|
||||||
|
ExportFormat::Avif => todo!(),
|
||||||
|
ExportFormat::Svg => todo!(),
|
||||||
|
ExportFormat::Png => {
|
||||||
|
let encoder = PngEncoder::new_with_quality(
|
||||||
|
buffered_file_write,
|
||||||
|
image::codecs::png::CompressionType::Best,
|
||||||
|
image::codecs::png::FilterType::Adaptive,
|
||||||
|
);
|
||||||
|
resized.write_with_encoder(encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match encode {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to generate {:?} - {:?}", &save_path, err);
|
error!("Failed to generate {:?} - {:?}", &save_path, err);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user