norman / has_image
- Source
- Commits
- Network (5)
- Issues (0)
- Downloads (5)
- Wiki (1)
- Graphs
-
Tree:
59f7224
commit 59f7224fcb6bb6bd8f170dc9cb00d57af04d33db
tree e1ec3a318106a8c5022aab419f44da25cd000c71
parent d7b057604aa0399afd51c89c3199644b860a2aea
tree e1ec3a318106a8c5022aab419f44da25cd000c71
parent d7b057604aa0399afd51c89c3199644b860a2aea
| 85e255d9 » | Norman Clarke | 2008-07-21 | 1 | require 'mini_magick' | |
| 84229d9f » | Norman Clarke | 2008-07-22 | 2 | ||
| 59fc89e6 » | Norman Clarke | 2008-07-20 | 3 | module HasImage | |
| 4 | |||||
| 52be8869 » | Norman Clarke | 2008-07-23 | 5 | # Image processing functionality for the HasImage gem. | |
| 59fc89e6 » | Norman Clarke | 2008-07-20 | 6 | class Processor | |
| 7 | |||||
| 85e255d9 » | Norman Clarke | 2008-07-21 | 8 | attr_accessor :options | |
| ed83fc0d » | Norman Clarke | 2008-07-23 | 9 | ||
| 10 | class << self | ||||
| 34079570 » | Norman Clarke | 2008-07-25 | 11 | ||
| 762c869d » | Norman Clarke | 2008-07-28 | 12 | # "The form of an {extended geometry | |
| 13 | # string}[http://www.imagemagick.org/script/command-line-options.php?#resize] is | ||||
| 14 | # <width>x<height>{+-}<xoffset>{+-}<yoffset>{%}{!}{<}{>}" | ||||
| 15 | def geometry_string_valid?(string) | ||||
| 16 | string =~ /\A[\d]*x[\d]*([+-][0-9][+-][0-9])?[%@!<>^]?\Z/ | ||||
| 17 | end | ||||
| 34079570 » | Norman Clarke | 2008-07-25 | 18 | ||
| ed83fc0d » | Norman Clarke | 2008-07-23 | 19 | # Arg should be either a file, or a path. This runs ImageMagick's | |
| 20 | # "identify" command and looks for an exit status indicating an error. If | ||||
| 21 | # there is no error, then ImageMagick has identified the file as something | ||||
| 22 | # it can work with and it will be converted to the desired output format. | ||||
| 23 | def valid?(arg) | ||||
| 24 | arg.close if arg.respond_to?(:close) && !arg.closed? | ||||
| 25 | silence_stderr do | ||||
| 26 | `identify #{arg.respond_to?(:path) ? arg.path : arg.to_s}` | ||||
| 27 | $? == 0 | ||||
| 28 | end | ||||
| d7b05760 » | gerrit | 2008-10-21 | 29 | end | |
| 30 | |||||
| ed83fc0d » | Norman Clarke | 2008-07-23 | 31 | end | |
| d8172965 » | Norman Clarke | 2008-07-22 | 32 | ||
| 7c1ac7c5 » | Norman Clarke | 2008-07-24 | 33 | # The constuctor should be invoked with the options set by has_image. | |
| 34 | def initialize(options) # :nodoc: | ||||
| d8172965 » | Norman Clarke | 2008-07-22 | 35 | @options = options | |
| 36 | end | ||||
| 59fc89e6 » | Norman Clarke | 2008-07-20 | 37 | ||
| 52be8869 » | Norman Clarke | 2008-07-23 | 38 | # Create the resized image, and transforms it to the desired output | |
| c99267ce » | gerrit | 2008-10-14 | 39 | # format if necessary. | |
| 40 | # | ||||
| 41 | # +size+ should be a valid ImageMagick {geometry string}[http://www.imagemagick.org/script/command-line-options.php#resize]. | ||||
| 42 | # +format+ should be an image format supported by ImageMagick, e.g. "PNG", "JPEG" | ||||
| 43 | # yields the processed Image file as a file-like | ||||
| 44 | def process(file, size=options[:resize_to], format=options[:convert_to]) | ||||
| fd08f541 » | gerrit | 2008-10-13 | 45 | unless size.blank? || Processor.geometry_string_valid?(size) | |
| 39d0b37d » | Norman Clarke | 2008-07-29 | 46 | raise InvalidGeometryError.new('"%s" is not a valid ImageMagick geometry string' % size) | |
| 47 | end | ||||
| c99267ce » | gerrit | 2008-10-14 | 48 | with_image(file) do |image| | |
| 49 | convert_image(image, format) if format | ||||
| 50 | resize_image(image, size) if size | ||||
| 51 | yield IO.read(image.path) if block_given? | ||||
| 52 | image | ||||
| 59fc89e6 » | Norman Clarke | 2008-07-20 | 53 | end | |
| ed83fc0d » | Norman Clarke | 2008-07-23 | 54 | end | |
| c99267ce » | gerrit | 2008-10-14 | 55 | alias_method :resize, :process #Backwards-compat | |
| ed83fc0d » | Norman Clarke | 2008-07-23 | 56 | ||
| 59f7224f » | gerrit | 2008-10-22 | 57 | # Gets the given +dimension+ (width/height) from the image file at +path+ | |
| 58 | def measure(path, dimension) | ||||
| 59 | MiniMagick::Image.from_file(path)[dimension.to_sym] | ||||
| 60 | end | ||||
| 61 | |||||
| c99267ce » | gerrit | 2008-10-14 | 62 | private | |
| 63 | # operate on the image with MiniMagick | ||||
| 64 | # yields a MiniMagick::Image object | ||||
| 65 | def with_image(file) | ||||
| 66 | path = file.respond_to?(:path) ? file.path : file | ||||
| 67 | file.close if file.respond_to?(:close) && !file.closed? | ||||
| 68 | silence_stderr do | ||||
| ab753be7 » | gerrit | 2008-10-20 | 69 | begin | |
| 70 | image = MiniMagick::Image.from_file(path) | ||||
| 71 | yield image | ||||
| 72 | rescue MiniMagick::MiniMagickError | ||||
| 73 | raise ProcessorError.new("#{path} doesn't look like an image file.") | ||||
| 74 | ensure | ||||
| 75 | image.tempfile.close! if defined?(image) && image | ||||
| 76 | end | ||||
| c99267ce » | gerrit | 2008-10-14 | 77 | end | |
| 78 | end | ||||
| 79 | |||||
| 80 | # +image+ should be a MiniMagick::Image and +size+ a Geometry String | ||||
| 52be8869 » | Norman Clarke | 2008-07-23 | 81 | # Image resizing is placed in a separate method for easy monkey-patching. | |
| 82 | # This is intended to be invoked from resize, rather than directly. | ||||
| 7c1ac7c5 » | Norman Clarke | 2008-07-24 | 83 | # By default, the following ImageMagick functionality is invoked: | |
| 84 | # * auto-orient[http://www.imagemagick.org/script/command-line-options.php#auto-orient] | ||||
| 85 | # * strip[http://www.imagemagick.org/script/command-line-options.php#strip] | ||||
| 86 | # * resize[http://www.imagemagick.org/script/command-line-options.php#resize] | ||||
| 87 | # * gravity[http://www.imagemagick.org/script/command-line-options.php#gravity] | ||||
| 88 | # * extent[http://www.imagemagick.org/script/command-line-options.php#extent] | ||||
| 89 | # * quality[http://www.imagemagick.org/script/command-line-options.php#quality] | ||||
| c99267ce » | gerrit | 2008-10-14 | 90 | def resize_image(image, size) | |
| 91 | image.combine_options do |commands| | ||||
| 52be8869 » | Norman Clarke | 2008-07-23 | 92 | commands.send("auto-orient".to_sym) | |
| 93 | commands.strip | ||||
| 762c869d » | Norman Clarke | 2008-07-28 | 94 | # Fixed-dimension images | |
| 95 | if size =~ /\A[\d]*x[\d]*!?\Z/ | ||||
| 96 | commands.resize "#{size}^" | ||||
| 97 | commands.gravity "center" | ||||
| 98 | commands.extent size | ||||
| 99 | # Non-fixed-dimension images | ||||
| 100 | else | ||||
| 101 | commands.resize "#{size}" | ||||
| 102 | end | ||||
| 52be8869 » | Norman Clarke | 2008-07-23 | 103 | commands.quality options[:output_quality] | |
| 104 | end | ||||
| 105 | end | ||||
| 106 | |||||
| c99267ce » | gerrit | 2008-10-14 | 107 | def convert_image(image, format=options[:convert_to]) | |
| 108 | image.format(format) unless image[:format] == format | ||||
| 59fc89e6 » | Norman Clarke | 2008-07-20 | 109 | end | |
| 110 | |||||
| 111 | end | ||||
| 112 | |||||
| 113 | end | ||||
