Packages

Mix tasks and library for converting jpeg, png, etc images to webp

Current section

5 Versions

Jump to

Compare versions

5 files changed
+143 additions
-43 deletions
  @@ -28,13 +28,19 @@ def deps do
28 28 end
29 29 ```
30 30
31 - ## Usage: there are 2 ways to use as a mix task or as a library -- mix task usage detailed below
31 + ## Usage: 3 ways to use: static_compressors, mix task or library -- compressor and mix task usage detailed below
32 32
33 - ```bash
34 - $ mix webp default "assets/images/"
35 - ```
33 + # Static Compressor for phoenix
36 34
35 + Note that installation requires that Phoenix watchers can accept `MFArgs`
36 + tuples – so you must have Phoenix > v1.5.9.
37 37
38 + ```elixir
39 + config :phoenix,
40 + static_compressors: [Webp.Compressor]
41 + ```
42 +
43 + # mix task
38 44 ## Profiles
39 45
40 46 The first argument to `webp` is the execution profile.
  @@ -53,11 +59,7 @@ config :webp,
53 59 When `mix webp default` is invoked, the task arguments will be appended
54 60 to the ones configured above.
55 61
56 - ## Adding to Phoenix
57 -
58 - To add `webp` to an application using Phoenix, you need only four steps.
59 - Note that installation requires that Phoenix watchers can accept `MFArgs`
60 - tuples – so you must have Phoenix > v1.5.9.
62 + ## Adding to Phoenix w/o static compressor
61 63
62 64 First add it as a dependency in your `mix.exs`:
63 65
  @@ -132,7 +134,7 @@ width: 745px;
132 134 }
133 135 ```
134 136
135 - and the corresponding added to the layout
137 + and the following to the layout
136 138
137 139 ```html
138 140 <html class="no-js" lang="en">
  @@ -5,9 +5,25 @@
5 5 {<<"elixir">>,<<"~> 1.13">>}.
6 6 {<<"files">>,
7 7 [<<"lib">>,<<"lib/tasks">>,<<"lib/tasks/webp.ex">>,<<"lib/priv">>,
8 - <<"lib/priv/webp.bash">>,<<"lib/webp.ex">>,<<"mix.exs">>,<<"README.md">>]}.
8 + <<"lib/priv/webp.bash">>,<<"lib/compresser.ex">>,<<"lib/webp.ex">>,
9 + <<"mix.exs">>,<<"README.md">>]}.
9 10 {<<"licenses">>,[<<"MIT">>]}.
10 11 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/mithereal/ex_webp">>}]}.
11 12 {<<"name">>,<<"webp">>}.
12 - {<<"requirements">>,[]}.
13 - {<<"version">>,<<"0.1.0">>}.
13 + {<<"requirements">>,
14 + [[{<<"app">>,<<"eimp">>},
15 + {<<"name">>,<<"eimp">>},
16 + {<<"optional">>,false},
17 + {<<"repository">>,<<"hexpm">>},
18 + {<<"requirement">>,<<"~> 1.0">>}],
19 + [{<<"app">>,<<"phoenix">>},
20 + {<<"name">>,<<"phoenix">>},
21 + {<<"optional">>,false},
22 + {<<"repository">>,<<"hexpm">>},
23 + {<<"requirement">>,<<">= 0.0.0">>}],
24 + [{<<"app">>,<<"jason">>},
25 + {<<"name">>,<<"jason">>},
26 + {<<"optional">>,false},
27 + {<<"repository">>,<<"hexpm">>},
28 + {<<"requirement">>,<<"~> 1.0">>}]]}.
29 + {<<"version">>,<<"0.1.1">>}.
  @@ -0,0 +1,56 @@
1 + defmodule Webp.Compressor do
2 + @behaviour Phoenix.Digester.Compressor
3 +
4 + require Logger
5 +
6 + def compress_file(file_path, content) do
7 + valid_extension =
8 + Path.extname(file_path) in Application.get_env(:webp, :image_extensions, [
9 + ".png",
10 + ".jpg",
11 + ".jpeg"
12 + ])
13 +
14 + case valid_extension do
15 + true ->
16 + compressed_content =
17 + if :eimp.is_supported(:webp) do
18 + convert(content, :webp)
19 + else
20 + location = Application.get_env(:webp, :destination)
21 + params = %{location: location}
22 + Webp.convert(file_path, params)
23 + :error
24 + end
25 +
26 + case compressed_content do
27 + {:ok, data} ->
28 + {:ok, data}
29 +
30 + _ ->
31 + :error
32 + end
33 +
34 + false ->
35 + :error
36 + end
37 + end
38 +
39 + def file_extensions do
40 + [".webp"]
41 + end
42 +
43 + defp convert(content, options) do
44 + cond do
45 + Code.ensure_loaded?(:eimp) and function_exported?(:eimp, :convert, 2) ->
46 + :eimp.convert(content, options)
47 +
48 + false ->
49 + if Mix.env() in [:dev, :test] do
50 + Logger.error(":eimp Not Found")
51 + end
52 +
53 + :error
54 + end
55 + end
56 + end
  @@ -63,8 +63,8 @@ defmodule Webp do
63 63 cmd(path, args, [])
64 64 end
65 65
66 - defp cmd([command | args], extra_args, opts) do
67 - System.cmd(command, args ++ extra_args, opts)
66 + defp cmd(command, args, opts) do
67 + System.cmd(command, args, opts)
68 68 end
69 69
70 70 @doc """
  @@ -84,7 +84,6 @@ defmodule Webp do
84 84 raise "no arguments passed to webp"
85 85 end
86 86
87 -
88 87 opts = [
89 88 cd: config[:location] || File.cwd!(),
90 89 env: config[:env] || %{},
  @@ -93,32 +92,46 @@ defmodule Webp do
93 92 ]
94 93
95 94 args = [location] ++ extra_args
95 + glob = location <> "/*"
96 96
97 - files = File.glob(location)
97 + files = Path.wildcard(glob)
98 98
99 99 for source <- files do
100 100 path = Application.get_env(:webp, :path, "/usr/bin/cwebp")
101 101
102 - extname = Path.extname(source)
102 + valid_extension =
103 + Path.extname(source) in Application.get_env(:webp, :image_extensions, [
104 + ".png",
105 + ".jpg",
106 + ".jpeg"
107 + ])
103 108
104 - destination = destination || Path.basename(source, extname)
109 + case valid_extension do
110 + true ->
111 + extname = Path.extname(source)
105 112
106 - filename = Path.basename(source, extname)
113 + destination = destination || Path.basename(source, extname)
107 114
108 - params = "-q #{quality}} #{source} -o #{destination}/#{filename}.webp"
115 + filename = Path.basename(source, extname)
109 116
110 - command = path <> params
117 + params = "#{source} -o #{filename}.webp"
111 118
112 - path =
113 - if "--watch" in args do
114 - [script_path() | command]
115 - else
116 - command
117 - end
119 + command = path <> params
118 120
119 - path
120 - |> cmd(args, opts)
121 - |> elem(1)
121 + path =
122 + if "--watch" in args do
123 + [script_path() | command]
124 + else
125 + command
126 + end
127 +
128 + path
129 + |> cmd(args, opts)
130 + |> elem(1)
131 +
132 + false ->
133 + :error
134 + end
122 135 end
123 136 end
124 137
  @@ -147,21 +160,32 @@ defmodule Webp do
147 160 stderr_to_stdout: true
148 161 ]
149 162
150 - quality = params[:quality] || 80
151 -
152 163 path = Application.get_env(:webp, :path, "/usr/bin/cwebp")
153 164
154 - extname = Path.extname(source)
165 + valid_extension =
166 + Path.extname(source) in Application.get_env(:webp, :image_extensions, [
167 + ".png",
168 + ".jpg",
169 + ".jpeg"
170 + ])
155 171
156 - filename = Path.basename(source, extname)
172 + case valid_extension do
173 + true ->
174 + extname = Path.extname(source)
157 175
158 - destination = params[:destination] || Path.basename(source, extname)
176 + filename = Path.basename(source, extname)
159 177
160 - params = ["-q #{quality}}", "#{source} -o #{destination}/#{filename}.webp"]
178 + destination = params[:destination] || Path.basename(source, extname)
161 179
162 - path
163 - |> cmd(params, opts)
164 - |> elem(1)
180 + params = ["#{source}", "-o", "#{destination}"]
181 +
182 + path
183 + |> cmd(params, opts)
184 + |> elem(1)
185 +
186 + false ->
187 + :error
188 + end
165 189 end
166 190
167 191 @doc """
  @@ -1,7 +1,7 @@
1 1 defmodule ExWebp.MixProject do
2 2 use Mix.Project
3 3
4 - @version "0.1.0"
4 + @version "0.1.1"
5 5 @source_url "https://github.com/mithereal/ex_webp"
6 6
7 7 def project do
  @@ -20,7 +20,6 @@ defmodule ExWebp.MixProject do
20 20 ]
21 21 end
22 22
23 -
24 23 defp aliases do
25 24 [
26 25 c: "compile",
  @@ -64,7 +63,10 @@ defmodule ExWebp.MixProject do
64 63 [
65 64 # {:dep_from_hexpm, "~> 0.3.0"},
66 65 # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
67 - {:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
66 + {:eimp, "~> 1.0"},
67 + {:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
68 + {:phoenix, ">= 0.0.0"},
69 + {:jason, "~> 1.0"}
68 70 ]
69 71 end