Created
September 30, 2022 23:19
-
-
Save jgb-solutions/f8cbbf9e1810fce03e2b8266ded73758 to your computer and use it in GitHub Desktop.
Photon Image service module in Elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule JGBSolutions.Image do | |
# https://developer.wordpress.com/docs/photon/api/ | |
@photon_hosts ["i0.wp.com", "i1.wp.com", "i2.wp.com", "i3.wp.com"] | |
def get_photon_url(url, opts \\ []) do | |
url = Application.get_env(:jgb_solutions, :domain) <> url | |
if String.starts_with?(url, "http") do | |
url_without_protocol = make_url_without_protocol(url) | |
photon_host = Enum.random(@photon_hosts) | |
{photon_query, _opts} = | |
{"", opts} | |
|> width() | |
|> height() | |
|> crop() | |
|> resize() | |
|> fit() | |
|> ulb() | |
|> lb() | |
|> filter() | |
|> brightness() | |
|> contrast() | |
|> colorize() | |
|> smooth() | |
|> zoom() | |
|> quality() | |
|> strip() | |
"https://#{photon_host}/#{url_without_protocol}?#{photon_query}" | |
else | |
url | |
end | |
end | |
def width({string, %{width: width} = opts}), do: {string <> "&w=#{width}", opts} | |
def width(tuple), do: tuple | |
def height({string, %{height: height} = opts}), do: {string <> "&h=#{height}", opts} | |
def height(tuple), do: tuple | |
def crop({string, %{crop: crop} = opts}) do | |
%{x: x, y: y, w: w, h: h} = crop | |
{string <> "&crop=#{x},#{y},#{w},#{h}", opts} | |
end | |
def crop(tuple), do: tuple | |
def resize({string, %{resize: resize} = opts}) do | |
%{width: width, height: height} = resize | |
{string <> "&resize=#{width},#{height}", opts} | |
end | |
def resize(tuple), do: tuple | |
def fit({string, %{fit: fit} = opts}) do | |
%{width: width, height: height} = fit | |
{string <> "&fit=#{width},#{height}", opts} | |
end | |
def fit(tuple), do: tuple | |
def ulb({string, %{ulb: _ulb} = opts}), do: {string <> "&ulb=true", opts} | |
def ulb(tuple), do: tuple | |
def lb({string, %{lb: lb} = opts}) do | |
%{width: width, height: height} = lb | |
{string <> "&lb=#{width},#{height}", opts} | |
end | |
def lb(tuple), do: tuple | |
def filter({string, %{filter: filter} = opts}), | |
do: {string <> "&filter=#{filter}", opts} | |
def filter(tuple), do: tuple | |
def brightness({string, %{brightness: brightness} = opts}), | |
do: {string <> "&brightness=#{brightness}", opts} | |
def brightness(tuple), do: tuple | |
def contrast({string, %{contrast: contrast} = opts}), | |
do: {string <> "&contrast=#{contrast}", opts} | |
def contrast(tuple), do: tuple | |
def colorize({string, %{colorize: colorize} = opts}) do | |
%{red: red, green: green, blue: blue} = colorize | |
{string <> "&colorize=#{red},#{green},#{blue}", opts} | |
end | |
def colorize(tuple), do: tuple | |
def smooth({string, %{smooth: smooth} = opts}), do: {string <> "&smooth=#{smooth}", opts} | |
def smooth(tuple), do: tuple | |
def zoom({string, %{zoom: zoom} = opts}), | |
do: {string <> "&zoom=#{zoom}", opts} | |
def zoom(tuple), do: tuple | |
def quality({string, %{quality: quality} = opts}), | |
do: {string <> "&quality=#{quality}", opts} | |
def quality(tuple), do: tuple | |
def strip({string, %{strip: strip} = opts}) do | |
{string <> "&strip=#{strip}", opts} | |
end | |
def strip(tuple), do: tuple | |
def make_url_without_protocol(domain), do: String.replace(domain, ~r/(^\w+:|^)\/\//, "") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment