Skip to content

Commit

Permalink
Add exif argument
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jan 21, 2024
1 parent 01d20ac commit 80f210b
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 15 deletions.
3 changes: 0 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ references:
email: jeroen@berkeley.edu
orcid: https://orcid.org/0000-0002-4035-0289
year: '2024'
identifiers:
- type: url
value: https://curl.se/libcurl/
- type: software
title: httr
abstract: 'httr: Tools for Working with URLs and HTTP'
Expand Down
15 changes: 12 additions & 3 deletions R/resmush_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#' @param qlty Only affects `jpg` files. Integer between 0 and 100 indicating
#' the optimization level. For optimal results use vales above 90.
#' @param verbose Logical. If `TRUE` displays a summary of the results.
#'
#' @param exif_preserve Logical. Should be the
#' [Exif](https://en.wikipedia.org/wiki/Exif) information removed as well?
#' Default is to remove (i.e. `exif_preserve = FALSE`).
#' @return
#' Writes on disk the optimized file if the API call is successful.
#' In any case, a (invisibly) data frame with a summary of the process is
Expand Down Expand Up @@ -52,7 +54,8 @@
#' resmush_file(jpg_file, outfile = tmp_jpg, verbose = TRUE)
#' resmush_file(jpg_file, outfile = tmp_jpg, verbose = TRUE, qlty = 10)
#' }
resmush_file <- function(file, outfile = file, qlty = 92, verbose = FALSE) {
resmush_file <- function(file, outfile = file, qlty = 92, verbose = FALSE,
exif_preserve = FALSE) {
# Master table with results
res <- data.frame(
src_img = file, dest_img = NA, src_size = NA,
Expand Down Expand Up @@ -89,8 +92,14 @@ resmush_file <- function(file, outfile = file, qlty = 92, verbose = FALSE) {
file_post$name <- basename(file)

# Send to reSmusht
# Make logical
exif_preserve <- isTRUE(exif_preserve)

api_post <- httr::POST(
paste0("http://api.resmush.it/?qlty=", qlty),
paste0(
"http://api.resmush.it/?qlty=", qlty,
"&exif=", exif_preserve
),
body = list(files = file_post)
)

Expand Down
12 changes: 8 additions & 4 deletions R/resmush_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#' resmush_url(jpg_url, verbose = TRUE, qlty = 10)
#' }
resmush_url <- function(url, outfile = file.path(tempdir(), basename(url)),
qlty = 92, verbose = FALSE) {
qlty = 92, verbose = FALSE, exif_preserve = FALSE) {
# Master table with results
res <- data.frame(
src_img = url, dest_img = NA, src_size = NA,
Expand All @@ -67,7 +67,7 @@ resmush_url <- function(url, outfile = file.path(tempdir(), basename(url)),

# API Calls -----

res_get <- smush_from_url(url, qlty, n_rep = 3)
res_get <- smush_from_url(url, qlty, exif_preserve, n_rep = 3)

if ("error" %in% names(res_get)) {
cli::cli_alert_warning("API Error for {.href {url}}")
Expand Down Expand Up @@ -139,12 +139,16 @@ resmush_url <- function(url, outfile = file.path(tempdir(), basename(url)),

# Helper function for retrying the call
# Useful for some cases e.g imgur
smush_from_url <- function(url, qlty, n_rep = 3) {
smush_from_url <- function(url, qlty, exif_preserve = TRUE, n_rep = 3) {
# Make logical
exif_preserve <- isTRUE(exif_preserve)

for (i in seq(1, n_rep)) {
api_get <- httr::GET(
paste0(
"http://api.resmush.it/ws.php?qlty=", qlty,
"&img=", url
"&img=", url,
"&exif=", exif_preserve
)
)

Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
},
"SystemRequirements": null
},
"fileSize": "440.846KB",
"fileSize": "447.577KB",
"releaseNotes": "https://github.com/dieghernan/resmush/blob/master/NEWS.md",
"readme": "https://github.com/dieghernan/resmush/blob/main/README.md",
"contIntegration": ["https://github.com/dieghernan/resmush/actions/workflows/check-full.yaml", "https://app.codecov.io/gh/dieghernan/resmush"],
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CMD
CodeFactor
Exif
Flaticon
Flaticons
Freepik
Expand Down
12 changes: 11 additions & 1 deletion man/resmush_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/resmush_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions tests/testthat/_snaps/remush_url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Test offline

Code
dm <- resmush_url(png_url)
Message
! Offline

---

Code
dm
Output
src_img
1 https://raw.githubusercontent.com/dieghernan/resmush/main/inst/extimg/example.png
dest_img src_size dest_size compress_ratio notes
1 NA NA NA NA Offline

# Test corner

Code
dm <- resmush_url(png_url)
Message
! API Not responding, check <https://resmush.it/status>

---

Code
dm
Output
src_img
1 https://raw.githubusercontent.com/dieghernan/resmush/main/inst/extimg/example.png
dest_img src_size dest_size compress_ratio
1 NA NA NA NA
notes
1 API Not responding, check https://resmush.it/status}

# Test not url

Code
dm <- resmush_url(turl)
Message
! API Error for <https://dieghernan.github.io/aaabbbccc.png>
i Error 401: Cannot copy from remote url

---

Code
dm
Output
src_img dest_img src_size dest_size
1 https://dieghernan.github.io/aaabbbccc.png NA NA NA
compress_ratio notes
1 NA 401: Cannot copy from remote url

# Not valid file

Code
dm <- resmush_url(turl)
Message
! API Error for <https://raw.githubusercontent.com/dieghernan/resmush/main/README.md>
i Error 403: Unauthorized extension. Allowed are : JPG, PNG, GIF, BMP, TIFF,
WEBP

---

Code
dm
Output
src_img dest_img
1 https://raw.githubusercontent.com/dieghernan/resmush/main/README.md NA
src_size dest_size compress_ratio
1 NA NA NA
notes
1 403: Unauthorized extension. Allowed are : JPG, PNG, GIF, BMP, TIFF, WEBP

Loading

0 comments on commit 80f210b

Please sign in to comment.