copy og tag images
This commit is contained in:
parent
8c72e7b440
commit
4f09373df3
1
justfile
1
justfile
@ -52,6 +52,7 @@ clean:
|
||||
# SSG
|
||||
ssg:
|
||||
- wget --no-convert-links -r -p -E -P dist --no-host-directories 127.0.0.1:{{port}}
|
||||
find generated_images/ -name "*_og*" -exec cp --parents {} dist/ \;
|
||||
|
||||
# Preview server
|
||||
preview:
|
||||
|
63
src/picture_generator/image_src_generator.rs
Normal file
63
src/picture_generator/image_src_generator.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use std::{path::Path, sync::Arc};
|
||||
|
||||
use anyhow::Context;
|
||||
use image::ImageReader;
|
||||
use tracing::debug;
|
||||
|
||||
use super::{
|
||||
image_generator::generate_images,
|
||||
picture_markup_generator::{get_export_formats, get_generated_file_name, get_image_path},
|
||||
};
|
||||
|
||||
pub fn generate_image_with_src(
|
||||
orig_img_path: &str,
|
||||
width: u32,
|
||||
height: u32,
|
||||
suffix: &str,
|
||||
generate_image: bool,
|
||||
) -> Result<String, anyhow::Error> {
|
||||
let path_to_generated = get_generated_file_name(orig_img_path);
|
||||
let file_stem = path_to_generated.file_stem().unwrap().to_str().unwrap();
|
||||
let path_to_generated = path_to_generated.with_file_name(format!("{file_stem}{suffix}"));
|
||||
|
||||
let disk_img_path =
|
||||
Path::new("static/").join(orig_img_path.strip_prefix("/").unwrap_or(orig_img_path));
|
||||
let resolutions = [(width, height, 1.)];
|
||||
|
||||
let exported_formats = get_export_formats(orig_img_path);
|
||||
let exported_format = *exported_formats.first().unwrap();
|
||||
|
||||
let path_to_generated_arc = Arc::new(path_to_generated);
|
||||
let path_to_generated_clone = Arc::clone(&path_to_generated_arc);
|
||||
|
||||
if generate_image {
|
||||
rayon::spawn(move || {
|
||||
let orig_img = ImageReader::open(&disk_img_path)
|
||||
.with_context(|| format!("Failed to read instrs from {:?}", &disk_img_path))
|
||||
.unwrap()
|
||||
.decode()
|
||||
.unwrap();
|
||||
let path_to_generated = path_to_generated_clone.as_ref();
|
||||
|
||||
let result = generate_images(
|
||||
&orig_img,
|
||||
path_to_generated,
|
||||
&resolutions,
|
||||
&[exported_format],
|
||||
)
|
||||
.with_context(|| "Failed to generate images".to_string());
|
||||
if let Err(e) = result {
|
||||
tracing::error!("Error: {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
let path_to_generated = Arc::clone(&path_to_generated_arc);
|
||||
|
||||
let image_path = get_image_path(
|
||||
&path_to_generated,
|
||||
resolutions.first().expect("Should this error ever happen?"),
|
||||
&exported_format,
|
||||
);
|
||||
|
||||
Ok(image_path)
|
||||
}
|
@ -23,5 +23,6 @@ It should be used from the templates as well
|
||||
|
||||
pub mod export_format;
|
||||
pub mod image_generator;
|
||||
pub mod image_src_generator;
|
||||
pub mod picture_markup_generator;
|
||||
pub mod resolutions;
|
||||
|
@ -119,7 +119,7 @@ pub fn generate_picture_markup(
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn get_image_path(path: &Path, resolution: &(u32, u32, f32), format: &ExportFormat) -> String {
|
||||
pub fn get_image_path(path: &Path, resolution: &(u32, u32, f32), format: &ExportFormat) -> String {
|
||||
let path_name = path.to_str().expect("Image has to have a valid path");
|
||||
let (width, height, _) = resolution;
|
||||
let extension = format.get_extension();
|
||||
@ -197,7 +197,7 @@ fn strip_prefixes(path: &Path) -> &Path {
|
||||
parent_path
|
||||
}
|
||||
|
||||
fn get_generated_file_name(orig_img_path: &str) -> PathBuf {
|
||||
pub fn get_generated_file_name(orig_img_path: &str) -> PathBuf {
|
||||
let path = Path::new(&orig_img_path);
|
||||
// let parent = path
|
||||
// .parent()
|
||||
@ -247,7 +247,7 @@ fn generate_srcset(path: &Path, format: &ExportFormat, resolutions: &[(u32, u32,
|
||||
.join(", ")
|
||||
}
|
||||
|
||||
fn get_export_formats(orig_img_path: &str) -> Vec<ExportFormat> {
|
||||
pub fn get_export_formats(orig_img_path: &str) -> Vec<ExportFormat> {
|
||||
let path = Path::new(&orig_img_path)
|
||||
.extension()
|
||||
.and_then(|ext| ext.to_str());
|
||||
|
@ -6,7 +6,8 @@
|
||||
<meta property="og:url" content="https://michalvanko.dev/blog/{{slug}}" />
|
||||
{% match thumbnail %}
|
||||
{% when Some with (img) %}
|
||||
<meta property="og:image" content="https://michalvanko.dev{{img}}" />
|
||||
{% let src = crate::picture_generator::image_src_generator::generate_image_with_src(img, 1200, 630, "_og", true).unwrap_or("thumbnail not found".to_string())|safe %}
|
||||
<meta property="og:image" content="https://michalvanko.dev{{src}}" />
|
||||
{% when None %}
|
||||
<meta property="og:image" content="https://michalvanko.dev/images/m-logo.svg" />
|
||||
{% endmatch %}
|
||||
|
Loading…
Reference in New Issue
Block a user