pub struct Variant { /* private fields */ }Expand description
A resized version of an image, made by Cloudflare Image Transformations (Active Storage’s variants).
Variant::path builds /cdn-cgi/image/<options>/<source>: Cloudflare’s
edge fetches the source image (for example the app’s own route that
serves it), resizes it and caches the result.
Variants are lazy (made on the first request, like Rails’
variant(...).processed on first view) and never touch the Worker’s
CPU or R2’s storage. format=auto is always set: browsers that accept
AVIF or WebP get it, others get the original format.
Needs a custom domain (a zone on Cloudflare) with Transformations turned
on in the dashboard (Images > Transformations); *.workers.dev hosts
cannot use it. Declare variants as constants, like Rules.
§Examples
use ocre::storage::{Fit, Variant};
const THUMB: Variant = Variant::new().width(300).height(300).fit(Fit::Cover).quality(80);
const LARGE: Variant = Variant::new().width(1600);
assert_eq!(
THUMB.path("/photos/7/image"),
"/cdn-cgi/image/width=300,height=300,fit=cover,quality=80,format=auto/photos/7/image"
);
assert_eq!(LARGE.path("https://files.example.com/a.jpg"), "/cdn-cgi/image/width=1600,format=auto/https://files.example.com/a.jpg");Implementations§
Source§impl Variant
impl Variant
Sourcepub const fn new() -> Self
pub const fn new() -> Self
A variant with no resizing: only format=auto.
§Examples
assert_eq!(ocre::storage::Variant::new().path("/logo.png"), "/cdn-cgi/image/format=auto/logo.png");Sourcepub const fn width(self, pixels: u32) -> Self
pub const fn width(self, pixels: u32) -> Self
Sets the largest width, in pixels.
§Examples
let path = ocre::storage::Variant::new().width(640).path("/a.png");
assert_eq!(path, "/cdn-cgi/image/width=640,format=auto/a.png");Sourcepub const fn height(self, pixels: u32) -> Self
pub const fn height(self, pixels: u32) -> Self
Sets the largest height, in pixels.
§Examples
let path = ocre::storage::Variant::new().height(480).path("/a.png");
assert_eq!(path, "/cdn-cgi/image/height=480,format=auto/a.png");Sourcepub const fn fit(self, fit: Fit) -> Self
pub const fn fit(self, fit: Fit) -> Self
Sets how the image fits the width × height box; without it, Cloudflare scales the image down to fit.
§Examples
use ocre::storage::{Fit, Variant};
let path = Variant::new().width(100).height(100).fit(Fit::Pad).path("/a.png");
assert_eq!(path, "/cdn-cgi/image/width=100,height=100,fit=pad,format=auto/a.png");Sourcepub const fn quality(self, quality: u8) -> Self
pub const fn quality(self, quality: u8) -> Self
Sets the JPEG/WebP/AVIF quality, 1 to 100 (values outside are clamped).
§Examples
let path = ocre::storage::Variant::new().quality(200).path("/a.jpg");
assert_eq!(path, "/cdn-cgi/image/quality=100,format=auto/a.jpg");Sourcepub fn path(&self, source: &str) -> String
pub fn path(&self, source: &str) -> String
The same-origin path of this variant of source: /cdn-cgi/image/<options>/<source>.
source is a path on the same zone (/photos/1/image, the leading
/ is dropped as Cloudflare expects) or an absolute https:// URL
(allowed in the zone’s Transformations settings). Pure; use it in
templates as src="{{ THUMB.path(photo_path) }}".
§Examples
let path = ocre::storage::Variant::new().width(64).path("//avatars/1");
assert_eq!(path, "/cdn-cgi/image/width=64,format=auto/avatars/1");Trait Implementations§
impl Copy for Variant
impl Eq for Variant
impl StructuralPartialEq for Variant
Auto Trait Implementations§
impl Freeze for Variant
impl RefUnwindSafe for Variant
impl Send for Variant
impl Sync for Variant
impl Unpin for Variant
impl UnsafeUnpin for Variant
impl UnwindSafe for Variant
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.