--- title: "Using resmush" output: rmarkdown::html_vignette bibliography: REFERENCES.bib link-citations: yes vignette: > %\VignetteIndexEntry{Using resmush} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- **resmush** is a **R** package that allow users to optimize and compress images using [**reSmush.it**](https://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`. - Max image size: 5 Mb. - Compression via several algorithms: - [**PNGQuant**](https://pngquant.org/): Strip unneeded chunks from `png`s, preserving a full alpha transparency. - [**JPEGOptim**](https://github.com/tjko/jpegoptim)**:** Lossless optimization based on optimizing the Huffman tables. - [**OptiPNG**](https://optipng.sourceforge.net/): `png` reducer that is used by several online optimizers. ## Example Compressing an online `jpg` image: ``` r library(resmush) url <- paste0( "https://raw.githubusercontent.com/dieghernan/", "resmush/main/img/jpg_example_original.jpg" ) resmush_url(url, outfile = "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 '.'. ``` ::: figure [![](https://raw.githubusercontent.com/dieghernan/resmush/main/img/jpg_example_original.jpg){alt="Original uncompressed file" width="100%"}](https://raw.githubusercontent.com/dieghernan/resmush/main/img/jpg_example_original.jpg) [![](jpg_example_compress.jpg){alt="Optimized file" width="100%"}](https://dieghernan.github.io/resmush/reference/figures/jpg_example_compress.jpg)
::: 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. ``` r # Extreme case resmush_url(url, outfile = "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 '.'. ``` ::: figure [![Low quality figure](jpg_example_compress_low.jpg){width="100%"}](https://dieghernan.github.io/resmush/reference/figures/jpg_example_compress_low.jpg) ::: All the functions return invisibly a data set with a summary of the process. The next example shows how when compressing a local file. ``` r 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) tibble::as_tibble(summary[, -c(1, 2)]) #> # A tibble: 1 × 6 #> src_size dest_size compress_ratio notes src_bytes dest_bytes #>