resmush is an R package for optimizing local image files, directories and online images with the reSmush.it API. The API is free for personal use and does not require an API key. reSmush.it is also available through WordPress and other tools.
The reSmush.it API provides:
Optimize and download an online JPEG image with resmush_url():
library(resmush)
url <- "https://dieghernan.github.io/resmush/img/jpg_example_original.jpg"
resmush_url(url, outfile = "jpg_example_compress.jpg", overwrite = TRUE)
#> ══ resmush summary ══════════════════════════════════════════════════════════
#> ℹ Input: 1 URL, 178.7 Kb total.
#> ✔ Optimized 1 URL: size is now 45 Kb (was 178.7 Kb). Saved 133.7 Kb (74.82%).
#> Saved result in directory 'C:/user/john_doe/AppData/Local/Temp/'.Use the qlty argument to adjust the optimization level for JPEG files. For best results, use values above 90.
# Use a low optimization level.
resmush_url(
url,
outfile = tempfile(fileext = ".jpg"),
overwrite = TRUE,
qlty = 3
)
#> ══ resmush summary ══════════════════════════════════════════════════════════
#> ℹ Input: 1 URL, 178.7 Kb total.
#> ✔ Optimized 1 URL: size is now 2.2 Kb (was 178.7 Kb). Saved 176.4 Kb (98.74%).
#> Saved result in directory 'C:/user/john_doe/AppData/Local/Temp/'.qlty = 3), compared with Figure 1 (b).
All optimization functions return, invisibly, a data frame summarizing the optimization. Successful API calls also write the optimized files to disk. The following example shows the returned data frame for a local image file:
png_file <- system.file("extimg/example.png", package = "resmush")
# Copy to a temporary file for this example.
tmp_png <- tempfile(fileext = ".png")
file.copy(png_file, tmp_png, overwrite = TRUE)
#> [1] TRUE
summary <- resmush_file(tmp_png, overwrite = TRUE)
tibble::as_tibble(summary[, -c(1, 2)])
#> # A tibble: 1 × 6
#> src_size dest_size compress_ratio notes src_bytes dest_bytes
#> <chr> <chr> <chr> <chr> <dbl> <dbl>
#> 1 239.9 Kb 70.7 Kb 70.54% OK 245618 72356Several other R packages provide image optimization tools:
xfun::tinify(): Similar to resmush_file() but uses TinyPNG and requires an API key.xfun::optipng(): Compresses local files using OptiPNG, which must be installed locally.oxipng, optional lossy PNG palette reduction, and JPEG re-encoding via mozjpeg.xfun::optipng() but with more options. Requires additional local software.| Tool | CRAN | Additional software | Online images | API key required | Limits |
|---|---|---|---|---|---|
xfun::tinify() |
Yes | No | Yes | Yes | 500 compressions/month (free tier) |
xfun::optipng() |
Yes | Yes | No | No | No |
| tinieR | No | No | Yes | Yes | 500 compressions/month (free tier) |
| tinyimg | Yes | Yes (Rust toolchain) | No | No | No |
| optout | No | Yes | No | No | No |
| resmush | Yes | No | Yes | No | Personal use, files under 5 MB |
| Tool | PNG | JPEG | GIF | BMP | TIFF | WebP | |
|---|---|---|---|---|---|---|---|
xfun::tinify() |
✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
xfun::optipng() |
✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
| tinieR | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| tinyimg | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| optout | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
| resmush | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
In practice, resmush is designed for quick image optimization with minimal setup, including support for online images and formats such as GIF, BMP and TIFF. Packages such as tinyimg may be a better fit for fully local workflows focused on PNG / JPEG optimization and fine-grained control over compression settings.