R/social-img.R

Defines functions img_size img_sizes readfun readimage save_img design_img

Documented in design_img img_size img_sizes readfun readimage save_img

#' Desgin a social media image with one of the templates
#'
#' @param social Name of the social media template
#' @param path Optional directory to the template
#' @param label Text to put in the image, use \\n as line break
#' @param x Optional, to specify x axis position for the text
#' @param y Optional, to specify y axis position for the text
#' @param font_size Optional, to specify the font size for the text
#'
#' @return It will print out the image and create a design_img_obj object with name, width, and height information for export
#' @export
#'
#' @examples design_img("twitter", "He he Example", font_size = 45)
#' @examples design_img("twitter", path = "./inst/twitter.jpeg", label = "He he Example", font_size = 45)
design_img <- function(social = NULL, label = NULL, x = NULL, y = NULL, font_size = NULL, path = NULL) {
  # set up parameters
  width <- img_sizes(social)$width
  height <- img_sizes(social)$height
  ylim <- height/width
  x <- ifelse(is.null(x), img_sizes(social)$x, x)
  y <- ifelse(is.null(y), img_sizes(social)$y, y*img_sizes(social)$y)
  font_size <- ifelse(is.null(font_size), img_sizes(social)$font_size, (font_size/16))

  if(is.null(path)) {
    j <- readimage(social)
  } else {
    j <- readimage(path = path)
  }


  # ads <- recordPlot()
  par(mar=rep(0, 4), xaxs='i', yaxs='i')#, family = "Droid Sans")
  plot(NULL, xlim=c(0,1), ylim=c(0,ylim),
       xaxt='n', yaxt='n',
       xlab = "", ylab = "", main = "",
       axes = FALSE, asp = 1,
       bg = "transparent")
  rasterImage(j,0,0,1,ylim)
  text(x = x, y = y,
       label,
       cex = font_size,
       col = "#686868")

  x <- recordPlot()

  structure(list(name = social,
                 plot = x,
                 width = width,
                 height = height),
            class = "social_media_img")

}

#' Save a design_img_obj to disk as a jpeg file
#'
#' @param design_img_obj A output from \code{design_img()}
#' @param name Optional, a name for the file to save on disk
#'
#' @return Writes a .jpeg file to your home directory
#' @export
#'
#' @examples save_img(design_img("twitter", "He he Example"))
save_img <- function(design_img_obj, name = NULL) {

  if(!is.social_media_img(design_img_obj)) stop("wrong input")

  name <- paste0(ifelse(is.null(name), design_img_obj$name, name), ".jpeg")

  grDevices::jpeg(name, width = design_img_obj$width,
       height = design_img_obj$height,
       units = 'px', quality = 1)

  design_img_obj

  grDevices::dev.off()
}

#' Read social media template stored in JPEG or PNG format
#'
#' Reads an image from JPEG or PNG content into a raster array.
#'
#' @param x The name of the template. It can be twitter, google_plus, facebook, instagram, wechat_rec, wechat_square
#' @param path Optional, a url of the template
#'
#' @return If the name of the template can be found in the template directory, it will return a raster array. If the template cannot be found in Template directory, it will return an error message.
#' @export
#' @import tools
#'
#' @examples readimage("twitter")
#' @examples readimage(path = "./inst/twitter.jpeg")
readimage <- function(x, path = NULL){
  path <- ifelse(is.null(path),
                 paste0("https://raw.githubusercontent.com/yizhexu/clamshell_data/master/", x, ".jpeg"),
                 path)
  ext <- tools::file_ext(path)
  if(!grepl("jpeg||jpg||png", path)) stop("Is your image one of the following format? jpeg, jpg, and png?")
  readfun(path, ext)
}

#' Function to download and read image
#'
#' @param path path of the image
#' @param ext extension of the image
#'
#' @export
readfun <- function(path, ext) {
  if(grepl("http", path)) {
    z <- tempfile()
    download.file(path, z, mode="wb")
  } else {
    z <- path
  }
  switch(ext,
         'jpg' = jpeg::readJPEG(z),
         'jpeg'= jpeg::readJPEG(z),
         "png" = png::readPNG(z))
}


#' Popular social media image specs
#'
#' Provides some popular social media image sizes
#'
#' @param name one of the following social media names: twitter, google_plus, facebook, instagram, wechat_rec, wechat_square
#'
#' @return a social_media_specs object with width and height of the image, x and y position for text in the image, and the default font size
#' @export
#'
#' @examples img_sizes("twitter")
img_sizes <- function(name) {

  switch(name,
         twitter = img_size(width = 1024, height = 512, x = 663.5, y = 380, font_size = 35),
         wechat_rec = img_size(1080, 600, 720.7, 300, 70),
         wechat_square = img_size(800, 800, 400, 470, 70),
         facebook = img_size(1200, 628, 784.5, 462, 39),
         google_plus = img_size(800, 800, 400, 509, 34),
         instagram = img_size(1080, 1080, 540, 687, 45))
}

#' Describe a social media image specs
#'
#' See \link{img_sizes} for a list of popular social media image specs
#'
#' @param width The required width of the image
#' @param height The required height of the image
#' @param x The x position for the text
#' @param y The y position for the text
#' @param font_size The font size for text
#'
#' @export
#'
#' @examples twitter <- img_size(width = 1024, height = 512, x = 663.5, y = 380, font_size = 35)
img_size <- function(width, height, x, y, font_size) {
  structure(list(width = width, height = height,
                 x = x/width, y = (height-y)/height/(width/height),
                 font_size = font_size/16),
            class = "social_media_specs")
}
yizhexu/rsocial documentation built on May 28, 2017, 9:42 a.m.