Resize and center text

This commit is contained in:
torbjornmolin
2025-09-28 18:32:38 +02:00
parent c2736613a9
commit eb4a06b669
2 changed files with 21 additions and 7 deletions

View File

@@ -24,7 +24,7 @@
width: 100%; width: 100%;
max-width: 600px; max-width: 600px;
background: white; background: white;
border-radius: 16px; border-radius: 8px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
padding: 35px; padding: 35px;
margin: 40px 20px; margin: 40px 20px;
@@ -73,7 +73,10 @@
border-color: #764ba2; border-color: #764ba2;
outline: none; outline: none;
} }
input:invalid textarea:invalid {
border-color: #760000;
outline: none;
}
textarea { textarea {
resize: vertical; resize: vertical;
min-height: 100px; min-height: 100px;

View File

@@ -74,28 +74,39 @@ fn get_files(options: GenerateOptions) -> Vec<(String, Vec<u8>)> {
let mut files: Vec<(String, Vec<u8>)> = Vec::new(); let mut files: Vec<(String, Vec<u8>)> = Vec::new();
use text_to_png::TextRenderer; use text_to_png::TextRenderer;
let renderer = TextRenderer::default(); let renderer = TextRenderer::default();
let image_file_ending = "png";
for f in options.filenames { for f in options.filenames {
let mut imgbuf = image::ImageBuffer::new(options.width, options.height); let mut imgbuf = image::ImageBuffer::new(options.width, options.height);
for (x, y, pixel) in imgbuf.enumerate_pixels_mut() { for (x, y, pixel) in imgbuf.enumerate_pixels_mut() {
*pixel = image::Rgba([255, 0, 2, 255]); *pixel = image::Rgba([190, 190, 190, 255]);
} }
let textpngdata = renderer let textpngdata = renderer
.render_text_to_png_data(&f, 64, "Dark Turquoise") .render_text_to_png_data(format!("{}.{}", &f, image_file_ending), 100, "White")
.unwrap(); .unwrap();
let other = image::ImageReader::new(Cursor::new(textpngdata.data)) let pngtextsource = image::ImageReader::new(Cursor::new(textpngdata.data))
.with_guessed_format() .with_guessed_format()
.unwrap() .unwrap()
.decode() .decode()
.unwrap(); .unwrap();
imgbuf.copy_from(&other, 0, 0).unwrap(); let resized_text = pngtextsource.resize(
options.width / 2,
options.height,
image::imageops::FilterType::Nearest,
);
let xpos = (imgbuf.width() - resized_text.width()) / 2;
let ypos = (imgbuf.height() - resized_text.height()) / 2;
imgbuf.copy_from(&resized_text, xpos, ypos).unwrap();
let mut buf = Cursor::new(Vec::new()); let mut buf = Cursor::new(Vec::new());
imgbuf.write_to(&mut buf, image::ImageFormat::Png).unwrap(); imgbuf.write_to(&mut buf, image::ImageFormat::Png).unwrap();
files.push((String::from(format!("{}.png", f)), buf.into_inner())); files.push((
String::from(format!("{}.{}", f, image_file_ending)),
buf.into_inner(),
));
} }
files files