public
Description: Treat an ActiveRecord model as a file attachment, storing its patch, size, content type, etc.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/attachment_fu.git
Click here to lend your support to: attachment_fu and make a donation at www.pledgie.com !
attachment_fu / lib / attachment_fu / pixels / mojo_magick.rb
100644 39 lines (34 sloc) 1.246 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'mojo_magick/image_resources'
require 'mojo_magick/mojo_magick'
 
module AttachmentFu # :nodoc:
  module Pixels
    module MojoMagick
      def with_image(attachment, &block)
        block.call attachment.full_filename
      end
 
      def get_image_size(image)
        size = ::MojoMagick.get_image_size(image)
        [size[:width], size[:height]]
      end
 
      # Performs the actual resizing operation for a thumbnail.
      # Returns a AttachmentFu::Pixels::Image object.
      #
      # Options:
      # - :size - REQUIRED: either an integer, an array of two integers for width and height, or an geometry string.
      # - :to - Final location of the saved image. Defaults to the pixel instance's location.
      #
      def resize_image(image, options = {})
        size = options[:size]
        case size
          when Fixnum then size = [size, size]
          when String then size = get_image_size(image) / size
        end
        destination = options[:to] || image
        AttachmentFu::Pixels::Image.new destination do |img|
          ::MojoMagick.resize(image, destination, :width => size[0], :height => size[1])
          img.width, img.height = get_image_size(destination)
        end
      end
    end
  end
end