Rust client for the Tinify API, used for TinyPNG and TinyJPG. Tinify compresses your images intelligently. Read more at https://tinify.com.
Go to the documentation for the HTTP client.
In currently development.
There are still features of TinyPNG to implement.
To look at all the features of Tinify API: Documentation.
Install the API client with Cargo. Add this to Cargo.toml
:
toml
[dependencies]
tinify-rs = "1.2.0"
About key
Get an API key from https://tinypng.com/developers
Compress from a file ```rust use tinify::Tinify; use tinify::TinifyError;
fn main() -> Result<(), TinifyError> { let key = "tinify api key"; let tinify = Tinify::new().setkey(key); let client = tinify.getclient()?; let _ = client .fromfile("./unoptimized.jpg")? .tofile("./optimized.jpg")?;
Ok(()) } ```
fn main() -> Result<(), TinifyError> { let key = "tinify api key"; let tinify = Tinify::new().setkey(key); let client = tinify.getclient()?; let _ = client .fromurl("https://tinypng.com/images/panda-happy.png")? .tofile("./optimized.png")?;
Ok(()) } ```
fn main() -> Result<(), TinifyError> { let key = "tinify api key"; let tinify = Tinify::new().setkey(key); let client = tinify.getclient()?; let bytes = fs::read("./unoptimized.jpg")?; let _ = client .frombuffer(&bytes)? .tofile("./optimized.jpg")?;
Ok(()) } ```
fn get_client() -> Result
tinify .setkey(key) .getclient() }
fn main() -> Result<(), TinifyError> { let client = getclient()?; let _ = client .fromfile("./unoptimized.jpg")? .resize(Resize::new( Method::FIT, Some(400), Some(200)), )? .to_file("./resized.jpg")?;
Ok(()) } ```
fn main() -> Result<(), TinifyError> { let _ = Tinify::new() .setkey("api key") .getclient()? .fromfile("./tmpimage.jpg")? .convert(( Some(Type::PNG), None, None, ), None, )? .to_file("./converted.png");
Ok(()) } ```
fn main() -> Result<(), TinifyError> { let _ = Tinify::new() .setkey("api key") .getclient()? .fromurl("https://tinypng.com/images/panda-happy.png")? .convert(( Some(Type::JPEG), None, None, ), Some(Color("#FF5733")), )? .tofile("./converted.jpg");
Ok(()) } ```
Create a .env file with a TiniPNG KEY
cargo test
All contributions will be welcomed. Feel free to open any issues or pull requests.