| Crates.io | web-image-meta |
| lib.rs | web-image-meta |
| version | 0.2.1 |
| created_at | 2025-08-03 01:42:39.837349+00 |
| updated_at | 2025-08-14 11:32:20.535768+00 |
| description | A lightweight Rust library for manipulating JPEG and PNG metadata, optimized for web images |
| homepage | https://github.com/ideamans/rust-web-image-meta |
| repository | https://github.com/ideamans/rust-web-image-meta |
| max_upload_size | |
| id | 1779280 |
| size | 160,200 |
A lightweight Rust library for manipulating JPEG and PNG metadata, optimized for web images.
JPEG Support
PNG Support
Add this to your Cargo.toml:
[dependencies]
web-image-meta = "0.2.0"
use web_image_meta::jpeg;
// Clean JPEG metadata while preserving orientation
let input_data = std::fs::read("input.jpg")?;
let cleaned_data = jpeg::clean_metadata(&input_data)?;
std::fs::write("cleaned.jpg", cleaned_data)?;
// Read JPEG comment
let comment = jpeg::read_comment(&input_data)?;
if let Some(text) = comment {
println!("Comment: {}", text);
}
// Write JPEG comment
let data_with_comment = jpeg::write_comment(&input_data, "Copyright 2024")?;
std::fs::write("commented.jpg", data_with_comment)?;
// Estimate size increase before adding comment
let comment = "This is my comment";
let size_increase = jpeg::estimate_text_comment(comment);
println!("Adding comment will increase file by {} bytes", size_increase);
use web_image_meta::png;
// Remove non-critical chunks from PNG
let input_data = std::fs::read("input.png")?;
let cleaned_data = png::clean_chunks(&input_data)?;
std::fs::write("cleaned.png", cleaned_data)?;
// Read PNG text chunks (supports tEXt, zTXt, iTXt)
let chunks = png::read_text_chunks(&input_data)?;
for chunk in chunks {
println!("{}: {}", chunk.keyword, chunk.text);
}
// zTXt (compressed) and iTXt (international) chunks are automatically handled
// Add text chunk to PNG
let data_with_text = png::add_text_chunk(
&input_data,
"Copyright",
"© 2024 Example Corp"
)?;
std::fs::write("tagged.png", data_with_text)?;
// Estimate size increase before adding text chunk
let keyword = "Author";
let text = "John Doe";
let size_increase = png::estimate_text_chunk(keyword, text);
println!("Adding text chunk will increase file by {} bytes", size_increase);
clean_metadata(data: &[u8]) -> Result<Vec<u8>, Error>Removes all metadata except essential information for web display.
read_comment(data: &[u8]) -> Result<Option<String>, Error>Reads the COM (comment) segment from a JPEG file.
Some(String) if a comment exists, None otherwisewrite_comment(data: &[u8], comment: &str) -> Result<Vec<u8>, Error>Writes or replaces a comment in a JPEG file.
estimate_text_comment(comment: &str) -> usizeEstimates the exact file size increase when adding a comment to a JPEG file.
clean_chunks(data: &[u8]) -> Result<Vec<u8>, Error>Removes all non-critical chunks from a PNG file.
read_text_chunks(data: &[u8]) -> Result<Vec<TextChunk>, Error>Reads all text chunks from a PNG file.
TextChunk structsadd_text_chunk(data: &[u8], keyword: &str, text: &str) -> Result<Vec<u8>, Error>Adds a new tEXt chunk to a PNG file.
estimate_text_chunk(keyword: &str, text: &str) -> usizeEstimates the exact file size increase when adding a text chunk to a PNG file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextChunk {
pub keyword: String, // 1-79 character Latin keyword
pub text: String, // Text content
}
#[derive(Debug)]
pub enum Error {
InvalidFormat(String), // Invalid image format
Io(std::io::Error), // I/O error
ParseError(String), // Parsing error
}
The library provides detailed error types:
InvalidFormat: The input is not a valid JPEG/PNG fileParseError: The file structure is corrupted or invalidIo: System I/O errorsAll functions validate their outputs to ensure the resulting images can be decoded.
This library is designed for web image optimization:
The library validates all inputs and outputs:
The library includes comprehensive tests:
This project is licensed under the MIT License - see the LICENSE-MIT file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
This library uses the following excellent crates: