public
Description: Rails plugin for uploading images as resources, with support for resizing, text stamping, and other special effects.
Homepage: http://wiki.github.com/Squeegy/fleximage
Clone URL: git://github.com/Squeegy/fleximage.git
Added default_image option that allows you to use a default image defined by a 
size and optional fill color.  So instead of referencing a static image, you can 
use a '320x240' solid red image instead.
Squeegy (author)
Sat May 31 13:28:50 -0700 2008
commit  fb328f2b8c710ca5bd2f84d01cd11ec81af69a55
tree    37f9f1a3ba0aae9ce99c5e289b2867e3ed95c7b8
parent  888e17dd88d33f6229c76b4ae99394cf7a5d33d2
...
38
39
40
 
 
 
41
42
43
...
51
52
53
54
 
55
 
56
57
58
...
118
119
120
 
 
 
121
122
123
...
444
445
446
447
 
448
449
450
 
 
 
 
 
 
 
 
 
451
452
453
...
38
39
40
41
42
43
44
45
46
...
54
55
56
 
57
58
59
60
61
62
...
122
123
124
125
126
127
128
129
130
...
451
452
453
 
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
0
@@ -38,6 +38,9 @@ module Fleximage
0
     #   very large in filesize.
0
     # * +default_image_path+: (String, nil default) If no image is present for this record, the image at this path will be
0
     #   used instead.  Useful for a placeholder graphic for new content that may not have an image just yet.
0
+    # * +default_image+: A hash which defines an empty starting image.  This hash look like: <tt>:size => '123x456',
0
+    #   :color => :transparent</tt>, where <tt>:size</tt> defines the dimensions of the default image, and <tt>:color</tt>
0
+    #   defines the fill.  <tt>:color</tt> can be a named color as a string ('red'), :transparent, or a Magick::Pixel object.
0
     # * +preprocess_image+: (Block, no default) Call this class method just like you would call +operate+ in a view.
0
     #   The image transoformation in the provided block will be run on every uploaded image before its saved as the 
0
     #   master image.
0
@@ -51,8 +54,9 @@ module Fleximage
0
     #       image_storage_format      :png
0
     #       require_image             true
0
     #       missing_image_message     'is required'
0
-    #       invalid_image_message     'was not a readable image'
0
+    #       invalid_image_message     'was not a readable image'\
0
     #       default_image_path        'public/images/no_photo_yet.png'
0
+    #       default_image             nil
0
     #       output_image_jpg_quality  85
0
     #       
0
     #       preprocess_image do |image|
0
@@ -118,6 +122,9 @@ module Fleximage
0
         # Set a default image to use when no image has been assigned to this record
0
         dsl_accessor :default_image_path
0
         
0
+        # Set a default image based on a a size and fill
0
+        dsl_accessor :default_image
0
+        
0
         # A block that processes an image before it gets saved as the master image of a record.
0
         # Can be helpful to resize potentially huge images to something more manageable. Set via
0
         # the "preprocess_image { |image| ... }" class method.
0
@@ -444,10 +451,19 @@ module Fleximage
0
         
0
         # Load the default image, or raise an expection
0
         def master_image_not_found
0
-          # Load the default image
0
+          # Load the default image from a path
0
           if self.class.default_image_path
0
             @output_image = Magick::Image.read("#{RAILS_ROOT}/#{self.class.default_image_path}").first
0
           
0
+          # Or create a default image
0
+          elsif self.class.default_image
0
+            x, y = Fleximage::Operator::Base.size_to_xy(self.class.default_image[:size])
0
+            color = self.class.default_image[:color]
0
+            
0
+            @output_image = Magick::Image.new(x, y) do
0
+              self.background_color = color if color && color != :transparent
0
+            end
0
+          
0
           # No default, not master image, so raise exception
0
           else
0
             message = "Master image was not found for this record"
...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 
 
66
67
68
...
76
77
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
80
81
...
52
53
54
 
 
 
 
 
 
 
 
 
 
 
55
56
57
58
59
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
0
@@ -52,17 +52,8 @@ module Fleximage
0
       # - SUPPORT METHODS
0
       # ---
0
       
0
-      # Converts a size object to an [x,y] array.  Acceptible formats are:
0
-      # 
0
-      # * 10
0
-      # * "10"
0
-      # * "10x20"
0
-      # * [10, 20]
0
-      #
0
-      # Usage:
0
-      #
0
-      #   x, y = size_to_xy("10x20")
0
-      def size_to_xy(size)
0
+      # Allows access to size conversion globally.  See size_to_xy for a more detailed explanation
0
+      def self.size_to_xy(size)
0
         case          
0
         when size.is_a?(Array) && size.size == 2  # [320, 240]
0
           size
0
@@ -76,6 +67,20 @@ module Fleximage
0
         end
0
       end
0
       
0
+      # Converts a size object to an [x,y] array.  Acceptible formats are:
0
+      # 
0
+      # * 10
0
+      # * "10"
0
+      # * "10x20"
0
+      # * [10, 20]
0
+      #
0
+      # Usage:
0
+      #
0
+      #   x, y = size_to_xy("10x20")
0
+      def size_to_xy(size)
0
+        self.class.size_to_xy size
0
+      end
0
+      
0
       # Scale the image, respecting aspect ratio.  
0
       # Operation will happen in the main <tt>@image</tt> unless you supply the +img+ argument
0
       # to operate on instead.
...
41
42
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0
@@ -41,4 +41,20 @@ class Test::Unit::TestCase #:nodoc:
0
       
0
     end
0
   end
0
+  
0
+  def color_at(image, coords)
0
+    image.load_image.pixel_color(*coords)
0
+  end
0
+  
0
+  def to_color(rgb)
0
+    Magick::Pixel.new(*rgb)
0
+  end
0
+  
0
+  def assert_color(expected, coords, image)
0
+    coords    = coords.split('x').collect(&:to_i)
0
+    expected  = to_color(expected)
0
+    actual    = color_at(image, coords)
0
+    
0
+    assert_equal(expected, actual, "Wrong color at (#{coords.join(',')}).  Expected #{expected}, Got #{actual}")
0
+  end
0
 end

Comments