Browse Source

Fix for #632 - add ability to get asset image dimensions (#670)

index-subcmd
Stuart Small Vincent Prouillet 5 years ago
parent
commit
12d2576fba
7 changed files with 57 additions and 2 deletions
  1. +1
    -0
      Cargo.lock
  2. +4
    -0
      components/site/src/lib.rs
  3. +1
    -0
      components/templates/Cargo.toml
  4. +36
    -1
      components/templates/src/global_fns/mod.rs
  5. +1
    -1
      components/templates/src/lib.rs
  6. +6
    -0
      docs/content/documentation/content/image-processing/index.md
  7. +8
    -0
      docs/content/documentation/templates/overview.md

+ 1
- 0
Cargo.lock View File

@@ -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",


+ 4
- 0
components/site/src/lib.rs View File

@@ -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(


+ 1
- 0
components/templates/Cargo.toml View File

@@ -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"


+ 36
- 1
components/templates/src/global_fns/mod.rs View File

@@ -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<String, Value>) -> Result<Value> {
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<String, HashMap<String, String>>,


+ 1
- 1
components/templates/src/lib.rs View File

@@ -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;


+ 6
- 0
docs/content/documentation/content/image-processing/index.md View File

@@ -145,3 +145,9 @@ Here is the result:
<small>
Image attribution: Public domain, except: _06-example.jpg_: Willi Heidelbach, _07-example.jpg_: Daniel Ullrich.
</small>


## 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)

+ 8
- 0
docs/content/documentation/templates/overview.md View File

@@ -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.



Loading…
Cancel
Save