pub struct SiImage { /* private fields */ }Expand description
Represents an image with text rendering capabilities.
Implementations§
source§impl SiImage
impl SiImage
sourcepub fn new(src: Vec<u8>) -> Self
pub fn new(src: Vec<u8>) -> Self
Creates a new SiImage from a vector of image data and a SiFont.
Arguments
src- The vector of image data.
sourcepub async fn from_network_async(image_url: &str) -> SiImage
pub async fn from_network_async(image_url: &str) -> 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.
sourcepub fn from_network(image_url: &str) -> SiImage
pub fn from_network(image_url: &str) -> 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.
Examples found in repository?
examples/main.rs (line 34)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}sourcepub fn render_text(
self,
text: &str,
text_scale: f32,
pos_x: f32,
pos_y: f32,
color: Option<String>,
using_font: &SiFont
) -> SiImage
pub fn render_text( self, text: &str, text_scale: f32, pos_x: f32, pos_y: f32, color: Option<String>, using_font: &SiFont ) -> 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”).using_font- The SiFont used for text rendering on the image.
Returns
A mutable instance of the main image, with the text rendered on it.
Examples found in repository?
examples/main.rs (lines 24-31)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}sourcepub fn to_bytes(&self) -> Vec<u8>
pub fn to_bytes(&self) -> Vec<u8>
Examples found in repository?
examples/main.rs (line 58)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}sourcepub fn height(&self) -> u32
pub fn height(&self) -> u32
Examples found in repository?
examples/main.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}sourcepub fn width(&self) -> u32
pub fn width(&self) -> u32
Examples found in repository?
examples/main.rs (line 9)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}source§impl SiImage
impl SiImage
sourcepub fn load_preset(
&mut self,
preset: Box<SiPreset>,
values: HashMap<String, Box<dyn Any>>
) -> &mut SiImage
pub fn load_preset( &mut self, preset: Box<SiPreset>, values: HashMap<String, Box<dyn Any>> ) -> &mut SiImage
Load a preset. NOTE: It doesn’t work in WASM. Only for direct usage.
Examples found in repository?
examples/main.rs (line 56)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
fn main() {
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
);
let preset = SiPreset::new(Box::new(|img: &mut SiImage, values: std::collections::HashMap<String, Box<dyn std::any::Any>>| {
println!("Dimensions: {}, {}", img.width(), img.height());
let font = match values.get("font") {
Some(font) => {
if font.type_id() == TypeId::of::<SiFont>() {
font.downcast_ref::<SiFont>().unwrap().clone()
} else {
SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
}
}
None => SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/sirin-stencil.ttf",
)
};
let new_img = img.clone().render_text(
"Hello from Preset!",
32.0,
480.00,
480.00,
Some("#00ffff".to_string()),
&font,
);
new_img
}));
let img = SiImage::from_network("https://res.cloudinary.com/zype/image/upload/regraphic");
let mut binding = img.render_text(
"Hello, World!",
64.0,
480.00,
254.00,
Some("#00ffff".to_string()),
&font.clone(),
)
.render_text(
"Hello, Tagline!",
48.0,
480.0,
320.0,
None,
&font.clone(),
);
let font = SiFont::from_network(
"https://github.com/Zype-Z/ShareImage.js/raw/main/assets/fonts/arial.ttf",
);
let _font: Box<dyn std::any::Any> = Box::new(font.clone());
let img = binding.load_preset(preset, std::collections::HashMap::from([("font".to_string(), _font)]));
let mut file = File::create("output.png").expect("Could not create bytes");
let _ = image::load_from_memory(&img.clone().to_bytes())
.expect("Could not load image")
.write_to(&mut file, image::ImageFormat::Png);
println!("Created!")
}Trait Implementations§
source§impl FromWasmAbi for SiImage
impl FromWasmAbi for SiImage
source§impl IntoWasmAbi for SiImage
impl IntoWasmAbi for SiImage
source§impl LongRefFromWasmAbi for SiImage
impl LongRefFromWasmAbi for SiImage
source§impl OptionFromWasmAbi for SiImage
impl OptionFromWasmAbi for SiImage
source§impl OptionIntoWasmAbi for SiImage
impl OptionIntoWasmAbi for SiImage
source§impl RefFromWasmAbi for SiImage
impl RefFromWasmAbi for SiImage
source§impl RefMutFromWasmAbi for SiImage
impl RefMutFromWasmAbi for SiImage
source§impl VectorFromWasmAbi for SiImage
impl VectorFromWasmAbi for SiImage
source§impl VectorIntoWasmAbi for SiImage
impl VectorIntoWasmAbi for SiImage
Auto Trait Implementations§
impl RefUnwindSafe for SiImage
impl Send for SiImage
impl Sync for SiImage
impl Unpin for SiImage
impl UnwindSafe for SiImage
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
Mutably borrows from an owned value. Read more
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
Same as
IntoWasmAbi::Abisource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
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.