Struct si_img::SiImage

source ·
pub struct SiImage { /* private fields */ }
Expand description

Represents an image with text rendering capabilities.

Implementations§

source§

impl SiImage

source

pub fn new(src: Vec<u8>, font: SiFont) -> Self

Creates a new SiImage from a vector of image data and a SiFont.

Arguments
  • src - The vector of image data.
  • font - The SiFont used for text rendering on the image.
source

pub fn from_vec(vec: Vec<u8>, font: SiFont) -> SiImage

Creates a new SiImage from a vector of image data.

source

pub async fn from_network_async(image_url: &str, font: SiFont) -> SiImage

Creates a new SiImage from image data fetched from a network URL asynchronously.

Arguments
  • image_url - The URL from which to fetch the image data.
  • font - The SiFont used for text rendering on the image.
source

pub fn from_network(image_url: &str, font: SiFont) -> SiImage

Creates a new SiImage from image data fetched from a network URL synchronously.

Arguments
  • image_url - The URL from which to fetch the image data.
  • font - The SiFont used for text rendering on the image.
Examples found in repository?
examples/main.rs (line 6)
4
5
6
7
8
9
10
11
fn main() {
    let font = SiFont::from_network("https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf");
    let mut img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/w_1200,h_650/CodeWithR/Template.png", font.clone());
    let img = img.text("Hello, World!", 64.0, 480.00, 254.00, None).set_font(font).text("Finally, it freaking works!", 48.0, 480.00, 320.0, Option::Some("#00ffff".to_string()));
    let mut file = File::create("output.png").expect("Could not create bytes");
    let _ = image::load_from_memory(&img.to_bytes()).expect("Could not load image").write_to(&mut file, image::ImageFormat::Png);
    println!("Created!")
}
source

pub fn text( &mut self, text: &str, text_scale: f32, pos_x: f32, pos_y: f32, color: Option<String> ) -> SiImage

Renders text onto the image.

Arguments
  • text - The text to render on the image.
  • text_scale - The scale of the rendered text.
  • pos_x - The X-coordinate position for rendering.
  • pos_y - The Y-coordinate position for rendering.
  • color - The color of the rendered text in hexadecimal format (e.g., “#RRGGBB”).
Examples found in repository?
examples/main.rs (line 7)
4
5
6
7
8
9
10
11
fn main() {
    let font = SiFont::from_network("https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf");
    let mut img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/w_1200,h_650/CodeWithR/Template.png", font.clone());
    let img = img.text("Hello, World!", 64.0, 480.00, 254.00, None).set_font(font).text("Finally, it freaking works!", 48.0, 480.00, 320.0, Option::Some("#00ffff".to_string()));
    let mut file = File::create("output.png").expect("Could not create bytes");
    let _ = image::load_from_memory(&img.to_bytes()).expect("Could not load image").write_to(&mut file, image::ImageFormat::Png);
    println!("Created!")
}
source

pub fn to_bytes(&self) -> Vec<u8>

Gets the image data as bytes in PNG format.

Examples found in repository?
examples/main.rs (line 9)
4
5
6
7
8
9
10
11
fn main() {
    let font = SiFont::from_network("https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf");
    let mut img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/w_1200,h_650/CodeWithR/Template.png", font.clone());
    let img = img.text("Hello, World!", 64.0, 480.00, 254.00, None).set_font(font).text("Finally, it freaking works!", 48.0, 480.00, 320.0, Option::Some("#00ffff".to_string()));
    let mut file = File::create("output.png").expect("Could not create bytes");
    let _ = image::load_from_memory(&img.to_bytes()).expect("Could not load image").write_to(&mut file, image::ImageFormat::Png);
    println!("Created!")
}
source

pub fn set_font(&mut self, font: SiFont) -> SiImage

Sets the font for text rendering on the image.

Arguments
  • font - The SiFont to set as the font.
Examples found in repository?
examples/main.rs (line 7)
4
5
6
7
8
9
10
11
fn main() {
    let font = SiFont::from_network("https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf");
    let mut img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/w_1200,h_650/CodeWithR/Template.png", font.clone());
    let img = img.text("Hello, World!", 64.0, 480.00, 254.00, None).set_font(font).text("Finally, it freaking works!", 48.0, 480.00, 320.0, Option::Some("#00ffff".to_string()));
    let mut file = File::create("output.png").expect("Could not create bytes");
    let _ = image::load_from_memory(&img.to_bytes()).expect("Could not load image").write_to(&mut file, image::ImageFormat::Png);
    println!("Created!")
}
source

pub fn height(&self) -> u32

Gets the height of the image.

source

pub fn width(&self) -> u32

Gets the width of the image.

Trait Implementations§

source§

impl Clone for SiImage

source§

fn clone(&self) -> SiImage

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl From<SiImage> for JsValue

source§

fn from(value: SiImage) -> Self

Converts to this type from the input type.
source§

impl FromWasmAbi for SiImage

§

type Abi = u32

The wasm ABI type that this converts from when coming back out from the ABI boundary.
source§

unsafe fn from_abi(js: u32) -> Self

Recover a Self from Self::Abi. Read more
source§

impl IntoWasmAbi for SiImage

§

type Abi = u32

The wasm ABI type that this converts into when crossing the ABI boundary.
source§

fn into_abi(self) -> u32

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
source§

impl LongRefFromWasmAbi for SiImage

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = Ref<'static, SiImage>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl OptionFromWasmAbi for SiImage

source§

fn is_none(abi: &Self::Abi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
source§

impl OptionIntoWasmAbi for SiImage

source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
source§

impl RefFromWasmAbi for SiImage

§

type Abi = u32

The wasm ABI type references to Self are recovered from.
§

type Anchor = Ref<'static, SiImage>

The type that holds the reference to Self for the duration of the invocation of the function that has an &Self parameter. This is required to ensure that the lifetimes don’t persist beyond one function call, and so that they remain anonymous.
source§

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

Recover a Self::Anchor from Self::Abi. Read more
source§

impl RefMutFromWasmAbi for SiImage

§

type Abi = u32

Same as RefFromWasmAbi::Abi
§

type Anchor = RefMut<'static, SiImage>

Same as RefFromWasmAbi::Anchor
source§

unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor

Same as RefFromWasmAbi::ref_from_abi
source§

impl WasmDescribe for SiImage

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ReturnWasmAbi for Twhere T: IntoWasmAbi,

§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more