Skip to content

cran/resmush

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

resmush resmush website

R-CMD-check codecov r-universe CodeFactor DOI Project Status: Active – The project has reached a stable, usable state and is being actively developed.

resmush is a R package that allow users to optimize and compress images using reSmush.it. reSmush.it is a free API that provides image optimization, and it has been implemented on Wordpress, Drupal or Magento.

Some of the features of reSmush.it are:

  • Free optimization services, no API key required.
  • Optimize local and online images.
  • Image files supported: png, jpg/jpeg, gif, bmp, tiff, webp.
  • Max image size: 5 Mb.
  • Compression via several algorithms:
    • PNGQuant: Strip unneeded chunks from pngs, preserving a full alpha transparency.
    • JPEGOptim: Lossless optimization based on optimizing the Huffman tables.
    • OptiPNG: png reducer that is used by several online optimizers.

Installation

Once accepted, you can install resmush from CRAN with:

install.packages("resmush")

You can install the development version of resmush from GitHub with:

# install.packages("devtools")
devtools::install_github("dieghernan/resmush")

Alternatively, you can install resmush using the r-universe:

# Install resmush in R:
install.packages("resmush", repos = c(
  "https://dieghernan.r-universe.dev",
  "https://cloud.r-project.org"
))

Example

Compressing an online jpg image:

library(resmush)

url <- paste0(
  "https://raw.githubusercontent.com/dieghernan/",
  "resmush/main/img/jpg_example_original.jpg"
)

resmush_url(url, outfile = "man/figures/jpg_example_compress.jpg", overwrite = TRUE)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 url with size 178.7 Kb
#> ✔ Success for 1 url: Size now is 45 Kb (was 178.7 Kb). Saved 133.7 Kb (74.82%).
#> See result in directory 'man/figures'.
Original online figure Optimized figure

Original picture (top) 178.7 Kb and optimized picture (bottom) 45 Kb (Compression 74.8%)

The quality of the compression can be adjusted in the case of jpg files using the parameter qlty. However, it is recommended to keep this value above 90 to get a good image quality.

# Extreme case
resmush_url(url,
  outfile = "man/figures/jpg_example_compress_low.jpg", overwrite = TRUE,
  qlty = 3
)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 url with size 178.7 Kb
#> ✔ Success for 1 url: Size now is 2.2 Kb (was 178.7 Kb). Saved 176.4 Kb (98.74%).
#> See result in directory 'man/figures'.
Low quality figure Low quality figure

Low quality image due to a high compression rate.

All the functions return invisibly a data set with a summary of the process. The next example shows how when compressing a local file.

png_file <- system.file("extimg/example.png", package = "resmush")

# For the example, copy to a temporary file
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      72356

Other alternatives

  • xfun package by Yihui Xie Sponsor Yihui Xie on GitHub has the following functions that optimize image files:
    • xfun::tinify() is similar to resmush_file() but uses TinyPNG. API key required.
    • xfun::optipng() compress local files with OptiPNG (that needs to be installed locally).
  • tinieR package by jmablog. R package that provides a full interface with TinyPNG.
  • optout package by @coolbutuseless. Similar to xfun::optipng() with additional options. Needs additional software installed locally.
  • Imgbot: Automatic optimization for files hosted in GitHub repos.

Citation

Hernangómez D (2024). resmush: Optimize and Compress Image Files with reSmush.it. doi:10.5281/zenodo.10556679, https://dieghernan.github.io/resmush/.

A BibTeX entry for LaTeX users is

@Manual{R-resmush,
  title = {{resmush}: Optimize and Compress Image Files with {reSmush.it}},
  author = {Diego Hernangómez},
  year = {2024},
  version = {0.1.0},
  doi = {10.5281/zenodo.10556679},
  url = {https://dieghernan.github.io/resmush/},
  abstract = {Compress local and online images using the reSmush.it API service <https://resmush.it/>.},
}

Attributions

Logo uses:

About

❗ This is a read-only mirror of the CRAN R package repository. resmush — Optimize and Compress Image Files with 'reSmush.it'. Homepage: https://dieghernan.github.io/resmush/https://github.com/dieghernan/resmush Report bugs for this package: https://github.com/dieghernan/resmush/issues

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages