{"id":13395084,"url":"https://github.com/sdsykes/fastimage","last_synced_at":"2025-05-11T05:46:37.696Z","repository":{"id":589299,"uuid":"223486","full_name":"sdsykes/fastimage","owner":"sdsykes","description":"FastImage finds the size or type of an image given its uri by fetching as little as needed","archived":false,"fork":false,"pushed_at":"2025-01-03T16:41:06.000Z","size":25647,"stargazers_count":1375,"open_issues_count":0,"forks_count":117,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-05-08T05:05:59.392Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://github.com/sdsykes/fastimage/tree/master","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sdsykes.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2009-06-10T13:53:18.000Z","updated_at":"2025-05-07T12:15:39.000Z","dependencies_parsed_at":"2024-03-28T19:38:01.771Z","dependency_job_id":"d95ff133-6629-4c6f-8885-56c91ebcd486","html_url":"https://github.com/sdsykes/fastimage","commit_stats":{"total_commits":271,"total_committers":42,"mean_commits":"6.4523809523809526","dds":0.3136531365313653,"last_synced_commit":"b65d75c8a315b7e5eaa18194eac1e25ee52dba48"},"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdsykes%2Ffastimage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdsykes%2Ffastimage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdsykes%2Ffastimage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sdsykes%2Ffastimage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sdsykes","download_url":"https://codeload.github.com/sdsykes/fastimage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253060737,"owners_count":21847584,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:41.506Z","updated_at":"2025-05-11T05:46:37.679Z","avatar_url":"https://github.com/sdsykes.png","language":"Ruby","readme":"[![https://rubygems.org/gems/fastimage](https://img.shields.io/gem/dt/fastimage.svg)](https://rubygems.org/gems/fastimage)\n[![https://github.com/sdsykes/fastimage/actions/workflows/test.yml](https://github.com/sdsykes/fastimage/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/sdsykes/fastimage/actions/workflows/test.yml)\n\n# FastImage\n\n**FastImage finds the size or type of an image given its uri by fetching as little as needed**\n\n## The problem\n\nYour app needs to find the size or type of an image. This could be for adding width and height attributes to an image tag, for adjusting layouts or overlays to fit an image or any other of dozens of reasons.\n\nBut the image is not locally stored - it's on another asset server, or in the cloud - at Amazon S3 for example.\n\nYou don't want to download the entire image to your app server - it could be many tens of kilobytes, or even megabytes just to get this information. For most common image types (GIF, PNG, BMP etc.), the size of the image is simply stored at the start of the file. For JPEG files it's a little bit more complex, but even so you do not need to fetch much of the image to find the size.\n\nFastImage does this minimal fetch for image types GIF, JPEG, PNG, TIFF, BMP, ICO, CUR, PSD, SVG, WEBP and JXL. And it doesn't rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).\n\nYou only need supply the uri, and FastImage will do the rest.\n\n## Features\n\n- Reads local (and other) files - anything that is not parseable as a URI will be interpreted as a filename, and - will attempt to open it with `File#open`.\n\n- Automatically reads from any object that responds to `:read` - for instance an IO object if that is passed instead of a URI.\n\n- Follows up to 4 HTTP redirects to get the image.\n\n- Obey the `http_proxy` setting in your environment to route requests via a proxy. You can also pass a `:proxy` argument if you want to specify the proxy address in the call.\n\n- Add a timeout to the request which will limit the request time by passing `:timeout =\u003e number_of_seconds`.\n\n- Returns `nil` if it encounters an error, but you can pass `:raise_on_failure =\u003e true` to get an exception.\n\n- Provides a reader for the content length header provided in HTTP. This may be useful to assess the file size of an image, but do not rely on it exclusively - it will not be present in chunked responses for instance.\n\n- Accepts additional HTTP headers. This can be used to set a user agent or referrer which some servers require. Pass an `:http_header` argument to specify headers, e.g., `:http_header =\u003e {'User-Agent' =\u003e 'Fake Browser'}`.\n\n- Gives you information about the parsed display orientation of an image with Exif data (jpeg or tiff).\n\n- Handles [Data URIs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs) correctly.\n\n## Security\n\nTake care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.\n\n## Examples\n\n```ruby\nrequire 'fastimage'\n\nFastImage.size(\"https://switchstep.com/images/ios.gif\")\n=\u003e [196, 283]  # width, height\nFastImage.type(\"http://switchstep.com/images/ss_logo.png\")\n=\u003e :png\nFastImage.type(\"/some/local/file.gif\")\n=\u003e :gif\nFile.open(\"/some/local/file.gif\", \"r\") {|io| FastImage.type(io)}\n=\u003e :gif\nFastImage.size(\"http://switchstep.com/images/favicon.ico\", :raise_on_failure=\u003etrue, :timeout=\u003e0.01)\n=\u003e raises FastImage::ImageFetchFailure\nFastImage.size(\"http://switchstep.com/images/favicon.ico\", :raise_on_failure=\u003etrue, :timeout=\u003e2)\n=\u003e [16, 16]\nFastImage.size(\"http://switchstep.com/images/faulty.jpg\", :raise_on_failure=\u003etrue)\n=\u003e raises FastImage::SizeNotFound\nFastImage.new(\"http://switchstep.com/images/ss_logo.png\").content_length\n=\u003e 4679\nFastImage.size(\"http://switchstep.com/images/ss_logo.png\", :http_header =\u003e {'User-Agent' =\u003e 'Fake Browser'})\n=\u003e [300, 300]\nFastImage.new(\"http://switchstep.com/images/ExifOrientation3.jpg\").orientation\n=\u003e 3\nFastImage.size(\"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==\")\n=\u003e [1, 1]\n```\n\n## Installation\n\n### Gem\n\n```sh\ngem install fastimage\n```\n\n### Bundler\n\nAdd fastimage to your Gemfile.\n\n```ruby\ngem 'fastimage'\n```\n\nThen you're off - just use `FastImage.size()` and `FastImage.type()` in your code as in the examples.\n\n## Documentation\n\nhttp://sdsykes.github.io/fastimage/rdoc/FastImage.html\n\n## Maintainer\n\nFastImage is maintained by Stephen Sykes (sdsykes). SamSaffron also helps out from time to time (thanks!).\n\n## Benchmark\n\nIt's way faster than conventional methods for most types of file when fetching over the wire. Compared here by using OpenURI which will fetch the whole file.\n\n```ruby\nrequire 'benchmark'\nrequire 'fastimage'\nrequire 'open-uri'\n\nuri = \"http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg\"\nputs Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}\n[9545, 6623]\n  0.059088   0.067694   0.126782 (  0.808131)\n\nputs Benchmark.measure {p FastImage.size(uri)}\n[9545, 6623]\n  0.006198   0.001563   0.007761 (  0.162021)\n```\n\nThe file is fetched in about 0.8 seconds in this test (the number in brackets is the total time taken), but as FastImage doesn't need to fetch the whole thing, it completes in 0.16s.\n\nYou'll see similar excellent results for the other file types.\n\n```ruby\nrequire 'benchmark'\nrequire 'fastimage'\nrequire 'open-uri'\n\nuri = \"https://upload.wikimedia.org/wikipedia/commons/a/a9/Augustine_Herrman_1670_Map_Virginia_Maryland.tiff\"\nputs Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}\n[12805, 10204]\n  1.332587   2.049915   3.382502 ( 19.925270)\n\nputs Benchmark.measure {p FastImage.size(uri)}\n[12805, 10204]\n  0.004593   0.000924   0.005517 (  0.100592)\n```\n\nSome tiff files however do not have their metadata near the start of the file.\n\n```ruby\nrequire 'benchmark'\nrequire 'fastimage'\nrequire 'open-uri'\n\nuri = \"https://upload.wikimedia.org/wikipedia/commons/1/14/Center-Filled_LIMA.tif\"\nputs Benchmark.measure {URI.open(uri, 'rb') {|fh| p FastImage.size(fh)}}\n[22841, 19404]\n  0.350304   0.321104   0.671408 (  3.053605)\n\nputs Benchmark.measure {p FastImage.size(uri)}\n[22841, 19404]\n  0.163443   0.214301   0.377744 (  2.880414)\n```\n\nNote that if you want a really fast result for this file type, [image_size](https://github.com/toy/image_size?tab=readme-ov-file#experimental-fetch-image-meta-from-http-server) might be useful as it has an optimisation for this (fetching only the needed data range).\n\n```ruby\nrequire 'benchmark'\nrequire 'image_size/uri'\nrequire 'fastimage'\n\nuri = \"https://upload.wikimedia.org/wikipedia/commons/1/14/Center-Filled_LIMA.tif\"\nputs Benchmark.measure {p ImageSize.url(uri).size }\n[22841, 19404]\n  0.008983   0.001311   0.010294 (  0.128986)\n\nputs Benchmark.measure {p FastImage.size(uri)}\n[22841, 19404]\n  0.163443   0.214301   0.377744 (  2.880414)\n```\n\n## Tests\n\nYou'll need to bundle, or `gem install fakeweb` and possibly also  `gem install test-unit` to be able to run the tests.\n\n```sh\nruby test/test.rb\n```\n\n## References\n\n- [Pennysmalls - Find jpeg dimensions fast in pure Ruby, no image library needed](http://pennysmalls.wordpress.com/2008/08/19/find-jpeg-dimensions-fast-in-pure-ruby-no-ima/)\n- [Antti Kupila - Getting JPG dimensions with AS3 without loading the entire file](https://www.anttikupila.com/archive/getting-jpg-dimensions/)\n- [imagesize gem](https://rubygems.org/gems/imagesize)\n- [EXIF Reader](https://github.com/remvee/exifr)\n\n## FastImage in other languages\n\n- [Python by bmuller](https://github.com/bmuller/fastimage)\n- [Swift by kaishin](https://github.com/kaishin/ImageScout)\n- [Go by rubenfonseca](https://github.com/rubenfonseca/fastimage)\n- [PHP by tommoor](https://github.com/tommoor/fastimage)\n- [Node.js by ShogunPanda](https://github.com/ShogunPanda/fastimage)\n- [Objective C by kylehickinson](https://github.com/kylehickinson/FastImage)\n- [Android by qstumn](https://github.com/qstumn/FastImageSize)\n- [Flutter by ky1vstar](https://github.com/ky1vstar/fastimage.dart)\n\n### Also of interest\n- [C++ by xiaozhuai](https://github.com/xiaozhuai/imageinfo)\n- [Rust by xiaozhuai](https://github.com/xiaozhuai/imageinfo-rs)\n\n## Licence\n\nMIT, see file \"MIT-LICENSE\"\n","funding_links":[],"categories":["Ruby","Uncategorized","Image Processing"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdsykes%2Ffastimage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsdsykes%2Ffastimage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsdsykes%2Ffastimage/lists"}