Skip to content

Commit

Permalink
Fix issues in handling prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Oct 29, 2024
1 parent 74f3a52 commit d2b7964
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 14 additions & 2 deletions lib/vix/vips/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,18 @@ defmodule Vix.Vips.Image do
end
end

@spec new_from_file(String.t(), keyword) :: {:ok, t()} | {:error, term()}

def new_from_file(path) do
path =
path
|> Path.expand()
|> normalize_string()

Nif.nif_image_new_from_file(path)
|> wrap_type()
end

@doc """
Opens `path` for reading, returns an instance of `t:Vix.Vips.Image.t/0`
Expand Down Expand Up @@ -336,8 +348,8 @@ defmodule Vix.Vips.Image do
from `Vix.Vips.Operation`. For example for jpeg use
`Vix.Vips.Operation.jpegload/2`
"""
@spec new_from_file(String.t(), keyword) :: {:ok, t()} | {:error, term()}
def new_from_file(path, opts \\ []) do
@doc since: "0.31.0"
def new_from_file(path, opts) do
with {:ok, path} <- normalize_path(path),
{:ok, loader} <- Vix.Vips.Foreign.find_load(path),
{:ok, {ref, _optional}} <- Operation.Helper.operation_call(loader, [path], opts) do
Expand Down
18 changes: 16 additions & 2 deletions test/vix/vips/image_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ defmodule Vix.Vips.ImageTest do
end

test "new_from_file" do
assert {:error, :invalid_path} == Image.new_from_file("invalid.jpg")
assert {:error, "Failed to find load"} == Image.new_from_file(__ENV__.file)
assert {:error, :invalid_path} == Image.new_from_file("invalid.jpg", [])
assert {:error, "Failed to find load"} == Image.new_from_file(__ENV__.file, [])

assert {:ok, %Image{ref: ref}} = Image.new_from_file(img_path("puppies.jpg"))
assert is_reference(ref)
Expand All @@ -32,6 +32,20 @@ defmodule Vix.Vips.ImageTest do
assert Image.width(img1) == 2 * Image.width(img2)
end

test "new_from_file supports optional options suffix" do
assert {:ok, %Image{ref: ref} = img1} =
Image.new_from_file(img_path("puppies.jpg"))

assert is_reference(ref)

assert {:ok, %Image{ref: ref} = img2} =
Image.new_from_file(img_path("puppies.jpg") <> "[shrink=2]")

assert is_reference(ref)

assert Image.width(img1) == 2 * Image.width(img2)
end

test "new_from_file ignores invalid options" do
# png does not support `shrink` option
assert {:ok, %Image{ref: ref}} =
Expand Down

0 comments on commit d2b7964

Please sign in to comment.