Giter Club home page Giter Club logo

fastimage's Introduction

Hex Version Hex docs License Build Status Code coverage status Hex.pm

Fastimage

Description

Fastimage finds the dimensions/size or file type of a remote or local image file given the file path or url respectively. It streams the smallest amount of data necessary to ascertain the file size. This aspect is useful when getting the file size for very large images.

Features

  • Supports bmp, jpeg, png, webp and gif files
  • Supports local files by using the file path of the image
  • Supports blobs/objects by using the binary of the image
  • Supports remote files by using the url of the image
  • Follows redirects for a given url
  • Fastimage.info/1 yields the image info as a struct %Fastimage{}
  • Fastimage.size/1 yields the image size as a struct %Fastimage.Dimensions{width: _w, height: _h}
  • Fastimage.type/1 yields the image type as an atom :bmp, :jpeg, :gif, :webp or :png

Examples

Fastimage.info("https://raw.githubusercontent.com/stephenmoloney/fastimage/master/priv/test.jpg")
# => {:ok,
#      %Fastimage{
#        dimensions: %Fastimage.Dimensions{height: 142, width: 283},
#        image_type: :jpeg,
#        source: "https://raw.githubusercontent.com/stephenmoloney/fastimage/master/priv/test.jpg",
#        source_type: :url
#      }}

Fastimage.type("https://raw.githubusercontent.com/stephenmoloney/fastimage/master/priv/test.jpg")
# => {:ok, :jpeg}

Fastimage.size("https://raw.githubusercontent.com/stephenmoloney/fastimage/master/priv/test.jpg")
# => {:ok, %Fastimage.Dimensions{height: 142, width: 283}}

See docs for further examples

Installation

Add fastimage to your list of dependencies in mix.exs:

def deps do
  [
    {:fastimage, "~> 1.0.0-rc4"}
  ]
end

Tests

mix test

Benchmarks

mix bench

Credit/Acknowledgements

Licence

MIT Licence

fastimage's People

Contributors

eirenauts-infra avatar kagux avatar kuon avatar sneako avatar stephenmoloney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fastimage's Issues

Create simplification functions for test fixtures

*** Issue: ***

  • The test fixtures can be simplified even further as there is a pattern, see below:
  @gh_raw_url "https://raw.githubusercontent.com/stephenmoloney/fastimage/master/priv/"
  @jpg_url "#{@gh_raw_url}test.jpg"
  @jpg_url_with_query "https://avatars0.githubusercontent.com/u/12668653?v=2&s=40"
  @jpg_with_redirect "http://seanmoloney.com/images/cover1.jpg"
  @png_url "#{@gh_raw_url}test.png"
  @gif_url "#{@gh_raw_url}test.gif"
  @bmp_url "#{@gh_raw_url}test.bmp"
  @webp_vp8l_url "#{@gh_raw_url}webp_vp8l.webp"
  @webp_vp8_url "#{@gh_raw_url}webp_vp8.webp"
  @webp_vp8x_url "#{@gh_raw_url}webp_vp8x.webp"

  @jpg_file "./priv/test.jpg"
  @png_file "./priv/test.png"
  @gif_file "./priv/test.gif"
  @bmp_file "./priv/test.bmp"
  @webp_vp8_file "./priv/test_vp8.webp"
  @webp_vp8x_file "./priv/test_vp8x.webp"
  @webp_vp8l_file "./priv/test_vp8l.webp"

  @jpg_binary File.read!(@jpg_file)
  @png_binary File.read!(@png_file)
  @gif_binary File.read!(@gif_file)
  @bmp_binary File.read!(@bmp_file)
  @webp_vp8_binary File.read!(@webp_vp8_file)
  @webp_vp8x_binary File.read!(@webp_vp8x_file)
  @webp_vp8l_binary File.read!(@webp_vp8l_file)

dependencies are incompatible with Phoenix

