R/read-image.R

Defines functions is_url get_file read_image

read_image <- function(path) {
  if (isTRUE(grepl("\\.png$", path, ignore.case = TRUE))) {
    img <- png::readPNG(get_file(path), native = TRUE)
  } else if (isTRUE(grepl("(\\.jpg$)|(\\.jpeg)", path, ignore.case = TRUE))) {
    img <- jpeg::readJPEG(get_file(path), native = TRUE)
  } else {
    warning(paste0("Image type not supported: ", path), call. = FALSE)
    img <- grDevices::as.raster(matrix(0, 10, 10))
  }
}

get_file <- function(path) {
  if (is_url(path)) {
    curl::curl_fetch_memory(path)$content
  } else {
    path
  }
}

is_url <- function(path)
{
  grepl("https?://", path)
}
clauswilke/gridtext documentation built on March 4, 2024, 2:45 p.m.