| Title: | Optimize and Compress Image Files with 'reSmush.it' |
|---|---|
| Description: | Optimize and compress local and online image files with the 'reSmush.it' 'API' <https://resmush.it/api/>. Process individual files or entire directories. The 'API' is free for personal use, accepts files smaller than 5 'MB' and supports 'PNG', 'JPEG', 'GIF', 'BMP' and 'TIFF' files. |
| Authors: | Diego Hernangómez [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8457-4658>) |
| Maintainer: | Diego Hernangómez <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.2 |
| Built: | 2026-07-02 21:45:51 UTC |
| Source: | https://github.com/cran/resmush |
Use with caution. Remove files that match suffix from one or more
directories. This is intended to clean output files created by
resmush_file() or resmush_dir().
resmush_clean_dir(dir, suffix = "_resmush", recursive = FALSE)resmush_clean_dir(dir, suffix = "_resmush", recursive = FALSE)
dir |
A character vector of directory paths. See the |
suffix |
A character string containing the suffix pattern used to
identify files. The value is interpreted as a regular expression. The
default is |
recursive |
Logical. Should the file search recurse into directories? |
An invisible() NULL. Messages list the files selected for removal.
resmush_file() and resmush_dir() create the suffixed output
files that this function removes.
# Create a temporary file with a suffix to remove. png_file <- system.file("extimg/example.png", package = "resmush") suffix <- "_would_be_removed" tmp_png <- file.path( tempdir(), paste0("example", suffix, ".png") ) file.exists(tmp_png) file.copy(png_file, tmp_png, overwrite = TRUE) file.exists(tmp_png) # Run with the default suffix. This should not remove the file. resmush_clean_dir(tempdir()) file.exists(tmp_png) # Use the matching suffix to remove the file. resmush_clean_dir(tempdir(), suffix = suffix) file.exists(tmp_png)# Create a temporary file with a suffix to remove. png_file <- system.file("extimg/example.png", package = "resmush") suffix <- "_would_be_removed" tmp_png <- file.path( tempdir(), paste0("example", suffix, ".png") ) file.exists(tmp_png) file.copy(png_file, tmp_png, overwrite = TRUE) file.exists(tmp_png) # Run with the default suffix. This should not remove the file. resmush_clean_dir(tempdir()) file.exists(tmp_png) # Use the matching suffix to remove the file. resmush_clean_dir(tempdir(), suffix = suffix) file.exists(tmp_png)
Optimize supported image files in one or more directories with the reSmush.it API. The API is free for personal use and accepts files smaller than 5 MB.
resmush_dir( dir, ext = "\\.(png|jpe?g|bmp|gif|tif)$", suffix = "_resmush", overwrite = FALSE, progress = TRUE, report = TRUE, recursive = FALSE, ... )resmush_dir( dir, ext = "\\.(png|jpe?g|bmp|gif|tif)$", suffix = "_resmush", overwrite = FALSE, progress = TRUE, report = TRUE, recursive = FALSE, ... )
dir |
A character vector of paths to local directories. |
ext |
A |
suffix |
A character string inserted before each output file extension.
The default is |
overwrite |
Logical. Should the input files be overwritten? If |
progress |
Logical. Should a progress bar be displayed? |
report |
Logical. Should a summary report be displayed in the console? |
recursive |
Logical. Should the file search within |
... |
Arguments passed on to
|
An invisibly returned data frame with one row per result and columns
containing source and destination paths, formatted and raw file sizes,
compression ratios and status notes. Returns NULL if no result is
available. Successful API calls also write the optimized files to disk.
resmush_clean_dir() removes output files created by previous runs.
The reSmush.it API documentation describes the external service.
Other image optimization functions:
resmush_file(),
resmush_url()
# Copy the example directory. example_dir <- system.file("extimg", package = "resmush") temp_dir <- tempdir() file.copy(example_dir, temp_dir, recursive = TRUE) # Create the destination folder path. dest_folder <- file.path(tempdir(), "extimg") # Optimize files non-recursively. resmush_dir(dest_folder) resmush_clean_dir(dest_folder) # Optimize files recursively. summary <- resmush_dir(dest_folder, recursive = TRUE) # Inspect the returned optimization summary. summary[, -c(1, 2)] # Display the PNG output. if (require("png", quietly = TRUE)) { a_png <- grepl("png$", summary$dest_img) my_png <- png::readPNG(summary[a_png, ]$dest_img[2]) grid::grid.raster(my_png) } # Clean up the example files. unlink(dest_folder, force = TRUE, recursive = TRUE)# Copy the example directory. example_dir <- system.file("extimg", package = "resmush") temp_dir <- tempdir() file.copy(example_dir, temp_dir, recursive = TRUE) # Create the destination folder path. dest_folder <- file.path(tempdir(), "extimg") # Optimize files non-recursively. resmush_dir(dest_folder) resmush_clean_dir(dest_folder) # Optimize files recursively. summary <- resmush_dir(dest_folder, recursive = TRUE) # Inspect the returned optimization summary. summary[, -c(1, 2)] # Display the PNG output. if (require("png", quietly = TRUE)) { a_png <- grepl("png$", summary$dest_img) my_png <- png::readPNG(summary[a_png, ]$dest_img[2]) grid::grid.raster(my_png) } # Clean up the example files. unlink(dest_folder, force = TRUE, recursive = TRUE)
Optimize one or more local image files with the reSmush.it API. The API is free for personal use and accepts files smaller than 5 MB.
resmush_file( file, suffix = "_resmush", overwrite = FALSE, progress = TRUE, report = TRUE, qlty = 92, exif_preserve = FALSE )resmush_file( file, suffix = "_resmush", overwrite = FALSE, progress = TRUE, report = TRUE, qlty = 92, exif_preserve = FALSE )
file |
A character vector of paths to local image files. The API can optimize PNG, JPEG, GIF, BMP and TIFF files. |
suffix |
A character string inserted before each output file extension.
The default is |
overwrite |
Logical. Should the input files be overwritten? If |
progress |
Logical. Should a progress bar be displayed? |
report |
Logical. Should a summary report be displayed in the console? |
qlty |
An integer between |
exif_preserve |
Logical. Should
EXIF metadata be preserved? The
default is |
An invisibly returned data frame with one row per result and columns
containing source and destination paths, formatted and raw file sizes,
compression ratios and status notes. Returns NULL if no result is
available. Successful API calls also write the optimized files to disk. If
report = TRUE, a summary is displayed in the console.
resmush_clean_dir() removes output files created by previous runs.
The reSmush.it API documentation describes the external service.
Other image optimization functions:
resmush_dir(),
resmush_url()
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) resmush_file(tmp_png) # Optimize multiple files. jpg_file <- system.file("extimg/example.jpg", package = "resmush") tmp_jpg <- tempfile(fileext = ".jpg") file.copy(jpg_file, tmp_jpg, overwrite = TRUE) # Display a summary in the console. summary <- resmush_file(c(tmp_png, tmp_jpg)) # Inspect the returned optimization summary. summary # Display the PNG output. if (require("png", quietly = TRUE)) { my_png <- png::readPNG(summary$dest_img[1]) grid::grid.raster(my_png) } # Adjust the JPEG quality level. resmush_file(tmp_jpg) resmush_file(tmp_jpg, qlty = 10)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) resmush_file(tmp_png) # Optimize multiple files. jpg_file <- system.file("extimg/example.jpg", package = "resmush") tmp_jpg <- tempfile(fileext = ".jpg") file.copy(jpg_file, tmp_jpg, overwrite = TRUE) # Display a summary in the console. summary <- resmush_file(c(tmp_png, tmp_jpg)) # Inspect the returned optimization summary. summary # Display the PNG output. if (require("png", quietly = TRUE)) { my_png <- png::readPNG(summary$dest_img[1]) grid::grid.raster(my_png) } # Adjust the JPEG quality level. resmush_file(tmp_jpg) resmush_file(tmp_jpg, qlty = 10)
Optimize and download one or more online image files with the reSmush.it API. The API is free for personal use and accepts files smaller than 5 MB.
resmush_url( url, outfile = file.path(tempdir(), basename(url)), overwrite = FALSE, progress = TRUE, report = TRUE, qlty = 92, exif_preserve = FALSE )resmush_url( url, outfile = file.path(tempdir(), basename(url)), overwrite = FALSE, progress = TRUE, report = TRUE, qlty = 92, exif_preserve = FALSE )
url |
A character vector of URLs pointing to hosted image files. The API can optimize PNG, JPEG, GIF, BMP and TIFF files. |
outfile |
A character vector of paths where optimized files are stored.
By default, files are created in |
overwrite |
Logical. Should existing files in |
progress |
Logical. Should a progress bar be displayed? |
report |
Logical. Should a summary report be displayed in the console? |
qlty |
An integer between |
exif_preserve |
Logical. Should
EXIF metadata be preserved? The
default is |
An invisibly returned data frame with one row per result and columns
containing source and destination paths, formatted and raw file sizes,
compression ratios and status notes. Returns NULL if no result is
available. Successful API calls also write the optimized files to disk. If
outfile contains duplicate paths, resmush_url() makes them unique with
suffixes such as _01 and _02.
resmush_clean_dir() removes output files created by previous runs.
The reSmush.it API documentation describes the external service.
Other image optimization functions:
resmush_dir(),
resmush_file()
# Define the base URL. base_url <- "https://raw.githubusercontent.com/dieghernan/resmush/main/inst/" png_url <- paste0(base_url, "/extimg/example.png") resmush_url(png_url) # Optimize multiple URLs. jpg_url <- paste0(base_url, "/extimg/example.jpg") summary <- resmush_url(c(png_url, jpg_url)) # Inspect the returned optimization summary. summary # Display the PNG output. if (require("png", quietly = TRUE)) { my_png <- png::readPNG(summary$dest_img[1]) grid::grid.raster(my_png) } # Adjust the JPEG quality level. resmush_url(jpg_url) resmush_url(jpg_url, qlty = 10)# Define the base URL. base_url <- "https://raw.githubusercontent.com/dieghernan/resmush/main/inst/" png_url <- paste0(base_url, "/extimg/example.png") resmush_url(png_url) # Optimize multiple URLs. jpg_url <- paste0(base_url, "/extimg/example.jpg") summary <- resmush_url(c(png_url, jpg_url)) # Inspect the returned optimization summary. summary # Display the PNG output. if (require("png", quietly = TRUE)) { my_png <- png::readPNG(summary$dest_img[1]) grid::grid.raster(my_png) } # Adjust the JPEG quality level. resmush_url(jpg_url) resmush_url(jpg_url, qlty = 10)