derpycoder

derpycoder

Image.write for WebP file, doesn't resize the images, unlike .pngs

Hey @kip,

After admiring your work on Image library for a long time, I finally got a chance to use it in my project.

I have a mix task, that compiles the images from assets/images/* directory and puts them into priv/images/* directory.

It creates different variations for input images.

defmodule Mix.Tasks.Images.Compile do
  @moduledoc "Compile all images of projects, inspired by Benjamin Milde's elixir blog."
  @shortdoc "Compiles images"
  use Mix.Task

  @assets_folder "assets/images/"
  @image_sizes [300, 720, 960, 1200, 2000]

  @impl Mix.Task
  def run(_args) do
    src_paths =
      (@assets_folder <> "**/*.{png,webp,jpeg,jpg}")
      |> Path.wildcard()
      |> dbg()

    for src_path <- src_paths, size <- @image_sizes do
      file_name = src_path |> Path.rootname() |> Path.basename()
      ext = src_path |> Path.extname()

      path = src_path |> Path.split() |> Enum.drop(1) |> Path.join()
      dest_dir = "priv/static" |> Path.join(path) |> Path.dirname()
      dest_path = "#{dest_dir}/#{file_name}@#{size}#{ext}"

      {:ok, thumb} = Image.thumbnail(src_path, size)

      File.mkdir_p!(dest_dir)

      Image.write(thumb, dest_path,
        quality: 70,
        strip_metadata: true,
        minimize_file_size: true,
        compression: 6
      )
    end
  end
end

This produces following output: (For PNGs)

However, when I run the same code on WebP, it doesn’t work! Filename, may have changed, but the sizes remain the same.


What is your thoughts on tools like imgproxy?

After resize was done, I was thinking pre-compiling led to unnecessary resizing. For instance for images that are smaller than 500px, was being stretched to 1200px needlessly.

I will have to segregated folders and selectively resize them to different sizes, instead of resizing at runtime like imageproxy does.

Marked As Solved

kip

kip

ex_cldr Core Team

@derpycoder I just fixed a bug whereby :minimize_file_size wasn’t correctly applying :"min-size" option when writing a .webp image. That might help resolve the .webp image file size issue. I’ve not published an update to hex yet, I want to do some more testing with the updated evision that was just published first.

Also Liked

kip

kip

ex_cldr Core Team

I am working (slowly) on a similar capability in a separate library. I’m currently working on:

  • image_plug to provide a basic plug for serving images using a URL format similar to imgproxy.
  • image_cache to provide a caching layer over the top of image_plug.
  • image_components to provide a live view image component.
  • color to provide a standalone color library that image will then use.

I can’t estimate when they’ll be done but I will get them done.

That’s very strange! If you’re able to provide me an image example I’ll very happily take a look at it.

kip

kip

ex_cldr Core Team

I have pushed a commit that does what you proposed, but in keyword list format. For example:

iex>  Image.write(image, image_path, minimize_file_size: true,
...>    png: [compress: 60, lossy: true],
...>    jpg: [quality: 70],
...>    webp: [quality: 5])

I need to add tests before publishing but you’re welcome to try it out by configuring image from GitHub.

Since this is now getting very specific I suggest followup questions/discussion on this topic move to image discussions on github.

hauleth

hauleth

In most cases I prefer it over dealing with it on upload or whatever. Just slap some kind of cache in front of it to remove the need to duplicate work already done and you are good to go.

kip

kip

ex_cldr Core Team

@sodapopcan, :compression isn’t a valid option for .webp images. I checked the documentation and I can’t see a reference to it as an option so it’s just an example where different image types support different parameters for writing and therefore a generic writer is a bit tricky. Its why I added :minimise_file_size.

image returns an error on known but invalid options for a given image type so that there is no mysterious behaviour. Open to a discussion if that’s the right choice.

kip

kip

ex_cldr Core Team

Definitely agree, that’s why the :minimize_file_size option exists. But I messed the underlying options for .webp (now fixed). That’s the most generic “do the best you can” option to minimise the size of an image file.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement