png to jpg for space
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
use crate::models::{ClipboardData, ClipboardEntry, Image};
|
use crate::models::{ClipboardData, ClipboardEntry, Image};
|
||||||
|
use image::codecs::jpeg::JpegEncoder;
|
||||||
use image::codecs::png::PngEncoder;
|
use image::codecs::png::PngEncoder;
|
||||||
use image::{ExtendedColorType, ImageEncoder};
|
use image::{ExtendedColorType, ImageEncoder};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
@ -49,12 +50,16 @@ impl Database {
|
|||||||
let img_path = img.file_path(&self.dir_path);
|
let img_path = img.file_path(&self.dir_path);
|
||||||
|
|
||||||
let file = fs::File::create(&img_path)?;
|
let file = fs::File::create(&img_path)?;
|
||||||
let encoder = PngEncoder::new(file);
|
let rgb_pixels: Vec<u8> = raw_pixels
|
||||||
|
.chunks_exact(4)
|
||||||
|
.flat_map(|rgba| [rgba[0], rgba[1], rgba[2]])
|
||||||
|
.collect();
|
||||||
|
let encoder = JpegEncoder::new_with_quality(file, 70);
|
||||||
encoder.write_image(
|
encoder.write_image(
|
||||||
raw_pixels,
|
&rgb_pixels,
|
||||||
img.width,
|
img.width,
|
||||||
img.height,
|
img.height,
|
||||||
ExtendedColorType::Rgba8,
|
ExtendedColorType::Rgb8,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
("image", img.id.to_string())
|
("image", img.id.to_string())
|
||||||
|
|||||||
@ -27,7 +27,7 @@ impl Image {
|
|||||||
pub fn file_path(&self, base_dir: &str) -> PathBuf {
|
pub fn file_path(&self, base_dir: &str) -> PathBuf {
|
||||||
std::path::Path::new(base_dir)
|
std::path::Path::new(base_dir)
|
||||||
.join("images")
|
.join("images")
|
||||||
.join(format!("{}.png", self.id))
|
.join(format!("{}.jpg", self.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_bytes(&self, dir_path: &str) -> io::Result<Vec<u8>> {
|
pub fn load_bytes(&self, dir_path: &str) -> io::Result<Vec<u8>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user