tinify | R Documentation |
Shrink an image's (PNG or JPG) filesize with the TinyPNG API.
tinify(file, overwrite, suffix, quiet, return_path, resize, key = NULL)
file |
String, required. A string detailing the path to the file you wish to shrink, relative to the current working directory or as an absolute file path. Can include sub-directories and must include the file extension (.png or .jpg/.jpeg only). |
overwrite |
Boolean, defaults to |
suffix |
String, defaults to |
quiet |
Boolean, defaults to |
return_path |
String or |
resize |
Named list or |
key |
String, optional. A string containing your TinyPNG API key. Not
required if the API key is set using |
If return_path = "proj"
, return_path = "rel"
, or return_path = "abs"
, a string with the project, relative, or absolute path to the newly
tinified image file. If no project can be identified for return_path = "proj"
, returns NA
. If return_path = "all"
, a named list with all file
paths included as $project
, $relative
, and $absolute
respectively. If
return_path = NULL
, no return value.
If any argument is provided to tinify()
when called, it will overwrite the
default option set by tinify_defaults()
.
You can get a TinyPNG API key from
https://tinypng.com/developers. TinyPNG is smart enough to know when you
are uploading the same file again, and so will not count repeat calls of
tinify()
on the same image file against your monthly API usage limit. This
can be useful if, for example, you are using tinify()
in an RMarkdown
document as it won't count against your API usage every time you knit your
document. But, be aware that use of resize
also counts as an additional
API call, as the image is first reduced in filesize, then a second API call
is made to resize the newly tinified file.
tinify_key()
to set an API key globally so it does not need to be
provided with every call of tinify()
tinify_defaults()
to set default arguments so they do not need to
be provided with every call of tinify()
## Not run: # Shrink a PNG file img <- system.file("extdata", "example.png", package = "tinieR") tinify(img) # Also works with JPEG/JPG files img_jpg <- system.file("extdata", "example.jpg", package = "tinieR") tinify(img_jpg) # Return absolute path to newly shrunk file: shrunk_img <- tinify(img, return_path = "abs") # Suppress messages detailing file reduction amount: tinify(img, quiet = TRUE) # Overwrite original file in place: tinify(img, overwrite = TRUE) # Change suffix on new file: tinify(img, suffix = "_small") # Resize an image with the method "scale", only providing a width: tinify(img, resize = list(method = "scale", width = 300)) # Or resize an image with any other method by providing both width and height: tinify(img, resize = list(method = "cover", width = 300, height = 150)) # Overwrite a global API key set in tinify_api(): tinify(img, key = "NEW-API-KEY-HERE") # You can combine any of the above: tinify(img, overwrite = TRUE, quiet = TRUE, return_path = "rel") # Plays nice with the pipe: img %>% tinify() # And with purrr::map for multiple files: imgs <- c("example.png", "example2.png") purrr::map(imgs, ~tinify(.x)) # An example method for shrinking an entire directory: imgs_dir <- fs::dir_ls("imgs", glob = "*.png") purrr::map(imgs_dir, ~tinify(.x, overwrite = TRUE, quiet = TRUE)) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.