tinify 0.1.0

A high-performance Rust client for the Tinify API, providing image compression and optimization capabilities
Documentation
1
2
3
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# Tinify


[![Crates.io](https://img.shields.io/crates/v/tinify.svg)](https://crates.io/crates/tinify)
[![Documentation](https://docs.rs/tinify/badge.svg)](https://docs.rs/tinify)
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
[![Build Status](https://github.com/raynoryim/tinify/workflows/CI/badge.svg)](https://github.com/raynoryim/tinify/actions)

**English** | [δΈ­ζ–‡]README_CN.md

A high-performance Rust library for image compression and optimization, built on the [TinyPNG API]https://tinypng.com/developers. Provides async support, intelligent retry mechanisms, rate limiting, and cloud storage integration.

## ✨ Features


- πŸ–ΌοΈ **Smart Compression**: Lossless quality PNG/JPEG/WebP/AVIF image compression
- πŸ“ **Image Resizing**: Multiple resize methods (scale/fit/cover/thumb)
- πŸ”„ **Format Conversion**: Convert between popular image formats
- πŸ“Š **Metadata Preservation**: Optionally preserve copyright, creation time, location data
- ☁️ **Cloud Storage**: Direct upload to AWS S3, Google Cloud Storage
- πŸš€ **High-Performance Async**: Built on tokio for concurrent processing
- πŸ›‘οΈ **Type Safety**: Full Rust type system and comprehensive error handling
- ⚑ **Smart Retry**: Built-in exponential backoff retry logic and rate limiting
- πŸ“¦ **Zero Config**: Works out of the box with minimal setup

## πŸ“¦ Installation


Add to your `Cargo.toml`:

```toml
[dependencies]
tinify = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
```

## πŸš€ Quick Start


### Basic Usage


```rust
use tinify::Tinify;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize client
    let client = Tinify::new("your-api-key".to_string())?;

    // Compress image
    let source = client.source_from_file("input.png").await?;
    source.to_file("output.png").await?;

    println!("Image compression completed!");
    Ok(())
}
```

### Advanced Configuration


```rust
use tinify::Tinify;
use std::time::Duration;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Use builder pattern for advanced configuration
    let client = Tinify::builder()
        .api_key("your-api-key")
        .app_identifier("MyApp/1.0")
        .timeout(Duration::from_secs(30))
        .max_retry_attempts(3)
        .requests_per_minute(100)
        .build()?;

    let source = client.source_from_file("input.png").await?;
    source.to_file("output.png").await?;

    Ok(())
}
```

## πŸ“– Detailed Examples


### Image Resizing


```rust
use tinify::{Tinify, ResizeOptions, ResizeMethod};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.png").await?;

    // Configure resize options
    let resize_options = ResizeOptions {
        method: ResizeMethod::Fit,
        width: Some(300),
        height: Some(200),
    };

    // Resize image
    let mut result = source.resize(resize_options).await?;
    result.to_file("resized.png").await?;

    // Get image information
    if let Some(width) = result.image_width() {
        println!("Resized width: {} pixels", width);
    }

    Ok(())
}
```

### Format Conversion


```rust
use tinify::{Tinify, ConvertOptions, ImageFormat};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.png").await?;

    // Convert to WebP format
    let convert_options = ConvertOptions {
        format: ImageFormat::WebP,
        background: Some("#FFFFFF".to_string()),
    };

    let mut result = source.convert(convert_options).await?;
    result.to_file("output.webp").await?;

    Ok(())
}
```

### Metadata Preservation


```rust
use tinify::{Tinify, PreserveOptions, PreserveMetadata};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.jpg").await?;

    // Preserve copyright and creation time
    let preserve_options = PreserveOptions {
        preserve: vec![
            PreserveMetadata::Copyright,
            PreserveMetadata::Creation,
        ],
    };

    let mut result = source.preserve(preserve_options).await?;
    result.to_file("preserved.jpg").await?;

    Ok(())
}
```

### AWS S3 Cloud Storage


```rust
use tinify::{Tinify, StoreOptions, S3Options};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.png").await?;

    // Configure S3 storage options
    let s3_options = S3Options {
        service: "s3".to_string(),
        aws_access_key_id: "your-access-key".to_string(),
        aws_secret_access_key: "your-secret-key".to_string(),
        region: "us-east-1".to_string(),
        path: "my-bucket/images/compressed.png".to_string(),
        headers: None,
        acl: Some("public-read".to_string()),
    };

    // Store directly to S3
    let result = source.store(StoreOptions::S3(s3_options)).await?;

    if let Some(count) = result.compression_count() {
        println!("API usage count: {}", count);
    }

    Ok(())
}
```

### Google Cloud Storage


```rust
use tinify::{Tinify, StoreOptions, GCSOptions};
use serde_json::json;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.png").await?;

    // Configure GCS storage options
    let gcs_options = GCSOptions {
        service: "gcs".to_string(),
        gcp_access_token: "your-access-token".to_string(),
        path: "my-bucket/images/compressed.png".to_string(),
        headers: Some(json!({
            "Cache-Control": "public, max-age=31536000",
            "X-Goog-Meta-Source": "tinify-rs"
        })),
    };

    // Store directly to GCS
    let result = source.store(StoreOptions::GCS(gcs_options)).await?;

    Ok(())
}
```

### URL-based Processing


```rust
use tinify::Tinify;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;

    // Load image from URL
    let source = client.source_from_url("https://example.com/image.jpg").await?;
    source.to_file("compressed.jpg").await?;

    Ok(())
}
```

### Buffer-based Processing


```rust
use tinify::Tinify;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;

    // Create source from in-memory bytes
    let image_data = std::fs::read("input.png")?;
    let source = client.source_from_buffer(image_data).await?;

    // Get compressed bytes
    let compressed_data = source.to_buffer().await?;
    std::fs::write("output.png", compressed_data)?;

    Ok(())
}
```

## πŸ”§ API Reference


### Resize Methods


| Method | Description | Use Case |
|--------|-------------|----------|
| `Scale` | Proportional scaling | Precise width or height control |
| `Fit` | Fit within dimensions (preserve aspect ratio) | Create largest image within bounds |
| `Cover` | Cover dimensions (may crop) | Fill exact dimensions, preserve ratio |
| `Thumb` | Smart thumbnail | Auto-detect important regions |

### Supported Image Formats


| Format | Input Support | Output Support | Description |
|--------|---------------|----------------|-------------|
| PNG | βœ… | βœ… | Lossless compression, transparency support |
| JPEG | βœ… | βœ… | Lossy compression, ideal for photos |
| WebP | βœ… | βœ… | Modern format, smaller file sizes |
| AVIF | ❌ | βœ… | Next-gen format, best compression |

### Cloud Storage Support


| Service | Support Status | Notes |
|---------|----------------|--------|
| AWS S3 | βœ… | Full support with custom headers and ACL |
| Google Cloud Storage | βœ… | Full support with metadata |
| S3-Compatible Services | βœ… | MinIO, DigitalOcean Spaces, Backblaze B2, etc. |

## ⚠️ Error Handling


The library provides comprehensive error types:

```rust
use tinify::{Tinify, TinifyError};

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("api-key".to_string())?;

    match client.source_from_file("input.png").await {
        Ok(source) => {
            println!("Processing successful");
            // Continue processing...
        }
        Err(TinifyError::FileNotFound { path }) => {
            println!("File not found: {}", path);
        }
        Err(TinifyError::UnsupportedFormat { format }) => {
            println!("Unsupported format: {}", format);
        }
        Err(TinifyError::FileTooLarge { size, max_size }) => {
            println!("File too large: {} bytes (max: {} bytes)", size, max_size);
        }
        Err(TinifyError::QuotaExceeded) => {
            println!("API quota exhausted");
        }
        Err(TinifyError::AccountError { status, message }) => {
            println!("Account error [{}]: {}", status, message);
        }
        Err(e) => {
            println!("Other error: {}", e);
        }
    }

    Ok(())
}
```

## πŸ“Š Performance Optimization


### Async Concurrent Processing


```rust
use tinify::Tinify;
use tokio::task::JoinSet;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let mut join_set = JoinSet::new();

    // Process multiple images concurrently
    let files = vec!["image1.png", "image2.jpg", "image3.webp"];

    for (i, file) in files.iter().enumerate() {
        let client = client.clone();
        let file = file.to_string();

        join_set.spawn(async move {
            let source = client.source_from_file(&file).await?;
            let output = format!("compressed_{}.png", i);
            source.to_file(&output).await?;
            Ok::<String, tinify::TinifyError>(output)
        });
    }

    // Wait for all tasks to complete
    while let Some(result) = join_set.join_next().await {
        match result {
            Ok(Ok(filename)) => println!("βœ… Compressed: {}", filename),
            Ok(Err(e)) => println!("❌ Compression failed: {}", e),
            Err(e) => println!("❌ Task error: {}", e),
        }
    }

    Ok(())
}
```

### Batch Processing


```rust
use tinify::{Tinify, ResizeOptions, ResizeMethod};

async fn batch_process_images(
    client: &Tinify,
    input_files: Vec<&str>,
) -> Result<(), Box<dyn std::error::Error>> {
    for file in input_files {
        // Compress and resize
        let source = client.source_from_file(file).await?;

        let resize_options = ResizeOptions {
            method: ResizeMethod::Fit,
            width: Some(800),
            height: Some(600),
        };

        let mut result = source.resize(resize_options).await?;
        let output = format!("processed_{}", file);
        result.to_file(&output).await?;

        println!("βœ… Processed: {} -> {}", file, output);
    }

    Ok(())
}
```

## 🌐 Cloud Storage Integration


### AWS S3 Examples


```rust
use tinify::{Tinify, StoreOptions, S3Options};
use serde_json::json;

// Basic S3 upload
let s3_options = S3Options {
    service: "s3".to_string(),
    aws_access_key_id: "your-access-key".to_string(),
    aws_secret_access_key: "your-secret-key".to_string(),
    region: "us-east-1".to_string(),
    path: "my-bucket/images/compressed.png".to_string(),
    headers: None,
    acl: Some("public-read".to_string()),
};

// S3 upload with custom headers
let s3_options_with_headers = S3Options {
    service: "s3".to_string(),
    aws_access_key_id: "your-access-key".to_string(),
    aws_secret_access_key: "your-secret-key".to_string(),
    region: "us-east-1".to_string(),
    path: "my-bucket/images/compressed.png".to_string(),
    headers: Some(json!({
        "Cache-Control": "public, max-age=31536000",
        "Content-Disposition": "inline; filename=\"optimized.png\""
    })),
    acl: Some("public-read".to_string()),
};

let source = client.source_from_file("input.png").await?;
let result = source.store(StoreOptions::S3(s3_options)).await?;
```

### S3-Compatible Storage


Supports various S3-compatible storage services:

- **MinIO**: Self-hosted object storage
- **DigitalOcean Spaces**: Simple cloud storage
- **Backblaze B2**: Affordable cloud storage
- **Wasabi**: High-performance cloud storage

```rust
// MinIO configuration example
let minio_options = S3Options {
    service: "s3".to_string(),
    aws_access_key_id: "minioadmin".to_string(),
    aws_secret_access_key: "minioadmin".to_string(),
    region: "us-east-1".to_string(),
    path: "test-bucket/compressed.png".to_string(),
    headers: None,
    acl: None,
};
```

## 🎯 Complete Feature Showcase


Check out examples in the `examples/` directory:

- `01_compressing_images.rs` - Basic image compression
- `02_resizing_images.rs` - Image resizing operations
- `03_converting_images.rs` - Format conversion
- `04_preserving_metadata.rs` - Metadata preservation
- `05_saving_to_s3.rs` - AWS S3 storage
- `06_saving_to_gcs.rs` - Google Cloud Storage
- `07_error_handling.rs` - Error handling patterns
- `08_compression_count.rs` - Compression counter tracking
- `09_s3_compatible_storage.rs` - S3-compatible services
- `10_comprehensive_demo.rs` - Complete feature demonstration

Run examples:

```bash
# Basic compression example

cargo run --example 01_compressing_images

# Cloud storage test

export TINIFY_API_KEY="your-api-key"
export AWS_ACCESS_KEY_ID="your-aws-key"
export AWS_SECRET_ACCESS_KEY="your-aws-secret"
cargo run --example 05_saving_to_s3

# Error handling demonstration

cargo run --example 07_error_handling
```

## πŸ” API Quota Management


```rust
use tinify::Tinify;

#[tokio::main]

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Tinify::new("your-api-key".to_string())?;
    let source = client.source_from_file("input.png").await?;
    let result = source.to_buffer().await?;

    // Check compression count
    if let Some(count) = result.compression_count() {
        println!("Current API usage: {}", count);

        if count > 450 {
            println!("⚠️ Approaching free quota limit (500/month)");
        }
    }

    Ok(())
}
```

## βš™οΈ Environment Setup


### Environment Variables


```bash
# Tinify API configuration

export TINIFY_API_KEY="your-tinify-api-key"

# AWS S3 configuration

export AWS_ACCESS_KEY_ID="your-aws-access-key"
export AWS_SECRET_ACCESS_KEY="your-aws-secret-key"

# Google Cloud Storage configuration

export GCP_ACCESS_TOKEN="your-gcp-access-token"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
```

### Getting API Key


1. Visit [TinyPNG Developer Page]https://tinypng.com/developers
2. Register account and verify email
3. Get free API key (500 compressions/month)
4. Upgrade to paid plan for higher quotas

## πŸ§ͺ Testing


```bash
# Run all tests

cargo test

# Run doc tests

cargo test --doc

# Run specific example

cargo run --example 01_compressing_images

# Test with real images

cargo run --example test_real_image

# Cloud storage integration tests

./test_cloud_storage.sh
```

## πŸ“‹ System Requirements


- **Rust**: 1.70.0 or higher
- **Operating System**: Windows, macOS, Linux
- **Network**: Stable internet connection for TinyPNG API access
- **Memory**: Minimum 100MB available memory for image processing

## 🚨 Limitations and Considerations


### API Limitations


- **Free Quota**: 500 compressions/month
- **File Size**: Maximum 5MB per file
- **Supported Formats**: PNG, JPEG, WebP (input), PNG, JPEG, WebP, AVIF (output)
- **Concurrency**: Recommended max 10 concurrent requests

### Best Practices


1. **API Key Security**: Never hardcode API keys, use environment variables
2. **Error Handling**: Always properly handle network and API errors
3. **Quota Monitoring**: Regularly check API usage to avoid limits
4. **File Validation**: Validate file format and size before upload
5. **Concurrency Control**: Manage concurrent request count appropriately

```rust
// Recommended error handling pattern
match client.source_from_file("input.png").await {
    Ok(source) => {
        // Successful processing
    }
    Err(TinifyError::QuotaExceeded) => {
        // Quota exhausted, stop processing or wait for next month
        eprintln!("API quota exhausted, wait for next month or upgrade plan");
    }
    Err(TinifyError::FileTooLarge { size, max_size }) => {
        // File too large, consider preprocessing
        eprintln!("File too large: {} bytes (max: {})", size, max_size);
    }
    Err(e) => {
        // Other errors, log and possibly retry
        eprintln!("Compression failed: {}", e);
    }
}
```

## 🀝 Contributing


We welcome contributions of all kinds!

### Development Setup


```bash
# Clone repository

git clone https://github.com/raynoryim/tinify.git
cd tinify-rs

# Install dependencies and run tests

cargo test

# Run clippy checks

cargo clippy

# Run formatting

cargo fmt

# Run all checks

cargo check --examples
```

### Submitting PRs


1. Fork the repository
2. Create feature branch: `git checkout -b feature/amazing-feature`
3. Commit changes: `git commit -m 'feat: add amazing feature'`
4. Push branch: `git push origin feature/amazing-feature`
5. Create Pull Request

### Reporting Issues


Please report bugs or request features in [GitHub Issues](https://github.com/raynoryim/tinify/issues).

## πŸ“„ License


This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## πŸ”— Related Links


- **Documentation**: [docs.rs/tinify]https://docs.rs/tinify
- **Crates.io**: [crates.io/crates/tinify]https://crates.io/crates/tinify
- **TinyPNG API**: [tinypng.com/developers]https://tinypng.com/developers
- **Issue Tracker**: [GitHub Issues]https://github.com/raynoryim/tinify/issues

## πŸ™ Acknowledgments


- [TinyPNG]https://tinypng.com/ for providing excellent image compression API
- Rust community for amazing libraries and tools
- All contributors and users for their support

---

⭐ If this project helps you, please give us a star!