compress_images: Minify size your images

Description Usage Arguments Examples

View source: R/compress_images.R

Description

Minify size of images located in a directory and output results in another folder.

This function can only be used after executing the command compress_images_install() which installs a set of "npm" packages on your machine.

Usage

1
compress_images(dir_input, dir_output = NULL)

Arguments

dir_input, dir_output

input and output directories

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# generate some png
from_dir <- tempfile()
dir.create(from_dir)

png(file = file.path(
  from_dir, "file_1.png"), width = 8.3, height = 11.7,
  units = "in", res = 300)
barplot(1:10, col = 1:10)
dev.off()

png(file = file.path(
  from_dir, "file_2.png"), width = 8.3, height = 11.7,
  units = "in", res = 300)
barplot(1:10, col = 11:20)
dev.off()

library(locatexec)
if(exec_available("node") &&
   compress_images_available()){

  # generate dest folder
  new_dir <- tempfile()
  dir.create(new_dir)

  # run compression
  compress_images(from_dir, new_dir)

  desc_folder <- function(dir){
    all_files <- list.files(
      path = dir, full.names = TRUE, recursive = TRUE)
    data.frame(
      basename = basename(all_files),
      size = file.info(all_files)$size)
  }

  desc_folder(from_dir)
  desc_folder(new_dir)
}

ardata-fr/cookimage documentation built on Feb. 26, 2021, 12:24 a.m.