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.
Arguments
- 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
base::tempdir()with the samebase::basename()as each file inurl.outfilemust have the same length asurl.- overwrite
Logical. Should existing files in
outfilebe overwritten? IfFALSE, existing paths are made unique with a numeric suffix, such asexample_01.png.- progress
Logical. Should a progress bar be displayed?
- report
Logical. Should a summary report be displayed in the console?
- qlty
An integer between
0and100indicating the JPEG quality level. For best results, use values above90. This argument only affects JPEG files.- exif_preserve
Logical. Should EXIF metadata be preserved? The default is
FALSE, which removes it.
Value
An invisibly returned data frame with one row per result
and columns containing source and destination paths, formatted file sizes,
file sizes in bytes, 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.
Details
If outfile contains duplicate paths, resmush_url() makes them unique
with suffixes such as _01 and _02 unless overwrite = TRUE.
See also
The reSmush.it API documentation describes the external service.
Other image optimization functions:
resmush_dir(),
resmush_file()
Examples
# \donttest{
# 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)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 URL, 239.9 Kb total.
#> ✔ Optimized 1 URL: size is now 76.1 Kb (was 239.9 Kb). Saved 163.8 Kb (68.29%).
#> Saved result in directory /tmp/RtmpNxKsgB.
# Optimize multiple URLs.
jpg_url <- paste0(base_url, "/extimg/example.jpg")
summary <- resmush_url(c(png_url, jpg_url))
#> 🕐 reSmushing | ■■■■■■■■■■■■■■■■□□□□□□□□□□□□□□□ 50% [1ms] | ETA: 0s (1/2 UR…
#> 🕐 reSmushing | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% [915ms] | ETA: 0s (2/2 …
#>
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 2 URLs, 340.2 Kb total.
#> ✔ Optimized 2 URLs: size is now 159.2 Kb (was 340.2 Kb). Saved 181 Kb (53.20%).
#> Saved results in directory /tmp/RtmpNxKsgB.
# Inspect the returned optimization summary.
summary
#> src_img
#> 1 https://raw.githubusercontent.com/dieghernan/resmush/main/inst//extimg/example.png
#> 2 https://raw.githubusercontent.com/dieghernan/resmush/main/inst//extimg/example.jpg
#> dest_img src_size dest_size compress_ratio notes
#> 1 /tmp/RtmpNxKsgB/example_01.png 239.9 Kb 76.1 Kb 68.29% OK
#> 2 /tmp/RtmpNxKsgB/example.jpg 100.4 Kb 83.2 Kb 17.15% OK
#> src_bytes dest_bytes
#> 1 245618 77896
#> 2 102796 85164
# 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 summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 URL, 100.4 Kb total.
#> ✔ Optimized 1 URL: size is now 83.2 Kb (was 100.4 Kb). Saved 17.2 Kb (17.15%).
#> Saved result in directory /tmp/RtmpNxKsgB.
resmush_url(jpg_url, qlty = 10)
#> ══ resmush summary ═════════════════════════════════════════════════════════════
#> ℹ Input: 1 URL, 100.4 Kb total.
#> ✔ Optimized 1 URL: size is now 6.4 Kb (was 100.4 Kb). Saved 94 Kb (93.61%).
#> Saved result in directory /tmp/RtmpNxKsgB.
# }
