#optimization #api-client #tiny-png #api-key #tinify #tinyjpg

tinify-rs

A Rust client for the Tinify API

12 releases (stable)

1.4.2 Dec 27, 2024
1.4.1 May 1, 2024
1.4.0 Apr 10, 2024
1.3.0 Mar 11, 2024
0.2.0 Jun 21, 2021

#134 in Images

Download history 1/week @ 2024-09-18 4/week @ 2024-09-25 3/week @ 2024-10-02 2/week @ 2024-12-04 12/week @ 2024-12-11 135/week @ 2024-12-25

149 downloads per month

MIT license

135KB
1.5K SLoC

Tinify API client for Rust 🦀

CI Status Crate Version

Tinify API Client for the Rust Programming Language, used for TinyPNG and TinyJPG. Tinify compresses your images intelligently. Read more at https://tinify.com.

Documentation

Go to the documentation for the HTTP client.

Status

In currently development.

There are still features of TinyPNG to implement.

To look at all the features of Tinify API: Documentation.

Roadmap

  • Compressing images
  • Resizing images
  • Converting images
  • Preserving metadata
  • Saving to Amazon S3
  • Saving to Google Cloud Storage
  • Implement an async non-blocking Client

Getting Started

Install the API client with Cargo. Add this to Cargo.toml:

[dependencies]
tinify-rs = "1.4.1"

Using async client

[dependencies]
tinify-rs = { version = "1.4.1", features = ["async"] }

Usage

use tinify::error::TinifyError;
use tinify::sync::Tinify;
use std::path::Path;

fn main() -> Result<(), TinifyError> {
  let key = "api key";
  let output = Path::new("./optimized.jpg");
  let tinify = Tinify::new().set_key(key);
  let optimized = tinify
    .get_client()?
    .from_file("./unoptimized.jpg")?
    .to_file(output);

  if let Err(error) = optimized {
    match error {
      TinifyError::ClientError { ref upstream } => {
        println!("Error: {} message: {}", upstream.error, upstream.message);
      }
      _ => println!("{:?}", error),
    }
  }

  Ok(())
}

  • Compress from a file async
use tinify::error::TinifyError;
use tinify::async_bin::Tinify;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), TinifyError> {
  let key = "api key";
  let output = Path::new("./optimized.jpg");
  let tinify = Tinify::new().set_key(key);
  let compressed = tinify
    .get_async_client()?
    .from_file("./unoptimized.jpg").await?
    .to_file(output).await;

  if let Err(error) = compressed {
    match error {
      TinifyError::ClientError { ref upstream } => {
        println!("Error: {} message: {}", upstream.error, upstream.message);
      }
      _ => println!("{:?}", error),
    }
  }

  Ok(())
}

Running tests

Create a .env file with a TiniPNG KEY

cargo test --features async

Contribution

All contributions will be welcomed. Feel free to open any issues or pull requests.

Dependencies

~4–16MB
~224K SLoC