Skip to content

Commit

Permalink
Fix for getzola#632 - add ability to get asset image dimensions (getz…
Browse files Browse the repository at this point in the history
  • Loading branch information
stusmall authored and Keats committed May 30, 2019
1 parent ba6165f commit 12d2576
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions components/templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 36 additions & 1 deletion components/templates/src/global_fns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>>,
Expand Down
2 changes: 1 addition & 1 deletion components/templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions docs/content/documentation/content/image-processing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 8 additions & 0 deletions docs/content/documentation/templates/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 12d2576

Please sign in to comment.