Class: FlickrCollage::Image

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_tmpBoolean

Delete all downloaded images

Returns:

  • (Boolean)

    the current value of clear_tmp



143
144
145
# File 'lib/FlickrCollage.rb', line 143

def clear_tmp
  @clear_tmp
end

#collage_fileString

Collage file name

Returns:

  • (String)

    the current value of collage_file



143
144
145
# File 'lib/FlickrCollage.rb', line 143

def collage_file
  @collage_file
end

#img_heightNumber

Height for images crop

Returns:

  • (Number)

    the current value of img_height



143
144
145
# File 'lib/FlickrCollage.rb', line 143

def img_height
  @img_height
end

#img_pathString

Path for saving images

Returns:

  • (String)

    the current value of img_path



143
144
145
# File 'lib/FlickrCollage.rb', line 143

def img_path
  @img_path
end

#img_widthNumber

Width for images crop

Returns:

  • (Number)

    the current value of img_width



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

Parameters:

  • file (String)

    file path for downloaded image

Returns:

  • (true)

    image is valid

  • (false)

    image is not valid



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

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :files (Array<String>)

    file paths for collage images

Returns:

  • (true)

    collage created successfully

  • (false)

    something went wrong



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

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :files (Array<String>)

    file paths for collage images

Returns:

  • (true)

    collage created successfully

  • (false)

    something went wrong



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

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :file (String)

    file path for image

  • :width (Number) — default: @img_width

    image new width

  • :height (Number) — default: @img_height

    image new height

Returns:

  • (String)

    file path for resized 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

Parameters:

  • width (Number)

    image new width

  • height (Number)

    image new height

  • img (MiniMagick::Image)

    image

  • gravity (String) (defaults to: "Center")

    crop around

Returns:

  • (MiniMagick::Image)

    cropped image



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.combine_options 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