From 12d2576fbaaf4a5def9f7bca23502bc119a85370 Mon Sep 17 00:00:00 2001 From: Stuart Small Date: Thu, 30 May 2019 13:06:24 -0500 Subject: [PATCH] Fix for #632 - add ability to get asset image dimensions (#670) --- Cargo.lock | 1 + components/site/src/lib.rs | 4 ++ components/templates/Cargo.toml | 1 + components/templates/src/global_fns/mod.rs | 37 ++++++++++++++++++- components/templates/src/lib.rs | 2 +- .../content/image-processing/index.md | 6 +++ .../documentation/templates/overview.md | 8 ++++ 7 files changed, 57 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9555009..3816f38a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2364,6 +2364,7 @@ dependencies = [ "config 0.1.0", "csv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "errors 0.1.0", + "image 0.21.1 (registry+https://github.com/rust-lang/crates.io-index)", "imageproc 0.1.0", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "library 0.1.0", diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index a8ffcc9b..6be1fcdd 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -395,6 +395,10 @@ impl Site { "resize_image", global_fns::ResizeImage::new(self.imageproc.clone()), ); + self.tera.register_function( + "get_image_metadata", + global_fns::GetImageMeta::new(self.content_path.clone()), + ); self.tera.register_function("load_data", global_fns::LoadData::new(self.base_path.clone())); self.tera.register_function("trans", global_fns::Trans::new(self.config.clone())); self.tera.register_function( diff --git a/components/templates/Cargo.toml b/components/templates/Cargo.toml index 4f95aaca..28c0da18 100644 --- a/components/templates/Cargo.toml +++ b/components/templates/Cargo.toml @@ -10,6 +10,7 @@ lazy_static = "1" pulldown-cmark = "0.2" toml = "0.4" csv = "1" +image = "0.21" serde_json = "1.0" reqwest = "0.9" url = "1.5" diff --git a/components/templates/src/global_fns/mod.rs b/components/templates/src/global_fns/mod.rs index 16a8a1eb..f979a2e7 100644 --- a/components/templates/src/global_fns/mod.rs +++ b/components/templates/src/global_fns/mod.rs @@ -2,11 +2,13 @@ use std::collections::HashMap; use std::path::PathBuf; use std::sync::{Arc, Mutex, RwLock}; -use tera::{from_value, to_value, Function as TeraFn, Result, Value}; +use tera::{from_value, to_value, Error, Function as TeraFn, Result, Value}; use config::Config; use library::{Library, Taxonomy}; use utils::site::resolve_internal_link; +use image; +use image::GenericImageView; use imageproc; @@ -140,6 +142,39 @@ impl TeraFn for ResizeImage { } } +#[derive(Debug)] +pub struct GetImageMeta { + content_path: PathBuf +} + +impl GetImageMeta { + pub fn new(content_path: PathBuf) -> Self { + Self { content_path } + } +} + +impl TeraFn for GetImageMeta { + fn call(&self, args: &HashMap) -> Result { + let path = required_arg!( + String, + args.get("path"), + "`get_image_meta` requires a `path` argument with a string value" + ); + let src_path = self.content_path.join(&path); + if !src_path.exists(){ + return Err(format!("`get_image_meta`: Cannot find path: {}", path).into()); + } + let img = image::open(&src_path) + .map_err(|e| Error::chain(format!("Failed to process image: {}", path), e))?; + let mut map = tera::Map::new(); + map.insert(String::from("height"), Value::Number(tera::Number::from(img.height()))); + map.insert(String::from("width"), Value::Number(tera::Number::from(img.width()))); + Ok(Value::Object(map)) + } +} + + + #[derive(Debug)] pub struct GetTaxonomyUrl { taxonomies: HashMap>, diff --git a/components/templates/src/lib.rs b/components/templates/src/lib.rs index a1b61139..c51b452d 100644 --- a/components/templates/src/lib.rs +++ b/components/templates/src/lib.rs @@ -7,7 +7,7 @@ extern crate csv; extern crate pulldown_cmark; extern crate reqwest; extern crate url; - +extern crate image; #[cfg(test)] #[macro_use] extern crate serde_json; diff --git a/docs/content/documentation/content/image-processing/index.md b/docs/content/documentation/content/image-processing/index.md index 697cbe02..32c95d6b 100644 --- a/docs/content/documentation/content/image-processing/index.md +++ b/docs/content/documentation/content/image-processing/index.md @@ -145,3 +145,9 @@ Here is the result: Image attribution: Public domain, except: _06-example.jpg_: Willi Heidelbach, _07-example.jpg_: Daniel Ullrich. + + +## Get image size + +Sometimes when building a gallery it is useful to know the dimensions of each asset. You can get this information with +[get_image_metadata](./documentation/templates/overview.md#get-image-metadata) \ No newline at end of file diff --git a/docs/content/documentation/templates/overview.md b/docs/content/documentation/templates/overview.md index bd0df029..de2a3e9b 100644 --- a/docs/content/documentation/templates/overview.md +++ b/docs/content/documentation/templates/overview.md @@ -130,6 +130,14 @@ In the case of non-internal links, you can also add a cachebust of the format `? by passing `cachebust=true` to the `get_url` function. +### `get_image_metadata` +Gets metadata for an image. Today the only supported keys are `width` and `height`. + +```jinja2 + {% set meta = get_image_metadata(path="...") %} + Our image is {{ meta.width }}x{{ meta.height }} +``` + ### `get_taxonomy_url` Gets the permalink for the taxonomy item found.