View source: R/resize_images.R
| resize_images | R Documentation |
Saves images to PNG with a specified width. As input it accepts (SVG, PDF, EPS, JPG, JPEG, TIF, TIFF, BMP, PNG) Saves to subdirectory '/resized' within input folder (or same directory as file if input is a single file)
resize_images(path, width)
path |
Character string. Path to a folder containing image files, or path to a single image file. |
width |
Numeric vector. Target width(s) in pixels for the output PNG files. Can be a single value (recycled for all files) or a vector matching the number of files found. |
This function:
Searches for image files with extensions: svg, pdf, eps, jpg, jpeg, tif, tiff, bmp, png
Creates a "resized" subfolder in the target directory if it doesn't exist
Converts each file to PNG format at the specified width(s)
Saves output files as: originalname_width.png in the resized subfolder
Supported input formats:
Vector graphics: SVG, PDF, EPS (rasterized using rsvg/magick)
Raster images: JPG, JPEG, TIF, TIFF, BMP, PNG
Invisibly returns TRUE on success.
Dependencies required: rsvg, magick, and tools (base R).
SVG files are rasterized using rsvg::rsvg(), while PDF/EPS and other
formats are handled by magick::image_read().
# Create a temporary PNG file and resize it
tmp_png <- tempfile(fileext = ".png")
grDevices::png(tmp_png, width = 400, height = 300)
old_par <- graphics::par(no.readonly = TRUE)
graphics::par(mar = c(2, 2, 1, 1))
graphics::plot(1:2, 1:2, type = "n")
grDevices::dev.off()
graphics::par(old_par)
resize_images(tmp_png, width = 80)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.