copy og tag images

This commit is contained in:
Michal Vanko 2024-10-02 10:24:24 +02:00
parent 8c72e7b440
commit 4f09373df3
5 changed files with 70 additions and 4 deletions

View File

@ -52,6 +52,7 @@ clean:
# SSG # SSG
ssg: ssg:
- wget --no-convert-links -r -p -E -P dist --no-host-directories 127.0.0.1:{{port}} - 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 server
preview: preview:

View 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)
}

View File

@ -23,5 +23,6 @@ It should be used from the templates as well
pub mod export_format; pub mod export_format;
pub mod image_generator; pub mod image_generator;
pub mod image_src_generator;
pub mod picture_markup_generator; pub mod picture_markup_generator;
pub mod resolutions; pub mod resolutions;

View File

@ -119,7 +119,7 @@ pub fn generate_picture_markup(
Ok(result) 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 path_name = path.to_str().expect("Image has to have a valid path");
let (width, height, _) = resolution; let (width, height, _) = resolution;
let extension = format.get_extension(); let extension = format.get_extension();
@ -197,7 +197,7 @@ fn strip_prefixes(path: &Path) -> &Path {
parent_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 path = Path::new(&orig_img_path);
// let parent = path // let parent = path
// .parent() // .parent()
@ -247,7 +247,7 @@ fn generate_srcset(path: &Path, format: &ExportFormat, resolutions: &[(u32, u32,
.join(", ") .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) let path = Path::new(&orig_img_path)
.extension() .extension()
.and_then(|ext| ext.to_str()); .and_then(|ext| ext.to_str());

View File

@ -6,7 +6,8 @@
<meta property="og:url" content="https://michalvanko.dev/blog/{{slug}}" /> <meta property="og:url" content="https://michalvanko.dev/blog/{{slug}}" />
{% match thumbnail %} {% match thumbnail %}
{% when Some with (img) %} {% 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 %} {% when None %}
<meta property="og:image" content="https://michalvanko.dev/images/m-logo.svg" /> <meta property="og:image" content="https://michalvanko.dev/images/m-logo.svg" />
{% endmatch %} {% endmatch %}