Module: Imlib2Processor
- Defined in:
- lib/dimension/processors/imlib2.rb
Instance Method Summary collapse
- #close ⇒ Object
- #crop(width, height, x, y, gravity) ⇒ Object
- #crop_scaled(x, y, new_w, new_h) ⇒ Object
- #format ⇒ Object
- #geometry ⇒ Object
- #get_center_offset(w, h) ⇒ Object
- #get_new_geometry ⇒ Object
- #get_offset(w, h, gravity = 'c') ⇒ Object
- #get_resize_geometry(w, h, to_longer = true) ⇒ Object
- #image ⇒ Object
- #image_data ⇒ Object
- #resize(w, h) ⇒ Object
-
#resize_and_crop(w, h, gravity) ⇒ Object
def resize_and_fill(w, h) new_w, new_h = get_resize_geometry(w, h, false) x = (w.to_i - new_w) y = (h.to_i - new_h) image.crop_scaled!(x*-1, y*-1, image.w + x*2, image.h + y*2, w.to_i, h.to_i) end.
- #save! ⇒ Object
- #save_as(new_file_path) ⇒ Object
Instance Method Details
#close ⇒ Object
35 36 37 38 39 |
# File 'lib/dimension/processors/imlib2.rb', line 35 def close log "Closing image." FileUtils.rm(@temp_file) if @temp_file image.delete!(true) # free image, and de-cache it too end |
#crop(width, height, x, y, gravity) ⇒ Object
77 78 79 80 |
# File 'lib/dimension/processors/imlib2.rb', line 77 def crop(width, height, x, y, gravity) rect = [(x || 0).to_i, (y || 0).to_i, width.to_i, height.to_i] image.crop!(rect) end |
#crop_scaled(x, y, new_w, new_h) ⇒ Object
82 83 84 85 |
# File 'lib/dimension/processors/imlib2.rb', line 82 def crop_scaled(x, y, new_w, new_h) log "Resizing #{image.w}x#{image.h} to #{new_w}x#{new_h}. Offset at #{x},#{y}" image.crop_scaled!(x, y, image.w, image.h, new_w.to_i, new_h.to_i) end |
#format ⇒ Object
13 14 15 16 17 |
# File 'lib/dimension/processors/imlib2.rb', line 13 def format image.format # .gsub('jpeg', 'jpg') rescue ArgumentError File.extname(file).sub('.', '') end |
#geometry ⇒ Object
9 10 11 |
# File 'lib/dimension/processors/imlib2.rb', line 9 def geometry [image.w, image.h] end |
#get_center_offset(w, h) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dimension/processors/imlib2.rb', line 95 def get_center_offset(w, h) w_diff = image.w - w.to_i h_diff = image.h - h.to_i log "Width diff: #{w_diff}" log "Height diff: #{h_diff}" if w_diff == h_diff return [0, 0] elsif w_diff > h_diff return [w_diff.to_f/2, 0] else return [0, h_diff.to_f/2] end end |
#get_new_geometry ⇒ Object
41 42 43 |
# File 'lib/dimension/processors/imlib2.rb', line 41 def get_new_geometry geometry end |
#get_offset(w, h, gravity = 'c') ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/dimension/processors/imlib2.rb', line 87 def get_offset(w, h, gravity = 'c') if gravity.nil? or gravity == '' or gravity == 'c' return get_center_offset(w, h) else raise 'Not implemented' end end |
#get_resize_geometry(w, h, to_longer = true) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dimension/processors/imlib2.rb', line 110 def get_resize_geometry(w, h, to_longer = true) if to_longer or h.nil? if h.nil? || image.w < image.h new_h = ((w.to_f / image.w) * image.h).round return w.to_i, new_h else new_w = ((h.to_f / image.h) * image.w).round return new_w, h.to_i end else if w && image.w >= image.h new_h = ((w.to_f / image.w) * image.h).round return w.to_i, new_h else new_w = ((h.to_f / image.h) * image.w).round return new_w, h.to_i end end end |
#image ⇒ Object
5 6 7 |
# File 'lib/dimension/processors/imlib2.rb', line 5 def image @image ||= Imlib2::Image.load(@file) end |
#image_data ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/dimension/processors/imlib2.rb', line 19 def image_data unless @temp_file @temp_file = "/tmp/#{$$}.#{File.basename(file)}" save_as(@temp_file) end IO.read(@temp_file) end |
#resize(w, h) ⇒ Object
pablotron.org/software/imlib2-ruby/doc/
iw, ih = old_image.width, old_image.height
new_w, new_h = iw - 20, ih - 20
values = [10, 10, iw - 10, iw - 10, new_w, new_h]
new_image = old_image.crop_scaled values
52 53 54 55 |
# File 'lib/dimension/processors/imlib2.rb', line 52 def resize(w, h) new_w, new_h = get_resize_geometry(w, h, false) crop_scaled(0, 0, new_w, new_h) end |
#resize_and_crop(w, h, gravity) ⇒ Object
def resize_and_fill(w, h)
new_w, new_h = get_resize_geometry(w, h, false)
x = (w.to_i - new_w)
y = (h.to_i - new_h)
image.crop_scaled!(x*-1, y*-1, image.w + x*2, image.h + y*2, w.to_i, h.to_i)
end
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dimension/processors/imlib2.rb', line 64 def resize_and_crop(w, h, gravity) new_w, new_h = get_resize_geometry(w, h, true) crop_scaled(0, 0, new_w, new_h) # crop_scaled(0, 0, w, h) offset = get_offset(w, h, gravity) # original_height = image.h # original_width = image.w image.crop!(offset[0], offset[1], w.to_i, h.to_i) # crop_scaled(offset[0], offset[1], new_w, new_h) end |
#save! ⇒ Object
31 32 33 |
# File 'lib/dimension/processors/imlib2.rb', line 31 def save! image.save(file) end |
#save_as(new_file_path) ⇒ Object
27 28 29 |
# File 'lib/dimension/processors/imlib2.rb', line 27 def save_as(new_file_path) image.save(new_file_path) end |