Begin resize option
This commit is contained in:
parent
f389faef03
commit
83fa2c6864
5 changed files with 467 additions and 101 deletions
64
src/main.rs
64
src/main.rs
|
@ -1,9 +1,15 @@
|
|||
#![windows_subsystem = "windows"]
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use clap::{Arg, Command};
|
||||
use image::{imageops::FilterType, io::Reader as ImageReader};
|
||||
use log::info;
|
||||
use rfd::FileDialog;
|
||||
use native_dialog::{FileDialog, MessageDialog};
|
||||
use rexiv2::Metadata;
|
||||
use rust_embed::RustEmbed;
|
||||
use simplelog::{ColorChoice, ConfigBuilder, LevelFilter, TermLogger, TerminalMode};
|
||||
use std::{path::PathBuf, thread::JoinHandle};
|
||||
use std::{fs::File, io::BufWriter, path::PathBuf, thread::JoinHandle};
|
||||
use tempfile::tempdir;
|
||||
use warp::Filter;
|
||||
use wry::{
|
||||
application::{
|
||||
|
@ -30,7 +36,17 @@ fn main() -> Result<()> {
|
|||
ColorChoice::Auto,
|
||||
)?;
|
||||
|
||||
let img_path = if let Some(img_path) = std::env::args_os().skip(1).next() {
|
||||
let tmp_dir = tempdir()?;
|
||||
|
||||
let cmd = Command::new("Simple panorama viewer")
|
||||
.arg(
|
||||
Arg::new("filename")
|
||||
.allow_invalid_utf8(true)
|
||||
.help("Image path"),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let img_path = if let Some(img_path) = cmd.value_of_os("filename") {
|
||||
PathBuf::from(img_path)
|
||||
} else {
|
||||
let user_dirs = directories::UserDirs::new().unwrap();
|
||||
|
@ -46,13 +62,49 @@ fn main() -> Result<()> {
|
|||
"jpg", "JPG", "jpeg", "pjpeg", "pjpg", "PJPG", "webp", "avif",
|
||||
],
|
||||
)
|
||||
.set_directory(dir)
|
||||
.pick_file()
|
||||
.set_location(dir)
|
||||
.show_open_single_file()?
|
||||
.ok_or_else(|| anyhow!("No file"))?
|
||||
};
|
||||
|
||||
info!("Open `{}`", img_path.display());
|
||||
let img_data = std::fs::read(img_path)?;
|
||||
let (width, height) = image::image_dimensions(&img_path)?;
|
||||
const MAX_IMG_SIZE: u32 = 20_000_000;
|
||||
let img_data_path = if height * width > MAX_IMG_SIZE {
|
||||
// @todo Option pas de resize / taille en ligne de commande
|
||||
// @todo Option pas de resize / taille dans une config
|
||||
// @todo Icon
|
||||
let ratio = (height * width) as f64 / MAX_IMG_SIZE as f64;
|
||||
let new_height = (height as f64 / ratio.sqrt()) as u32;
|
||||
let new_width = (width as f64 / ratio.sqrt()) as u32;
|
||||
|
||||
if MessageDialog::new()
|
||||
.set_title("Resize")
|
||||
.set_text(&format!("Resize the file to {} × {}", new_width, new_height))
|
||||
.show_confirm()?
|
||||
{
|
||||
// Resize image
|
||||
let img = ImageReader::open(&img_path)?.decode()?;
|
||||
info!("Resize image to {} × {}", new_width, new_height);
|
||||
let image_resized = img.resize(new_width, new_height, FilterType::Triangle);
|
||||
|
||||
// Save file and add metadata
|
||||
let tmp_img_path = tmp_dir.path().join("img.jpg");
|
||||
image_resized.write_to(
|
||||
&mut BufWriter::new(File::create(&tmp_img_path)?),
|
||||
image::ImageOutputFormat::Jpeg(95),
|
||||
)?;
|
||||
let metadata = Metadata::new_from_path(&img_path)?;
|
||||
metadata.save_to_file(&tmp_img_path)?;
|
||||
tmp_img_path
|
||||
} else {
|
||||
img_path
|
||||
}
|
||||
} else {
|
||||
img_path
|
||||
};
|
||||
let img_data = std::fs::read(&img_data_path)?;
|
||||
tmp_dir.close()?;
|
||||
let img_base_64 = base64::encode(img_data);
|
||||
|
||||
info!("Generate HTML");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue