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?

https://github.com/imgproxy/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.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement