Skip to main content

Variant

Struct Variant 

Source
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

Source

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");
Source

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");
Source

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");
Source

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");
Source

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");
Source

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§

Source§

impl Clone for Variant

Source§

fn clone(&self) -> Variant

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Variant

Source§

impl Debug for Variant

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Variant

Source§

fn default() -> Variant

Returns the “default value” for a type. Read more
Source§

impl Eq for Variant

Source§

impl PartialEq for Variant

Source§

fn eq(&self, other: &Variant) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Variant

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V