I've spend a few hours trying to reconcile dependencies. The issue is coming from gun requiring cowlib 1.3.0, while Phoenix is on cowboy ~> 1.0.0 which requires cowlib ~> 1.0.0 and is incompatible if you try to force 1.3.0 on it.
With lower gun version that works with cowlib ~> 1.0.0 your lib breaks.
With cowlib ~> 1.0.0, override: true gun compilation break.
In the end I got it to work with my gun fork that relaxes cowlib dependency.

     {:cowlib, "~> 1.0.0", manager: :rebar, override: true},
     {:gun, github: "kagux/gun", override: true},
     {:ranch, "~> 1.2.0", manager: :rebar, override: true},

But I can't be sure of all consequences. Could be that issue I reported before is related to this change.
I think it would benefit your lib a lot to be compatible with latest version of Phoenix.

consider adding try catch block for file.stream!

in Fastimage.File, wrap in try catch so that error is catched and returned as Fastimage.Error.

  @spec recv(String.t(), :file) :: {:ok, binary(), File.Stream.t()} | {:error, Error.t()}
  defp recv(file_path, :file) do
#    {:ok, _data, _file_stream} =
#    try do
      file_path
      |> File.stream!([:read, :compressed, :binary], @file_chunk_size)
      |> stream_chunks(1, {0, <<>>, file_path}, 0, 0)
#    catch
#    end
  end

Deadlock with hackney 1.11

I open an issue here for reference.

With hackney 1.11 I have a deadlock when I use fastimage, all details here: benoitc/hackney#447 (comment)

I am not sure if the bug is in fastimage or hackney itself, but as I don't have the bug with hackney 1.9 I thought it might be hackney.

1.0rc checklist

  • returned errors, some degree of tidy up needed here for errors bubbling up from private functions
  • make multiple versions of :hackney acceptable
  • support binaries in addition to a file path or uri
  • change return image type return types to atoms @type image_types :: :bmp | :gif | :jpg | :png
  • change errors to Elixir Exceptions - eg UnknownTypeError
  • introduce type -> {:ok, type} and size -> {:ok, size}, type!, and size! function formats [will need to change raise(inspect(error)) -> raise(error) ]
  • credo linting
  • ensure close_stream in all relevant locations
  • added new Fastimage.info function
  • add options to type/1, size/1 and info/1 to allow manual overrides on
    the otherwised fixed values for stream_timeout, max_redirect_retries and
    max_error_retries

review line 66 of the Parser module

Description:

  • review line 66 of the Parser module (# get more data if less that 4 bytes remaining)
  • is it necessary and functioning as expected?

Images aren't processing well

I'm trying to process this image via Fastimage.size(image_path): 20151004001184375674-original

But it returns me exception

iex(3)> Fastimage.size(path)
** (MatchError) no match of right hand side value: ""
    (fastimage) lib/fastimage.ex:257: Fastimage.matching_byte/2
    (fastimage) lib/fastimage.ex:247: Fastimage.next_bytes_until_match/2
    (fastimage) lib/fastimage.ex:159: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:190: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:187: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:190: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:187: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:190: Fastimage.parse_jpeg/6
    (fastimage) lib/fastimage.ex:43: Fastimage.size/1

What should I do here?

BMP returns height: 0

Fastimage.size("https://raw.githubusercontent.com/myFree/public_eg/main/SampleBMP.bmp")
{:ok, %Fastimage.Dimensions{height: 0, width: 2560}}

The height of the image is 1920px

something's wrong with multiple consecutive size/1 calls

hey,

first of all, thanks a lot for your work on this library!
I wanted to use it to get dimensions for multiple images and noticed that results get mixed up. As in I request sizes for images [...., A, ... B, .... ] consecutively and often image B size is in fact coming from image A. with delay added between calls less images get affected until it's big enough to disappear completely.
do you have any ideas why this could be happening?

Changing API to {:ok, }, {:error, }

Hi man, firstly thanks for your work! I've been using FastImage in one of my apps in production.

Just curious about your thoughts on changing the API to more inline with other similar Elixir behavior.

FastImage.size() -> {:ok, %{width: _, height: _}} 

FastImage.size!() -> {%width: _, height: _}}

Definitely down to send in a PR with this change. Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.