Class: FlickrCollage::Image
- Inherits:
-
Object
- Object
- FlickrCollage::Image
- Includes:
- Methadone::CLILogging
- Defined in:
- lib/FlickrCollage.rb
Overview
Class for Image. Create collage and crop images
Constant Summary
- IMG_WIDTH =
800
- IMG_HEIGHT =
600
Instance Attribute Summary collapse
-
#clear_tmp ⇒ Boolean
Delete all downloaded images.
-
#collage_file ⇒ String
Collage file name.
-
#img_height ⇒ Number
Height for images crop.
-
#img_path ⇒ String
Path for saving images.
-
#img_width ⇒ Number
Width for images crop.
Class Method Summary collapse
-
.valid?(file) ⇒ true, false
Validates downloaded image.
Instance Method Summary collapse
-
#collage(opts = {}) ⇒ true, false
Creates collage from 10 images.
-
#collage_simple(opts = {}) ⇒ true, false
deprecated
Deprecated.
First collage implementation
-
#initialize(opts = {}) ⇒ Image
constructor
A new instance of Image.
-
#resize(opts = {}) ⇒ String
Resize image.
-
#resize_to_fill(width, height, img, gravity = "Center") ⇒ MiniMagick::Image
Smart image crop.
Constructor Details
#initialize(opts = {}) ⇒ Image
Returns a new instance of Image
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/FlickrCollage.rb', line 151 def initialize(opts = {}) opts[:img_path] ||= IMG_PATH opts[:img_width] ||= IMG_WIDTH opts[:img_height] ||= IMG_HEIGHT opts[:collage_file] ||= COLLAGE_FILE @img_path = opts[:img_path] @img_width = opts[:img_width] @img_height = opts[:img_height] @collage_file = opts[:collage_file] @clear_tmp = opts[:clear_tmp] end |
Instance Attribute Details
#clear_tmp ⇒ Boolean
Delete all downloaded images
143 144 145 |
# File 'lib/FlickrCollage.rb', line 143 def clear_tmp @clear_tmp end |
#collage_file ⇒ String
Collage file name
143 144 145 |
# File 'lib/FlickrCollage.rb', line 143 def collage_file @collage_file end |
#img_height ⇒ Number
Height for images crop
143 144 145 |
# File 'lib/FlickrCollage.rb', line 143 def img_height @img_height end |
#img_path ⇒ String
Path for saving images
143 144 145 |
# File 'lib/FlickrCollage.rb', line 143 def img_path @img_path end |
#img_width ⇒ Number
Width for images crop
143 144 145 |
# File 'lib/FlickrCollage.rb', line 143 def img_width @img_width end |
Class Method Details
.valid?(file) ⇒ true, false
Validates downloaded image
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/FlickrCollage.rb', line 170 def self.valid?(file) return false if file.nil? begin image = MiniMagick::Image.open(file) rescue StandardError => e warn "Image.valid: caught exception #{e}" raise e if RAISE_ERROR end image.valid? end |
Instance Method Details
#collage(opts = {}) ⇒ true, false
Creates collage from 10 images
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/FlickrCollage.rb', line 214 def collage(opts = {}) unless opts[:files].size == IMG_COUNT info "Not enough images for collage" return false end begin (1..4).each do |row| montage = MiniMagick::Tool::Montage.new files = montage_resize(files: opts[:files], row: row) files.each do |file| montage << file end montage << "-mode" montage << "Concatenate" montage << "-background" montage << "none" montage << "-geometry" montage << montage_geometry(row: row) montage << "-tile" montage << montage_tile(row: row) montage << montage_img_path(row: row) montage.call end rescue StandardError => e warn "Image.collage: caught exception #{e}" raise e if RAISE_ERROR end FileUtils.rm_rf("#{@img_path}tmp") if @clear_tmp true end |
#collage_simple(opts = {}) ⇒ true, false
Deprecated.
First collage implementation
Creates simple collage 5x2 from 10 images
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/FlickrCollage.rb', line 285 def collage_simple(opts = {}) unless opts[:files].size == IMG_COUNT info "No images for collage" return false end begin montage = MiniMagick::Tool::Montage.new opts[:files].each do |file| montage << resize(file: file) end montage << "-mode" montage << "Concatenate" montage << "-background" montage << "none" montage << "-geometry" montage << "#{@img_width}x#{@img_height}+0+0" montage << "-tile" montage << "5x2" montage << "#{@img_path}#{@collage_file}.jpg" puts montage.inspect.to_s montage.call rescue StandardError => e warn "Image.collage: caught exception #{e}" raise e if RAISE_ERROR end true end |
#resize(opts = {}) ⇒ String
Resize image
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/FlickrCollage.rb', line 189 def resize(opts = {}) return false if opts[:file].nil? opts[:width] ||= @img_width opts[:height] ||= @img_height begin image = MiniMagick::Image.open(opts[:file]) resize_to_fill(opts[:width], opts[:height], image) file_resize = "#{@img_path}tmp/#{File.basename(opts[:file], ".jpg")}_resize.jpg" image.write file_resize debug "Resize #{opts[:file]} to #{opts[:width]}x#{opts[:height]}" rescue StandardError => e warn "Image.resize: caught exception #{e}" raise e if RAISE_ERROR end file_resize end |
#resize_to_fill(width, height, img, gravity = "Center") ⇒ MiniMagick::Image
Smart image crop
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/FlickrCollage.rb', line 254 def resize_to_fill(width, height, img, gravity = "Center") cols, rows = img[:dimensions] img. do |cmd| if width != cols || height != rows scale_x = width / cols.to_f scale_y = height / rows.to_f if scale_x >= scale_y cols = (scale_x * (cols + 0.5)).round rows = (scale_x * (rows + 0.5)).round cmd.resize cols.to_s else cols = (scale_y * (cols + 0.5)).round rows = (scale_y * (rows + 0.5)).round cmd.resize "x#{rows}" end end cmd.gravity gravity cmd.background "rgba(255,255,255,0.0)" cmd.extent "#{width}x#{height}" if cols != width || rows != height end end |