Archive for 2012

Fetch Images from Web Url

Wednesday, March 21, 2012 · Posted in ,

You can find images from given web-url by using following code:

For this first you need to write below statements in your .rb file.

require "open-uri"
require "nokogiri"
require "image_size"


now use below code:

  def fetch_images(url)
    return [] if url.nil?
    url_content = open(url)
    url_data = Nokogiri::HTML(url_content)
    images = []
    url_data.xpath("/html/body//img").each do |img|
       src = img["src"]
       full_img_src = src.match(/^(http|https):\/\//) ? src : File.join(url,src)
       if valid_image(full_img_src)
        images << full_img_src
       end
    end
    return images
  rescue
     return 0
  end

  def valid_image(img_url)
    return false if img_url.nil?
    valid_img = false
    img_data = open(img_url, "rb").read
    min_size  = [100,100]
    img_size = ImageSize.new(img_data).size
    if img_size[0] >= min_size[0] and img_size[1] >= min_size[1]
      valid_img = true
    end
    return valid_img
  rescue
    return false
  end

Here I have restricted images to fetch only (100x100) sized images.

Powered by Blogger.