Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Make it easier for users to shrink image sizes #614

Closed
apreshill opened this issue Apr 15, 2021 · 4 comments
Closed

[FR] Make it easier for users to shrink image sizes #614

apreshill opened this issue Apr 15, 2021 · 4 comments
Labels
next to consider for next release

Comments

@apreshill
Copy link
Contributor

I would love a function way to do this, built into blogdown, since I struggle with this all the time!

I also have been looking at: https://jmablog.github.io/tinieR/

Also I know this is important: https://bookdown.org/yihui/rmarkdown-cookbook/optipng.html

It would be cool to offer a way to do this based on regex, like all resize & optimize all images in content/*thumbnail* 😉

Similar to: hugodown::use_tidy_thumbnail()

@yihui
Copy link
Member

yihui commented Apr 15, 2021

That's a good idea. I used to manually optimize images. I'll look into tinieR. Thanks!

@cderv
Copy link
Collaborator

cderv commented Apr 15, 2021

For pure shrinking of the size for optimization (like tinieR if I understand correctly), I believe xfun::optipng() does it correctly but it is targeted to a whole directory. webshot::shrink() or future webshot2::shrink() (they are the same) is another option for optipng.

For resizing there is also, webshot2::resize() (or webshot::resize()) . They are using system requirement though GraphicsMagick or ImageMagick.

I think we could make one version based on magick package.

(and that is what use_tidy_thumbnail() is doing: https://github.com/r-lib/hugodown/blob/97ea0cdd63152a58987ae9e2639bcc4db4e5a83a/R/tidy.R#L30)

@yihui
Copy link
Member

yihui commented Apr 15, 2021

Simply shrinking an image to the desired dimension (i.e. resizing) is easy. Optimizing image losslessly is hard. I just did some quick testing and https://tinypng.com seems to be quite decent in reducing the file size of both PNG and JPG images.

@yihui yihui added the next to consider for next release label Apr 15, 2021
yihui added a commit to yihui/xfun that referenced this issue May 13, 2021
…d use it: rstudio/blogdown#614)

the tinieR package is a more comprehensive implementation of the Tinify API but I only need one feature (i.e. shrink) and the package has several additional dependencies (fs, glue, httr, png, jpeg, rprojroot, yaml)
clrpackages pushed a commit to clearlinux-pkgs/R-xfun that referenced this issue May 18, 2021
Yihui Xie (23):
      make the title shorter, otherwise it could be wrapped by `R CMD build` and 'Yihui Xie' will be marked as "mis-spelled" by `R CMD check`
      clarify a news item
      add a function news2md() to convert package news to Markdown
      rstudio/tinytex@4772b5e
      add a category argument to news2md() so that category names could be discarded
      don't generate the bullet list if all items are empty
      BDR suggest authors not use ftp:// sites
      fix #52: default to using renv::install() to install packages when pkg_install() is called inside an renv project
      forgot to suggest renv
      address BDR's request to not use ftp:// (it is still supported, but at some point we may have to only support uploading via https://win-builder.r-project.org/upload.aspx)
      default lib = NULL for renv::install() (#52)
      add helper functions stop2() and warning2(), which uses call. = FALSE
      add a function format_bytes() borrowed from tinytex:::auto_size() and built on top of format.object_size()
      add a function tinify() to compress images (e.g., blogdown users could use it: rstudio/blogdown#614)
      escape %
      move a few functions
      rename src to pkg in install_dir(), and factor out the code to build the tarball to pkg_build()
      xfun::upload_win_builder() can be called without arguments now
      use [[ to get the file instead of list(file); delete the package tarball if it is automatically built
      a lightweight version of submit_cran()
      ignore readme to avoid CRAN's URL checking
      fix #51: don't add backticks to math environments when \begin{} and \end{} do not match
      CRAN release v0.23
@yihui yihui moved this to Backlog in R Markdown Team Projects Aug 8, 2022
@yihui yihui moved this from Backlog to In Progress in R Markdown Team Projects Aug 8, 2022
Repository owner moved this from In Progress to Done in R Markdown Team Projects Aug 8, 2022
yihui added a commit that referenced this issue Aug 8, 2022
@yihui
Copy link
Member

yihui commented Aug 8, 2022

The development version of xfun has a function xfun::shrink_images() now:

remotes::install_github('yihui/xfun')

If you call it with the argument tinify = TRUE, this function will both resize images (using magick) and compress them (using TinyPNG, which requires an API key to be set up). This is briefly documented in the blogdown book: https://bookdown.org/yihui/blogdown/useful-resources.html#file-optimization More detailed documentation is on the help pages ?xfun::shrink_images and ?xfun::tinify. Thanks!

clrpackages pushed a commit to clearlinux-pkgs/R-xfun that referenced this issue Aug 12, 2022
Martin Mächler (1):
      specify the default library location since base_pkgs() can "hang"

Yihui Xie (11):
      start the next version
      roxygenize and bump version
      forgot to pass ... to dir.create() in dir_create()
      close #68: add support for more file extensions
      bump version
      factor out a function all_files() that changes list.files()'s default argument values
      make it possible to return all logo paths
      close rstudio/blogdown#614: add a function shrink_images() to shrink (and optionally compress) images under a directory
      also support .webp images
      svg images can fail on GHA's Linux machines
      CRAN release v0.32
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Dec 18, 2022
# CHANGES IN xfun VERSION 0.35

- Added a new argument `token` to `protect_math()` to optionally
  include a token around math expressions.

- `base64_uri()` relies less on the **mime** package now. For some
  common file extensions (e.g., `.jpg`/`.png`), this function knows
  their MIME types.

- `stringsAsStrings()` has been removed from this package.

# CHANGES IN xfun VERSION 0.34

- Added a new function `alnum_id()` to generate ID strings from a character vector.

- The function `stringsAsStrings()` has been deprecated.

# CHANGES IN xfun VERSION 0.33

- Reverted the change for #68: the characters `-+!_#` are no longer
  accepted by default in filename extensions, since they are
  relatively rare and caused a breakage in rstudio/bookdown#1369. If
  you wish to allow for these characters, you may use the new `extra`
  argument in `file_ext()` and related functions, e.g.,
  `xfun::file_ext(x, extra = '-+!_#')`.

- The function `stringsAsStrings()` will be deprecated in a future
  release of **xfun**, because the global option `stringsAsFactors =
  FALSE` has become the default in base R since 4.0.0.

# CHANGES IN xfun VERSION 0.32

- Added a function `shrink_images()` to shrink images to a maximum
  width using the **magick** package (thanks, @apreshill,
  rstudio/blogdown#614).

- Added a function `tinify_dir()` as a wrapper of `tinify()` to
  compress images under a directory.

- `file_ext()` supports more file extensions now, such as `.c++`,
  `.FB2K-COMPONENT`, and so on (thanks, @tentacles-from-outer-space,
  #68).

- Fixed the issue that `xfun::base_pkgs()` could hang R (thanks,
  @mmaechler, #66).

- The `...` argument in `dir_create()` was not passed to `dir.create()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next to consider for next release
Projects
Archived in project
Development

No branches or pull requests

3 participants