Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created May 3, 2012 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/2585739 to your computer and use it in GitHub Desktop.
Save peterhellberg/2585739 to your computer and use it in GitHub Desktop.
Check image dimensions using the Dimensions gem.
require 'json'
class ImageDimensions < Sinatra::Base
get "/" do
'API: GET /[ur_product_id].json'
end
get /(\d{6})\.json/ do |ur_product_id|
content_type :json
data = { exists: false }
['jpg', 'png'].each do |e|
p = path(ur_product_id, e)
data = {
exists: true, ext: e, url: url(p),
width: Dimensions.width(p),
height: Dimensions.height(p)
} if File.exists? p
end
JSON.dump(data)
end
def root_path
"/var/www/apps/ur-image-manager/shared/system/data"
end
def path(ur_product_id, ext = 'jpg')
"#{root_path}/#{ur_product_id}/original/1.#{ext}"
end
def url(path)
path.gsub "#{root_path}", 'http://bilduppladdning-data.ur.se'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment