Optimize all the local files of a directory (or list of directories) using the reSmush.it API.
Usage
resmush_dir(
dir,
ext = "\\.(png|jpe?g|bmp|gif|tif)$",
suffix = "_resmush",
overwrite = FALSE,
progress = TRUE,
report = TRUE,
recursive = FALSE,
...
)
Arguments
- dir
Character or vector of characters representing paths of local directories.
- ext
regex
indicating the extensions of the files to be optimized. The default value would capture all the extensions admitted by the API.- suffix
Character, defaults to
"_resmush"
. By default, a new file with the suffix is created in the same directory (i.e., optimizedexample.png
would beexample_resmush.png
). Values""
,NA
andNULL
would be the same thanoverwrite = TRUE
.- overwrite
Logical. Should the files in
dir
be overwritten? IfTRUE
suffix
would be ignored.- progress
Logical. Display a progress bar when needed.
- report
Logical. Display a summary report of the process in the console. See also Value.
- recursive
Logical. Should the
dir
file search recursive? See alsolist.files()
.- ...
Arguments passed on to
resmush_file
qlty
Only affects
jpg
files. Integer between 0 and 100 indicating the optimization level. For optimal results use vales above 90.exif_preserve
Logical. Should the Exif information (if any) deleted? Default is to remove it (i.e.
exif_preserve = FALSE
).
Value
Writes on disk the optimized file if the API call is successful in the
directories specified in dir
.
In all cases, a (invisible) data frame with a summary of the process is returned as well.
See also
reSmush.it API docs.
See resmush_clean_dir()
to clean a directory of previous runs.
Other functions for optimizing:
resmush_file()
,
resmush_url()
Examples
# \donttest{
# Get example dir and copy
example_dir <- system.file("extimg", package = "resmush")
temp_dir <- tempdir()
file.copy(example_dir, temp_dir, recursive = TRUE)
#> [1] TRUE
# Dest folder
dest_folder <- file.path(tempdir(), "extimg")
# Non-recursive
resmush_dir(dest_folder)
#> βΉ Resmushing 2 files
#> π Go! | β β β β β β β β β β β β β β β β β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘ 50% [3ms] | ETA: 0s (1/2 files)
#> π Go! | β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 100% [1.7s] | ETA: 0s (2/2 files)
#>
#> ββ resmush summary βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> βΉ Input: 2 files with size 340.2 Kb
#> β Success for 2 files: Size now is 153.8 Kb (was 340.2 Kb). Saved 186.4 Kb (54.79%).
#> See results in directory
#> C:/Users/RUNNER~1/AppData/Local/Temp/RtmpaIt4HO/extimg.
resmush_clean_dir(dest_folder)
#> βΉ Would remove 2 files:
#> β C:\Users\RUNNER~1\AppData\Local\Temp\RtmpaIt4HO/extimg/example_resmush.jpg
#> β C:\Users\RUNNER~1\AppData\Local\Temp\RtmpaIt4HO/extimg/example_resmush.png
# Recursive
summary <- resmush_dir(dest_folder, recursive = TRUE)
#> βΉ Resmushing 5 files
#> π Go! | β β β β β β β β β β β β β β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘β‘ 40% [587ms] | ETA: 1s (2/5 files)
#> π Go! | β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β β 100% [2.3s] | ETA: 0s (5/5 files)
#>
#> ββ resmush summary βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> βΉ Input: 5 files with size 401.7 Kb
#> β Success for 5 files: Size now is 173.5 Kb (was 401.7 Kb). Saved 228.2 Kb (56.81%).
#> See results in directories
#> C:/Users/RUNNER~1/AppData/Local/Temp/RtmpaIt4HO/extimg,
#> C:/Users/RUNNER~1/AppData/Local/Temp/RtmpaIt4HO/extimg/top1/nested,
#> C:/Users/RUNNER~1/AppData/Local/Temp/RtmpaIt4HO/extimg/top1, and
#> C:/Users/RUNNER~1/AppData/Local/Temp/RtmpaIt4HO/extimg/top2.
# Same info in the invisible df
summary[, -c(1, 2)]
#> src_size dest_size compress_ratio notes src_bytes dest_bytes
#> 1 100.4 Kb 83.2 Kb 17.15% OK 102796 85164
#> 2 239.9 Kb 70.7 Kb 70.54% OK 245618 72356
#> 3 17.8 Kb 6 Kb 66.48% OK 18214 6105
#> 4 25.9 Kb 7.7 Kb 70.09% OK 26499 7926
#> 5 17.8 Kb 6 Kb 66.48% OK 18214 6105
# Display with png
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 example
unlink(dest_folder, force = TRUE, recursive = TRUE)
# }