Sha256: 51742753f24d7fe46faadc6cb200b3dee29475de5bc7ff2a29f8a12c7e40351e

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require 'tempfile'

module Dragonfly
  module RMagickUtils

    include Loggable

    private

    # Requires the extended object to respond to 'use_filesystem'
    def rmagick_image(temp_object, &block)
      imagelist = use_filesystem ? Magick::Image.read(temp_object.path) : Magick::Image.from_blob(temp_object.data)
      image = imagelist.first
      result = block.call(image)
      case result
      when Magick::Image, Magick::ImageList
        content = use_filesystem ? write_to_tempfile(result) : result.to_blob
        result.destroy!
      else
        content = result
      end
      image.destroy!
      content
    rescue Magick::ImageMagickError => e
      log.warn("Unable to handle content in #{self.class} - got:\n#{e}")
      throw :unable_to_handle
    end

    def ping_rmagick_image(temp_object, &block)
      imagelist = use_filesystem ? Magick::Image.ping(temp_object.path) : Magick::Image.from_blob(temp_object.data)
      image = imagelist.first
      result = block.call(image)
      image.destroy!
      result
    rescue Magick::ImageMagickError => e
      log.warn("Unable to handle content in #{self.class} - got:\n#{e}")
      throw :unable_to_handle
    end

    def write_to_tempfile(rmagick_image)
      tempfile = Tempfile.new('dragonfly')
      tempfile.close
      rmagick_image.write(tempfile.path)
      tempfile
    end

  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
dragonfly-0.8.6 lib/dragonfly/r_magick_utils.rb
dragonfly-0.8.5 lib/dragonfly/r_magick_utils.rb
dragonfly-0.8.4 lib/dragonfly/r_magick_utils.rb
fog-dragonfly-0.8.2 lib/dragonfly/r_magick_utils.rb
dragonfly-0.8.2 lib/dragonfly/r_magick_utils.rb
fog-dragonfly-0.8.1 lib/dragonfly/r_magick_utils.rb
dragonfly-0.8.1 lib/dragonfly/r_magick_utils.rb
dragonfly-0.8.0 lib/dragonfly/r_magick_utils.rb
dragonfly-0.7.7 lib/dragonfly/r_magick_utils.rb
dragonfly-0.7.6 lib/dragonfly/r_magick_utils.rb