• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

camatcode / basenji / 81b6c37c77d8ae82554fa9f03e976087d8009986

27 Jul 2025 01:38PM UTC coverage: 81.908% (+0.2%) from 81.695%
81b6c37c77d8ae82554fa9f03e976087d8009986

Pull #72

github

camatcode
perf: introduce oban telemetry
Pull Request #72: perf: introduce telemetry

76 of 86 new or added lines in 14 files covered. (88.37%)

40 existing lines in 10 files now uncovered.

1159 of 1415 relevant lines covered (81.91%)

500.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.44
/lib/basenji/process/image_processor.ex
1
defmodule Basenji.ImageProcessor do
2
  @moduledoc false
3

4
  alias Basenji.Reader.Process.JPEGOptimizer
5

6
  def get_image_preview(binary, preview_width_target, preview_height_target) do
7
    with {:ok, image} <- Image.from_binary(binary),
58✔
8
         {:ok, preview} <- make_preview(image, preview_width_target, preview_height_target) do
58✔
9
      bytes =
58✔
10
        Image.write!(preview, :memory, suffix: ".jpg")
11
        |> JPEGOptimizer.optimize!()
12

13
      {:ok, bytes}
14
    end
15
  end
16

17
  def resize_image(binary, opts \\ []) when is_binary(binary) do
18
    if Keyword.has_key?(opts, :width) || Keyword.has_key?(opts, :height) do
33✔
19
      with {:ok, image} <- Image.from_binary(binary) do
23✔
20
        {original_width, original_height} = {Image.width(image), Image.height(image)}
23✔
21
        {scale, options} = calculate_resize_opts(original_width, original_height, opts)
23✔
22
        {:ok, resized} = Image.resize(image, scale, options)
23✔
23

24
        {:ok, Image.write!(resized, :memory, suffix: ".jpg")}
25
      end
26
    else
27
      {:ok, binary}
28
    end
29
  end
30

31
  defp make_preview(image, preview_width_target, preview_height_target) do
32
    width = Image.width(image)
58✔
33
    height = Image.height(image)
58✔
34

35
    opts = thumbnail_opts(width, height)
58✔
36
    Image.thumbnail(image, "#{preview_width_target}x#{preview_height_target}", opts)
58✔
37
  end
38

UNCOV
39
  defp thumbnail_opts(width, height) when width > height * 2 or height > width * 2,
×
40
    do: [fit: :cover, crop: :attention, resize: :down]
41

42
  defp thumbnail_opts(_width, _height), do: [fit: :contain, resize: :down]
58✔
43

44
  defp calculate_resize_opts(start_width, start_height, opts) do
45
    if Keyword.has_key?(opts, :width) && Keyword.has_key?(opts, :height) do
23✔
46
      horizontal_scale = opts[:width] / start_width
5✔
47
      vertical_scale = opts[:height] / start_height
5✔
48
      {horizontal_scale, [vertical_scale: vertical_scale]}
49
    else
50
      if Keyword.has_key?(opts, :width) do
18✔
51
        {opts[:width] / start_width, []}
52
      else
53
        {opts[:height] / start_height, []}
54
      end
55
    end
56
  end
57
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc