Skip to content

Commit

Permalink
Add :read option to manipulate!
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanzeeb Khalili committed Jun 7, 2012
1 parent 9793c71 commit e887d95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 7 additions & 5 deletions lib/carrierwave/processing/rmagick.rb
Expand Up @@ -277,7 +277,9 @@ def resize_and_pad(width, height, background=:transparent, gravity=::Magick::Cen
#
def manipulate!(options={}, &block)
cache_stored_file! if !cached?
image = ::Magick::Image.read(current_path)

read_block = create_info_block(options[:read])
image = ::Magick::Image.read(current_path, &read_block)

frames = if image.size > 1
list = ::Magick::ImageList.new
Expand All @@ -296,7 +298,7 @@ def manipulate!(options={}, &block)
frame
end

write_block = create_write_block(options[:write])
write_block = create_info_block(options[:write])
if options[:format]
frames.write("#{options[:format]}:#{current_path}", &write_block)
else
Expand All @@ -309,9 +311,9 @@ def manipulate!(options={}, &block)

private

def create_write_block(write_options)
return nil unless write_options
assignments = write_options.map { |k, v| "self.#{k} = #{v}" }
def create_info_block(options)
return nil unless options
assignments = options.map { |k, v| "self.#{k} = #{v}" }
code = "lambda { |img| " + assignments.join(";") + "}"
eval code
end
Expand Down
15 changes: 11 additions & 4 deletions spec/processing/rmagick_spec.rb
Expand Up @@ -80,13 +80,20 @@
::Magick::Image::Info.any_instance.should_receive(:quality=).with(50)
::Magick::Image::Info.any_instance.should_receive(:depth=).with(8)

@instance.manipulate! do |img, index, options|
options[:write] = {
@instance.manipulate! :write => {
:quality => 50,
:depth => 8
}
img
end
end

it 'should support passing read options to RMagick' do
::Magick::Image::Info.any_instance.should_receive(:density=).with(10)
::Magick::Image::Info.any_instance.should_receive(:size=).with("200x200")

@instance.manipulate! :read => {
:density => 10,
:size => %{"200x200"}
}
end
end

Expand Down

0 comments on commit e887d95

Please sign in to comment.