Created
August 23, 2010 13:17
-
-
Save Oshuma/545457 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mime/types' | |
require 'RMagick' | |
require 'yaml' | |
module NerdNode | |
module Photo | |
class Collection | |
# A list of valid MIME types. | |
TYPES = %w{ | |
image/bmp | |
image/gif | |
image/jpeg | |
image/png | |
} | |
# Reads in a hash of photo collections in the format of: | |
# { | |
# 'Collection Name' => { | |
# 'photos' => '/path/to/photos', | |
# 'thumbs' => '/path/to/thumbs', | |
# } | |
# } | |
def self.from_config(config) | |
collections = [] | |
config.each do |name, options| | |
collections << new(name, options['photos'], options['thumbs']) | |
end | |
collections | |
end | |
attr_reader :name, :path, :thumb_path | |
def initialize(name, path, thumb_path = nil) | |
raise "Directory not found: #{path}" unless File.exists?(path) | |
@name = name | |
@path = path | |
@photos = {} | |
@thumb_path = thumb_path || File.expand_path("#{File.join(@path, 'thumbs')}") | |
read_photos | |
end | |
def photos | |
@photos.keys | |
end | |
def thumbnails | |
@photos.values | |
end | |
alias_method :thumbs, :thumbnails | |
# Returns an array with the base photo path. | |
def photo_paths | |
photos.map { |photo| remove_from_path(photo, /#{@path}\//) } | |
end | |
# Returns an array with the base thumbnail photo paths. | |
def thumbnail_paths | |
thumbnails.map { |thumb| remove_from_path(thumb, /#{@thumb_path}\//) if thumb } | |
end | |
alias_method :thumb_paths, :thumbnail_paths | |
def generate_thumbnails!(*args) | |
unless File.exists?(@thumb_path) | |
raise "Output directory not found: #{@thumb_path}" | |
end | |
@photos.each do |photo, thumb| | |
thumb = thumbnail_path(photo) unless thumb | |
image = Magick::Image.read(photo).first | |
image.thumbnail!(*args) | |
image.write(thumb) | |
@photos[photo] = thumb | |
end | |
end | |
# Returns a URL (semi) safe string of the +name+. | |
def url | |
url = name.dup | |
url.gsub!(/\s/, '_') | |
url.downcase! | |
url | |
end | |
# Returns <tt>@photos</tt>, but with relative paths. | |
def photo_urls | |
urls = {} | |
@photos.each do |photo_full, thumb_full| | |
photo = remove_from_path(photo_full, /#{@path}\//) | |
thumb = thumb_full ? remove_from_path(thumb_full, /#{@thumb_path}\//) : nil | |
urls[photo] = thumb | |
end | |
urls | |
end | |
private | |
def read_photos | |
files = Dir["#{@path}/**/*"].select do |file| | |
TYPES.include?(MIME::Types.type_for(file).to_s) | |
end | |
files.reject! { |file| File.directory?(file) } | |
files.flatten! | |
files.each do |file| | |
@photos[file] = File.exists?(thumbnail_path(file)) ? thumbnail_path(file) : nil | |
end | |
end | |
def remove_from_path(path_string, matcher) | |
path_string.gsub(matcher, '') | |
end | |
# Returns the string path of the given <tt>photo</tt>'s thumbnail. | |
def thumbnail_path(photo) | |
File.expand_path(File.join(@thumb_path, File.basename(photo))) | |
end | |
end # Collection | |
end # Photo | |
end # NerdNode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment