0
@@ -9,9 +9,9 @@ module HasImage
0
class FileTooBigError < StorageError ; end
0
class FileTooSmallError < StorageError ; end
0
class InvalidGeometryError < ProcessorError ; end
0
def included(base) # :nodoc:
0
base.extend(ClassMethods)
0
@@ -68,61 +68,104 @@ module HasImage
0
:image_too_big_message => "The image is too big."
0
# To use HasImage with a Rails model, all you have to do is add a column
0
- # named "has_image_file." For configuration defaults, you might want to take
0
- # a look at the default options specified in HasImage#default_options_for.
0
- # The different setting options are described below.
0
- # * +:resize_to+ - Dimensions to resize to. This should be an aImageMagick {geometry string}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sizes are recommended.
0
- # * +:thumbnails</tt> - A hash of thumbnail names and dimensions. The dimensions should be ImageMagick {geometry strings}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sized are recommended.
0
- # * +:auto_generate_thumbnails+ - Flag to indicate whether to automatically generate thumbnails when the image_data changes (e.g. on create). If you set this to false, your application code needs to take care of generating Thumbnails, e.g. using +#generate_thumbnail+
0
- # * +:delete+ - Flag to indicate if the images should be delete from the storage (e.g. the Filesystem) when the record is destroyed
0
- # * +:min_size</tt> - Minimum file size allowed. It's recommended that you set this size in kilobytes.
0
- # * +:max_size</tt> - Maximum file size allowed. It's recommended that you set this size in megabytes.
0
- # * +:base_path</tt> - Where to install the images. You should probably leave this alone, except for tests.
0
- # * +:path_prefix</tt> - Where to install the images, relative to basepath. You should probably leave this alone.
0
- # * +:convert_to</tt> - An ImageMagick format to convert images to. Recommended formats: JPEG, PNG, GIF.
0
- # * +:output_quality</tt> - Image output quality passed to ImageMagick.
0
- # * +:invalid_image_message</tt> - The message that will be shown when the image data can't be processed.
0
- # * +:image_too_small_message</tt> - The message that will be shown when the image file is too small. You should ideally set this to something that tells the user what the minimum is.
0
- # * +:image_too_big_message</tt> - The message that will be shown when the image file is too big. You should ideally set this to something that tells the user what the maximum is.
0
+ # named "has_image_file." For configuration defaults, you might want to
0
+ # take a look at the default options specified in
0
+ # HasImage#default_options_for. The different setting options are
0
+ # * <tt>:resize_to</tt> - Dimensions to resize to. This should be an ImageMagick {geometry string}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sizes are recommended.
0
+ # * <tt>:thumbnails</tt> - A hash of thumbnail names and dimensions. The dimensions should be ImageMagick {geometry strings}[http://www.imagemagick.org/script/command-line-options.php#resize]. Fixed sized are recommended.
0
+ # * <tt>:auto_generate_thumbnails</tt> - Flag to indicate whether to automatically generate thumbnails when the image_data changes (e.g. on create). If you set this to false, your application code needs to take care of generating thumbnails, e.g. using +#generate_thumbnail+
0
+ # * <tt>:delete</tt> - Flag to indicate if the images should be deleted from the storage (e.g. the filesystem) when the record is destroyed.
0
+ # * <tt>:min_size</tt> - Minimum file size allowed. It's recommended that you set this size in kilobytes.
0
+ # * <tt>:max_size</tt> - Maximum file size allowed. It's recommended that you set this size in megabytes.
0
+ # * <tt>:base_path</tt> - Where to install the images.
0
+ # * <tt>:path_prefix</tt> - Where to install the images, relative to basepath.
0
+ # * <tt>:convert_to</tt> - An ImageMagick format to convert images to.
0
+ # * <tt>:output_quality</tt> - Image output quality passed to ImageMagick.
0
+ # * <tt>:invalid_image_message</tt> - The message that will be shown when the image data can't be processed.
0
+ # * <tt>:image_too_small_message</tt> - The message that will be shown when the image file is too small. You should ideally set this to something that tells the user what the minimum is.
0
+ # * <tt>:image_too_big_message</tt> - The message that will be shown when the image file is too big. You should ideally set this to something that tells the user what the maximum is.
0
# has_image # uses all default options
0
# has_image :resize_to "800x800", :thumbnails => {:square => "150x150"}
0
# has_image :resize_to "100x150", :max_size => 500.kilobytes
0
- # has_image :invalid_image_message => "No se puede procesar la imagen."
0
+ # === Some slightly more advanced examples
0
+ # ==== Localizing HasImage
0
+ # has_image :invalid_image_message => "No se puede procesar la imagen."
0
+ # ==== Using has_image with Capistrano
0
+ # When deploying using Capistrano, you will likely want to keep images
0
+ # under a "system" directory so that newly deployed versions have access
0
+ # has_image :resize_to => "150x150",
0
+ # :base_path => File.join(Rails.root, 'public', 'system')
0
+ # ==== Testing with HasImage:
0
+ # If you want your tests to actually run the image processing, you should
0
+ # make sure your tests write the image files somewhere outside your public
0
+ # directory. Add something like this to your config/environments/test.rb:
0
+ # config.after_initialize do
0
+ # MyClass.has_image_options[:base_path] = File.join(RAILS_ROOT, "tmp")
0
+ # If you want to stub out calls to has_image so that your tests don't do
0
+ # the (slow) image processing, here's an example using Test::Unit and
0
+ # Photo.any_instance.stubs(:image_data=).returns(true)
0
+ # Photo.any_instance.stubs(:install_images).returns(true)
0
+ # Photo.any_instance.stubs(:image_data_valid?).returns(true)
0
def has_image(options = {})
0
options.assert_valid_keys(HasImage.default_options_for(self).keys)
0
options = HasImage.default_options_for(self).merge(options)
0
class_inheritable_accessor :has_image_options
0
write_inheritable_attribute(:has_image_options, options)
0
after_create :install_images
0
after_save :update_images
0
after_destroy :remove_images
0
validate_on_create :image_data_valid?
0
include ModelInstanceMethods
0
extend ModelClassMethods
0
module ModelInstanceMethods
0
# Does the object have an image?
0
!send(has_image_options[:column]).blank?
0
# Sets the uploaded image data. Image data can be an instance of Tempfile,
0
# or an instance of any class than inherits from IO.
0
# aliased as uploaded_data= for compatibility with attachment_fu
0
@@ -137,7 +180,7 @@ module HasImage
0
alias_method :uploaded_data, :image_data
0
# Is the image data a file that ImageMagick can process, and is it within
0
# the allowed minimum and maximum sizes?
0
@@ -150,7 +193,7 @@ module HasImage
0
errors.add_to_base(self.class.has_image_options[:invalid_image_message])
0
# Gets the "web path" for the image, or optionally, its thumbnail.
0
# Aliased as +public_filename+ for compatibility with attachment-Fu
0
def public_path(thumbnail = nil)
0
@@ -163,29 +206,29 @@ module HasImage
0
def absolute_path(thumbnail = nil)
0
storage.filesystem_path_for(self, thumbnail)
0
# Regenerates the thumbails from the main image.
0
def regenerate_thumbnails!
0
storage.generate_thumbnails(has_image_id, send(has_image_options[:column]))
0
alias_method :regenerate_thumbnails, :regenerate_thumbnails! #Backwards compat
0
def generate_thumbnail!(thumb_name)
0
storage.generate_thumbnail(has_image_id, send(has_image_options[:column]), thumb_name)
0
self[:width] || storage.measure(absolute_path, :width)
0
self[:height] || storage.measure(absolute_path, :height)
0
# Deletes the image from the storage.
0
return if send(has_image_options[:column]).blank? || !has_image_options[:delete]
0
@@ -204,7 +247,7 @@ module HasImage
0
# Creates new images and removes the old ones when image_data has been
0
@@ -218,21 +261,13 @@ module HasImage
0
return if !storage.temp_file
0
- def populate_attributes
0
- send("#{has_image_options[:column]}=", storage.install_images(self))
0
- self[:width] = storage.measure(absolute_path, :width) if self.class.column_names.include?('width')
0
- self[:height] = storage.measure(absolute_path, :height) if self.class.column_names.include?('height')
0
- private :populate_attributes
0
# Gets an instance of the underlying storage functionality. See
0
@storage ||= HasImage::Storage.new(has_image_options)
0
# By default, just returns the model's id. Since this id is used to divide
0
# the images up in directories, you can override this to return a related
0
# model's id if you want the images to be grouped differently. For example,
0
@@ -241,7 +276,17 @@ module HasImage
0
+ def populate_attributes
0
+ send("#{has_image_options[:column]}=", storage.install_images(self))
0
+ self[:width] = storage.measure(absolute_path, :width) if self.class.column_names.include?('width')
0
+ self[:height] = storage.measure(absolute_path, :height) if self.class.column_names.include?('height')
0
module ModelClassMethods
0
@@ -251,11 +296,11 @@ module HasImage
0
has_image_options[:thumbnails]
0
def from_partitioned_path(path)
0
find HasImage::Storage.id_from_path(path)