Shrink an image's (PNG or JPG) filesize with the TinyPNG API.
tinify(file, overwrite, suffix, quiet, return_path, resize, key = NULL)
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).
Boolean, defaults to FALSE
. By default, tinify will create
a new file with the suffix '_tiny' and preserve the original file. Set
TRUE
to instead overwrite the original file, with the same file name.
String, defaults to "_tiny"
. By default, tinify will create a
new file with the suffix '_tiny' and preserve the original file. Provide a
new character string here to change the suffix from '_tiny' to your own
choice. Empty strings (""
) are not accepted. suffix
is ignored when
overwrite
is set to TRUE
.
Boolean, defaults to FALSE
. By default, tinify provides details
on file names, amount of file size reduction (% and Kb), and the number of
TinyPNG API calls made this month. If set to TRUE
, tinify displays no
messages as it shrinks files.
String or NULL
, optional. One of "proj
", "rel
", "abs
", or
"all
". If "proj
", will return the file path of the newly tinified image
file relative to the Rstudio project directory (looking for an .Rproj file).
If no project can be identified, returns NA
. If "rel
", will return the
file path of the newly tinified image file, relative to the current
working directory at the time tinify()
is called. If "abs
", will return
the absolute file path of the newly tinified image file. If "all
", will
return a named list with all file paths. If NULL
(the default), no file
path is returned.
Named list or NULL
, optional. A named list with the elements method
as
a string, and width
and/or height
as numerics. Please note you can only
reduce an image's dimensions and make an image smaller with TinyPNG API, not
make an image larger. Method must be set to one of "scale", "fit", "cover",
or "thumb". If using "scale", you only need to provide width
OR height
,
not both. If using any other method, you must supply both a width
AND
height
. See https://tinypng.com/developers/reference#resizing-images and
the examples for more. If NULL
(the default), no resizing takes place.
String, optional. A string containing your TinyPNG API key. Not
required if the API key is set using tinify_api()
. If an API key is
provided with tinify_api()
, any other key provided in the function call
will override the key set by tinify_api()
.
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()
if (FALSE) {
# 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))
}