Skip to content

Latest commit

 

History

History
140 lines (106 loc) · 2.82 KB

005-aspectratio.livemd

File metadata and controls

140 lines (106 loc) · 2.82 KB

5 - Aspect Ratio

Mix.install([
  {:ex_image_info, "~> 0.2.4"}
])
Resolving Hex dependencies...
Dependency resolution completed:
New:
  ex_image_info 0.2.4
* Getting ex_image_info (Hex package)
==> ex_image_info
Compiling 12 files (.ex)
Generated ex_image_info app
:ok

ASPECT RATIO DE UNA IMAGEN

Enunciado

Crea un programa que se encargue de calcular el aspect ratio de una imagen a partir de una url.

Solución

# See https://stackoverflow.com/questions/30267943/elixir-download-a-file-image-from-a-url
defmodule Download do
  def from(url) do
    :inets.start()
    :ssl.start()
    Application.ensure_all_started(:inets)

    {:ok, response} =
      :httpc.request(:get, {String.to_charlist(url), []}, [], body_format: :binary)

    {{_, 200, 'OK'}, _headers, body} = response

    body
  end
end
{:module, Download, <<70, 79, 82, 49, 0, 0, 8, ...>>, {:from, 1}}
# See https://hexdocs.pm/ex_image_info/readme.html#examples
defmodule Image do
  require ExImageInfo
  defstruct [:data, :width, :height, :mime, :type]

  def from(data) do
    info = ExImageInfo.info(data)
    {mime, width, height, type} = info

    %__MODULE__{data: data, width: width, height: height, mime: mime, type: type}
  end
end
{:module, Image, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:from, 1}}
defmodule AspectRatio do
  # Greatest Common Divisor
  # See https://stackoverflow.com/questions/1186414/whats-the-algorithm-to-calculate-aspect-ratio
  defp gcd(a, b) when b == 0 do
    a
  end

  defp gcd(a, b) do
    gcd(b, rem(a, b))
  end

  def get(%Image{} = image) do
    divisor = gcd(image.width, image.height)
    "#{div(image.width, divisor)}:#{div(image.height, divisor)}"
  end
end
{:module, AspectRatio, <<70, 79, 82, 49, 0, 0, 9, ...>>, {:get, 1}}
defmodule Solution do
  def run(url) do
    Download.from(url)
    |> Image.from()
    |> AspectRatio.get()
  end
end
{:module, Solution, <<70, 79, 82, 49, 0, 0, 6, ...>>, {:run, 1}}
Solution.run("https://raw.githubusercontent.com/ElixirCL/elixircl.github.io/main/assets/logo.png")
"1007:1343"