Module: Mindee::Image::ImageUtils
- Defined in:
- lib/mindee.rb,
lib/mindee/image/image_utils.rb
Overview
Miscellaneous image operations.
Class Method Summary collapse
-
.calculate_dimensions_from_media_box(image, media_box) ⇒ Array<Integer>
Computes the Height & Width from a page’s media box.
-
.calculate_new_dimensions(original, max_width: nil, max_height: nil) ⇒ Object
Computes the new dimensions for a given SKBitmap, and returns a scaled down version of it relative to the provided bounds.
-
.compress_image_quality(image, quality) ⇒ Object
Compresses the quality of the provided MiniMagick image.
-
.image_to_stringio(image, format = 'JPEG') ⇒ StringIO
Converts a StringIO containing an image into a MiniMagick image.
-
.pdf_to_magick_image(pdf_stream, image_quality) ⇒ MiniMagick::Image
Transforms a PDF into a MagickImage.
-
.resize_image(image, width, height) ⇒ Object
Resizes a provided MiniMagick Image with the given width & height, if present.
-
.to_image(image) ⇒ MiniMagick::Image
Mostly here so that IDEs don’t get confused on the type (@type annotation fails sometimes.).
Class Method Details
.calculate_dimensions_from_media_box(image, media_box) ⇒ Array<Integer>
Computes the Height & Width from a page’s media box. Falls back to the size of the initial image.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/mindee/image/image_utils.rb', line 81 def self.calculate_dimensions_from_media_box(image, media_box) if !media_box.nil? && media_box.any? [ media_box[2]&.to_i || image[:width].to_i, media_box[3]&.to_i || image[:height].to_i, ] else [image[:width].to_i, image[:height].to_i] end end |
.calculate_new_dimensions(original, max_width: nil, max_height: nil) ⇒ Object
Computes the new dimensions for a given SKBitmap, and returns a scaled down version of it relative to the provided bounds.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mindee/image/image_utils.rb', line 61 def self.calculate_new_dimensions(original, max_width: nil, max_height: nil) raise 'Provided image could not be processed for resizing.' if original.nil? return [original.width, original.height] if max_width.nil? && max_height.nil? width_ratio = max_width ? max_width.to_f / original.width : Float::INFINITY height_ratio = max_height ? max_height.to_f / original.height : Float::INFINITY scale_factor = [width_ratio, height_ratio].min new_width = (original.width * scale_factor).to_i new_height = (original.height * scale_factor).to_i [new_width, new_height] end |
.compress_image_quality(image, quality) ⇒ Object
Compresses the quality of the provided MiniMagick image.
25 26 27 |
# File 'lib/mindee/image/image_utils.rb', line 25 def self.compress_image_quality(image, quality) image.quality quality.to_s end |
.image_to_stringio(image, format = 'JPEG') ⇒ StringIO
Converts a StringIO containing an image into a MiniMagick image.
47 48 49 50 51 52 53 54 |
# File 'lib/mindee/image/image_utils.rb', line 47 def self.image_to_stringio(image, format = 'JPEG') image.format format blob = image.to_blob stringio = StringIO.new(blob) stringio.rewind stringio end |
.pdf_to_magick_image(pdf_stream, image_quality) ⇒ MiniMagick::Image
Transforms a PDF into a MagickImage. This is currently used for single-page PDFs.
96 97 98 99 100 101 |
# File 'lib/mindee/image/image_utils.rb', line 96 def self.pdf_to_magick_image(pdf_stream, image_quality) compressed_image = MiniMagick::Image.read(pdf_stream.read) compressed_image.format('jpg') compressed_image.quality image_quality.to_s compressed_image end |
.resize_image(image, width, height) ⇒ Object
Resizes a provided MiniMagick Image with the given width & height, if present.
12 13 14 15 16 17 18 19 20 |
# File 'lib/mindee/image/image_utils.rb', line 12 def self.resize_image(image, width, height) if width && height image.resize "#{width}x#{height}" elsif width image.resize width.to_s elsif height image.resize "x#{height}" end end |
.to_image(image) ⇒ MiniMagick::Image
Mostly here so that IDEs don’t get confused on the type (@type annotation fails sometimes.)
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mindee/image/image_utils.rb', line 32 def self.to_image(image) if image.respond_to?(:read) && image.respond_to?(:rewind) image.rewind MiniMagick::Image.read(image) elsif image.is_a?(MiniMagick::Image) image else raise "Expected an I/O object or a MiniMagick::Image. '#{image.class}' given instead." end end |