-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage_component.ex
More file actions
139 lines (118 loc) · 4.12 KB
/
image_component.ex
File metadata and controls
139 lines (118 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
defmodule Literature.ImageComponent do
@moduledoc false
use Literature.Web, :html
alias Literature.Config
alias Literature.Uploaders.DifferentSizes
alias Literature.Uploaders.Helpers
def responsive_img_tag(assigns) do
assigns =
assigns
|> assign_new(:alt, fn -> "" end)
|> assign_new(:classes, fn -> "object-cover object-center w-full" end)
|> assign_new(:lazy_load, fn -> true end)
|> assign_new(:sizes, fn -> nil end)
~H"""
<%= if file = Map.get(@post, @field) do %>
<%= case get_post_field_img_size(@post, @field) do %>
<% {width, height} -> %>
<picture>
<source srcset={load_srcset(:webp, {file, @post})} sizes={@sizes} />
<source srcset={load_srcset(:jpg, {file, @post})} sizes={@sizes} />
<img
src={literature_image_url(@post, @field)}
alt={@alt}
width={width}
height={height}
class={@classes}
loading={if @lazy_load, do: "lazy"}
/>
</picture>
<% _nil -> %>
<img
src={literature_image_url(@post, @field)}
alt={@alt}
class={@classes}
loading={if @lazy_load, do: "lazy"}
/>
<% end %>
<% end %>
"""
end
def parse_image_tag(tag) when is_binary(tag) do
tag
|> Floki.parse_fragment!()
|> List.first()
|> parse_image_tag(tag)
end
def parse_image_tag({"img", _, _} = tag, img_str \\ nil) do
case get_img_size(tag) do
{width, height} ->
caption = find_img_attribute(tag, "title") || find_img_attribute(tag, "caption")
~s"""
<picture>
<source srcset="#{load_srcset(:jpg, find_img_attribute(tag, "src"))}"/>
<source srcset="#{load_srcset(:webp, find_img_attribute(tag, "src"))}"/>
<img src="#{find_img_attribute(tag, "src")}" alt="#{find_img_attribute(tag, "alt")}" width="#{width}" height="#{height}" loading="lazy" />
<figcaption style="font-style: italic;">#{caption}</figcaption>
</picture>
"""
_missing_size ->
img_str || Floki.raw_html(tag)
end
end
def build_images(params) do
~w(og_image twitter_image feature_image)
|> Enum.map(&{&1, params[&1]})
|> Enum.filter(fn {_, value} -> value end)
|> Enum.map(&rename_filename/1)
|> Enum.into(%{})
|> then(&Map.merge(params, &1))
end
defp load_srcset(version, {file, scope}) when version in ~w(jpg webp)a do
{width, _height} = get_original_size(file)
Range.new(100, width, Config.waffle_width_step())
|> Enum.map_join(", ", &"#{DifferentSizes.url({file, scope}, {version, &1})} #{&1}w")
end
defp load_srcset(version, url) when version in ~w(jpg webp)a do
url = String.replace(url, "\"", "") |> String.replace(~r/(jpeg|jpg|png)$/, to_string(version))
{width, height} = get_original_size(%{file_name: url})
Range.new(100, width, Config.waffle_width_step())
|> Enum.map_join(", ", &"#{String.replace(url, "w#{width}x#{height}", "w#{&1}")} #{&1}w")
end
defp get_original_size(%{file_name: file_name}) do
Helpers.get_dimension(file_name)
end
defp find_img_attribute(tag, attr) do
[tag]
|> Floki.attribute(attr)
|> List.first()
end
defp get_img_size(tag) do
tag
|> find_img_attribute("src")
|> Helpers.get_dimension()
end
defp get_post_field_img_size(struct, field) do
struct
|> literature_image_url(field)
|> Helpers.get_dimension()
end
defp rename_filename({field, file}) do
{width, height, _} = file.path |> Image.open!() |> Image.shape()
file_name =
Slugy.slugify(
"#{Path.basename(file.filename, Path.extname(file.filename))} w#{width}x#{height}"
)
{field, %{file | filename: "#{file_name}#{Path.extname(file.filename) |> String.downcase()}"}}
end
def any_upload_errors?(uploads) do
upload_names =
uploads
|> Map.get(:__phoenix_refs_to_names__, [])
|> Enum.map(fn {_ref, name} -> name end)
upload_names
|> Enum.map(&Map.fetch!(uploads, &1))
|> Enum.flat_map(&Map.get(&1, :errors, []))
|> Enum.any?()
end
end