Module: Mindee::Image::ImageCompressor

Defined in:
lib/mindee.rb,
lib/mindee/image/image_compressor.rb

Overview

Image compressor module to handle image compression.

Class Method Summary collapse

Class Method Details

.compress_image(image, quality: 85, max_width: nil, max_height: nil) ⇒ StringIO

Resize and/or compress an SKBitmap. This assumes the ratio was provided before hands.

Parameters:

  • image (MiniMagick::Image, StringIO)

    Input image.

  • quality (Integer, nil) (defaults to: 85)

    Quality of the final file.

  • max_width (Integer, nil) (defaults to: nil)

    Maximum width. If not specified, the horizontal ratio will remain the same.

  • max_height (Integer) (defaults to: nil)

    Maximum height. If not specified, the vertical ratio will remain the same.

Returns:

  • (StringIO)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mindee/image/image_compressor.rb', line 14

def self.compress_image(image, quality: 85, max_width: nil, max_height: nil)
  processed_image = ImageUtils.to_image(image)
  processed_image.format 'jpg'
  final_width, final_height = ImageUtils.calculate_new_dimensions(
    processed_image,
    max_width: max_width,
    max_height: max_height
  )
  ImageUtils.resize_image(processed_image, final_width, final_height) if final_width || final_height
  ImageUtils.compress_image_quality(processed_image, quality)

  ImageUtils.image_to_stringio(processed_image)
end