Module: Morandi

Defined in:
lib/morandi.rb,
lib/morandi/errors.rb,
lib/morandi/redeye.rb,
lib/morandi/version.rb,
lib/morandi/cairo_ext.rb,
lib/morandi/crop_utils.rb,
lib/morandi/image_operation.rb,
lib/morandi/image_processor.rb,
lib/morandi/profiled_pixbuf.rb,
lib/morandi/srgb_conversion.rb,
lib/morandi/operation/colourify.rb,
lib/morandi/operation/straighten.rb,
lib/morandi/vips_image_processor.rb,
lib/morandi/operation/image_border.rb,
lib/morandi/operation/vips_straighten.rb

Overview

Morandi namespace should contain all the functionality of the gem

Defined Under Namespace

Modules: CairoExt, CropUtils, Operation, RedEye Classes: CorruptImageError, Error, ImageProcessor, ProfiledPixbuf, SrgbConversion, UnknownTypeError, VipsImageProcessor

Constant Summary collapse

VERSION =
'0.100.0'

Class Method Summary collapse

Class Method Details

.process(source, options, target_path, local_options = {}) ⇒ Object

The main entry point for the library

Parameters:

  • source (String|GdkPixbuf::Pixbuf)

    source image

  • options (Hash)

    The options describing expected processing to perform

  • target_path (String)

    target location for image

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

    Hash of options other than desired transformations

Options Hash (options):

  • 'brighten' (Integer)

    Change image brightness (-20..20)

  • 'gamma' (Float)

    Gamma correct image

  • 'contrast' (Integer)

    Change image contrast (-20..20)

  • 'sharpen' (Integer)

    Sharpen (1..5) / Blur (-1..-5)

  • 'straighten' (Integer)

    Rotate by a small angle (in degrees) and zoom in to fill the size

  • 'redeye' (Array[[Integer,Integer],...])

    Apply redeye correction at point

  • 'angle' (Integer)

    Rotate image clockwise by multiple of 90 degrees (0, 90, 180, 270)

  • 'crop' (Array[Integer,Integer,Integer,Integer])

    Crop image (x, y, width, height)

  • 'fx' (String)

    Apply colour filters (‘greyscale’, ‘sepia’, ‘bluetone’)

  • 'border-style' (String)

    Set border style (‘square’, ‘retro’)

  • 'background-style' (String)

    Set border colour (‘retro’, ‘black’, ‘white’)

  • 'quality' (Integer) — default: 97

    Set JPG compression value (1 to 100)

  • 'output.max' (Integer)

    Downscales the image to fit within the square of given size before processing to limit the required resources

  • 'output.width' (Integer)

    Sets desired width of resulting image

  • 'output.height' (Integer)

    Sets desired height of resulting image

  • 'image.auto-crop' (TrueClass|FalseClass) — default: true

    If the output dimensions are set and this is true, image is cropped automatically to the desired dimensions.

  • 'output.limit' (TrueClass|FalseClass) — default: false

    If the output dimensions are defined and this is true, the output image is scaled down to fit within square of size of the longer edge (ignoring shorter dimension!)

Options Hash (local_options):

  • 'path.icc' (String)

    A path to store the input after converting to sRGB colour space

  • 'processor' (String) — default: 'pixbuf'

    Name of the image processing library (‘pixbuf’, ‘vips’) NOTE: vips processor only handles subset of operations, see ‘Morandi::VipsImageProcessor.supports?` for details



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/morandi.rb', line 50

def process(source, options, target_path, local_options = {})
  case local_options['processor']
  when 'vips'
    raise(ArgumentError, 'Requested unsupported Vips operation') unless VipsImageProcessor.supports?(source, options)

    # Cache saves time in expense of RAM when performing the same processing multiple times
    # Cache is also created for files based on their names, which can lead to leaking files data, so in terms
    # of security it feels prudent to disable it. Latest libvips supports "revalidate" option to prevent that risk
    cache_max = 0
    concurrency = 2 # Hardcoding to 2 for now to maintain some balance between resource usage and performance
    VipsImageProcessor.with_global_options(cache_max: cache_max, concurrency: concurrency) do
      VipsImageProcessor.new(source, options).write_to_jpeg(target_path)
    end
  else
    ImageProcessor.new(source, options, local_options).tap(&:result).write_to_jpeg(target_path)
  end
end