Class: ImageResizer::Processor
- Inherits:
-
Object
- Object
- ImageResizer::Processor
- Includes:
- Configurable, Utils
- Defined in:
- lib/image_resizer/processor.rb
Constant Summary collapse
- GRAVITIES =
{ 'nw' => 'NorthWest', 'n' => 'North', 'ne' => 'NorthEast', 'w' => 'West', 'c' => 'Center', 'e' => 'East', 'sw' => 'SouthWest', 's' => 'South', 'se' => 'SouthEast' }
- RESIZE_GEOMETRY =
Geometry string patterns
/^\d*x\d*[><%^!]?$|^\d+@$/
- CROPPED_RESIZE_GEOMETRY =
e.g. β20x50#neβ
/^(\d+)x(\d+)#(\w{1,2})?$/
- CROP_GEOMETRY =
e.g. β30x30+10+10β
/^(\d+)x(\d+)([+-]\d+)?([+-]\d+)?(\w{1,2})?$/
- THUMB_GEOMETRY =
Regexp.union RESIZE_GEOMETRY, CROPPED_RESIZE_GEOMETRY, CROP_GEOMETRY
Instance Attribute Summary
Attributes included from Loggable
Instance Method Summary collapse
- #_resize(temp_object, geometry, format = nil) ⇒ Object
- #auto_orient(temp_object) ⇒ Object
- #convert(temp_object, args = '', format = nil) ⇒ Object
- #crop(temp_object, opts = {}) ⇒ Object
- #crop_to_frame_and_resize(temp_object, options) ⇒ Object
- #flip(temp_object) ⇒ Object
- #flop(temp_object) ⇒ Object
- #generate_icon(temp_object, options = {}) ⇒ Object
- #greyscale(temp_object) ⇒ Object (also: #grayscale)
- #resize(temp_object, options = {}) ⇒ Object
- #resize_and_crop(temp_object, opts = {}) ⇒ Object
- #resize_and_crop_around_point(temp_object, options) ⇒ Object
- #rotate(temp_object, amount, opts = {}) ⇒ Object
- #strip(temp_object) ⇒ Object
- #thumb(temp_object, geometry) ⇒ Object
Methods included from Configurable
Methods included from Loggable
Instance Method Details
#_resize(temp_object, geometry, format = nil) ⇒ Object
48 49 50 |
# File 'lib/image_resizer/processor.rb', line 48 def _resize(temp_object, geometry, format=nil) convert(temp_object, "-resize '#{geometry}'", format) end |
#auto_orient(temp_object) ⇒ Object
52 53 54 |
# File 'lib/image_resizer/processor.rb', line 52 def auto_orient(temp_object) convert(temp_object, "-auto-orient") end |
#convert(temp_object, args = '', format = nil) ⇒ Object
220 221 222 |
# File 'lib/image_resizer/processor.rb', line 220 def convert(temp_object, args='', format=nil) super end |
#crop(temp_object, opts = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/image_resizer/processor.rb', line 56 def crop(temp_object, opts={}) width = opts[:width] height = opts[:height] gravity = GRAVITIES[opts[:gravity]] x = "#{opts[:x] || 0}" x = '+' + x unless x[/^[+-]/] y = "#{opts[:y] || 0}" y = '+' + y unless y[/^[+-]/] repage = opts[:repage] == false ? '' : '+repage' resize = opts[:resize] ? "-resize #{opts[:resize]} " : '' gravity = gravity ? "-gravity #{gravity} " : '' convert(temp_object, "#{resize}#{gravity}-crop #{width}x#{height}#{x}#{y} #{repage}", opts[:format]) end |
#crop_to_frame_and_resize(temp_object, options) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/image_resizer/processor.rb', line 128 def crop_to_frame_and_resize(temp_object, ) analyzer = ImageResizer::Analyzer.new desired_width = [:width].to_i desired_height = [:height].to_i upper_left_x_percent = [:upper_left].first upper_left_y_percent = [:upper_left].last lower_right_x_percent = [:lower_right].first lower_right_y_percent = [:lower_right].last original_width = analyzer.width(temp_object) original_height = analyzer.height(temp_object) upper_left_x = (original_width * upper_left_x_percent).round upper_left_y = (original_height * upper_left_y_percent).round frame_width = (original_width * (lower_right_x_percent - upper_left_x_percent)).round frame_height = (original_height * (lower_right_y_percent - upper_left_y_percent)).round if desired_width == 0 && frame_height > 0 ratio = frame_width.to_f / frame_height desired_width = (desired_height * ratio).round end if desired_height == 0 && frame_width > 0 ratio = frame_height.to_f / frame_width desired_height = (desired_width * ratio).round end convert(temp_object, "-crop #{frame_width}x#{frame_height}+#{upper_left_x}+#{upper_left_y} -resize #{desired_width}x#{desired_height} +repage", [:format]) end |
#flip(temp_object) ⇒ Object
162 163 164 |
# File 'lib/image_resizer/processor.rb', line 162 def flip(temp_object) convert(temp_object, "-flip") end |
#flop(temp_object) ⇒ Object
166 167 168 |
# File 'lib/image_resizer/processor.rb', line 166 def flop(temp_object) convert(temp_object, "-flop") end |
#generate_icon(temp_object, options = {}) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/image_resizer/processor.rb', line 224 def generate_icon(temp_object, ={}) max_resolution = [[:max_resolution] || 256, 256].min largest_png = convert(temp_object, "-resize #{max_resolution}x#{max_resolution}! -transparent white", :png) formats = [] current = 16 while current < max_resolution formats << convert(largest_png, "-resize #{current}x#{current}! -transparent white") current *= 2 end formats << largest_png convert(formats, '', :ico) end |
#greyscale(temp_object) ⇒ Object Also known as: grayscale
170 171 172 |
# File 'lib/image_resizer/processor.rb', line 170 def greyscale(temp_object) convert(temp_object, "-colorspace Gray") end |
#resize(temp_object, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/image_resizer/processor.rb', line 26 def resize(temp_object, ={}) width = [:width].to_i height = [:height].to_i if height == 0 && width == 0 temp_object.file elsif height == 0 _resize(temp_object, "#{width}x", [:format]) elsif width == 0 _resize(temp_object, "x#{height}", [:format]) else if [:crop_from_top_if_portrait] analyzer = ImageResizer::Analyzer.new center_of_gravity = analyzer.aspect_ratio(temp_object) >= 1 ? 'c' : 'n' else center_of_gravity = 'c' end resize_and_crop(temp_object, :width => width.to_i, :height => height.to_i, :gravity => center_of_gravity, :format => [:format]) end end |
#resize_and_crop(temp_object, opts = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/image_resizer/processor.rb', line 175 def resize_and_crop(temp_object, opts={}) if !opts[:width] && !opts[:height] if opts[:format] return convert(temp_object, '', opts[:format]) else return temp_object end elsif !opts[:width] || !opts[:height] attrs = identify(temp_object) opts[:width] ||= attrs[:width] opts[:height] ||= attrs[:height] end opts[:gravity] ||= 'c' opts[:resize] = "#{opts[:width]}x#{opts[:height]}^^" crop(temp_object, opts) end |
#resize_and_crop_around_point(temp_object, options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/image_resizer/processor.rb', line 72 def resize_and_crop_around_point(temp_object, ) analyzer = ImageResizer::Analyzer.new desired_width = [:width].to_i desired_height = [:height].to_i desired_ratio = desired_height > 0 ? desired_width.to_f / desired_height : 0 original_width = analyzer.width(temp_object) original_height = analyzer.height(temp_object) original_ratio = original_width.to_f / original_height if desired_ratio > original_ratio width = original_width height = width / desired_ratio else height = original_height width = height * desired_ratio end focus_x = [:point][0] * original_width focus_y = [:point][1] * original_height half_width = width * 0.5 half_height = height * 0.5 upper_left_x = [focus_x - half_width, 0].max upper_left_y = [focus_y - half_height, 0].max lower_right_x = upper_left_x + width lower_right_y = upper_left_y + height x_offset = [lower_right_x - original_width, 0].max y_offset = [lower_right_y - original_height, 0].max upper_left_x -= x_offset upper_left_y -= y_offset lower_right_x -= x_offset lower_right_y -= y_offset upper_left_x_percent = upper_left_x / original_width upper_left_y_percent = upper_left_y / original_height lower_right_x_percent = lower_right_x / original_width lower_right_y_percent = lower_right_y / original_height crop_to_frame_and_resize(temp_object, :upper_left => [upper_left_x_percent, upper_left_y_percent], :lower_right => [lower_right_x_percent, lower_right_y_percent], :width => desired_width, :height => desired_height, :format => [:format] ) end |
#rotate(temp_object, amount, opts = {}) ⇒ Object
194 195 196 |
# File 'lib/image_resizer/processor.rb', line 194 def rotate(temp_object, amount, opts={}) convert(temp_object, "-rotate '#{amount}#{opts[:qualifier]}'") end |
#strip(temp_object) ⇒ Object
198 199 200 |
# File 'lib/image_resizer/processor.rb', line 198 def strip(temp_object) convert(temp_object, "-strip") end |
#thumb(temp_object, geometry) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/image_resizer/processor.rb', line 202 def thumb(temp_object, geometry) case geometry when RESIZE_GEOMETRY resize(temp_object, geometry) when CROPPED_RESIZE_GEOMETRY resize_and_crop(temp_object, :width => $1, :height => $2, :gravity => $3) when CROP_GEOMETRY crop(temp_object, :width => $1, :height => $2, :x => $3, :y => $4, :gravity => $5 ) else raise ArgumentError, "Didn't recognise the geometry string #{geometry}" end end |