This gem allows the simple searching for and saving of images to your app. Various photo-searching API's will provide more consistent results, but this is a quick way to find images matching a query. Results can be restricted by photo dimensions and batch-saved to your directory. This gem was extracted from an app to fetch movie posters for a movie suggestion site, which dynamically creates the content on each page load.
Add this line to your application's Gemfile:
gem 'fotofetch'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fotofetch
- Initiate an instance of Fotofetch:
@fetcher = Fotofetch::Fetch.new
- To fetch an image of a Tesla Model S:
link = @fetcher.fetch_links("tesla model s")
- To save the link to your file directory (links must be in an array, so call .values):
@fetcher.save_images(link.values, './')
The 'save_images' method takes two arguments: link(s) and the file path for where to save. File names are gerated from the previous query, so if you search for 2 images of 'red_ball', they will be saved as 'red_ball_0.jpg' and 'red_ball_1.jpg'.
Arguments for the 'fetch_links' method are: search value (required), number of links returned (optional), and two dimension restrictions arguments (optional: width, height).
If a dimension argument is positive, it will look for pictures larger than that, and if the number is negative, results will be restricted to those smaller than that. Note that adding dimensions restrictions to your query will slow it down.
- To find a 1 small photo of Jupiter less than 500px x 500px:
@fetcher.fetch_links("jupiter", 1, -500, -500)
To find 3 large photos of Jupiter:
links = @fetcher.fetch_links("jupiter", 3, 1500, 1500)
And to save those 3 large photos:
@fetcher.save_images(links.values, './')
If a small or large enough image is not found, dimension restrictions will be disregarded.
Bug reports and pull requests are welcome on GitHub at https://github.com/steveoscar/fotofetch.
The gem is available as open source under the terms of the MIT License.