### Install ImageMagick or GraphicsMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Before using the MiniMagick module, you need to install either ImageMagick or GraphicsMagick. This example shows installation using Homebrew. ```sh $ brew install imagemagick # or $ brew install graphicsmagick ``` -------------------------------- ### Install libvips on Debian/Ubuntu Source: https://github.com/janko/image_processing/blob/master/README.md Use apt to install libvips on Debian or Ubuntu systems. This is required if you plan to use libvips as the backend for image processing. ```sh sudo apt install libvips ``` -------------------------------- ### Install ImageMagick on Debian/Ubuntu Source: https://github.com/janko/image_processing/blob/master/README.md Use apt to install ImageMagick on Debian or Ubuntu systems. This is required if you plan to use ImageMagick as the backend for image processing. ```sh sudo apt install imagemagick ``` -------------------------------- ### Install libvips on macOS Source: https://github.com/janko/image_processing/blob/master/README.md Use Homebrew to install libvips on macOS. This is required if you plan to use libvips as the backend for image processing. ```sh brew install vips ``` -------------------------------- ### Resize and Optimize Image with Ruby Source: https://github.com/janko/image_processing/wiki/Optimizing-processed-images Resizes an image to a maximum of 400x400 pixels with 85% quality, then optimizes the resulting thumbnail using the image_optim gem. Ensure the image_optim gem is installed and configured. ```ruby require "image_optim" thumbnail = ImageProcessing::Vips .resize_to_limit(400, 400) .saver(quality: 85) .call(image) image_optim = ImageOptim.new(...) image_optim.optimize_image!(thumbnail.path) thumbnail.open # refresh file descriptor thumbnail # optimized thumbnail ``` -------------------------------- ### Run Image Processing Tests Source: https://github.com/janko/image_processing/blob/master/README.md Execute the test suite for the image processing gem. Ensure both imagemagick and libvips libraries are installed before running. ```sh $ bundle exec rake test ``` -------------------------------- ### Convert PDF to JPG with Vips at 300 DPI Source: https://github.com/janko/image_processing/wiki/Specifying-resolution-of-images-of-PDFs Use the `dpi` option in the loader to control the resolution of rasterized PDF pages. This example renders the first page of a PDF to a JPG at 300 DPI. ```ruby pipeline = ImageProcessing::Vips .source(pdf_path) .convert("jpg") # renders the first page at 300 dpi image = pipeline.loader(dpi: 300, page: 0).call ``` -------------------------------- ### Instrument Image Processing Pipeline with Vips Source: https://github.com/janko/image_processing/blob/master/README.md Register an instrumenter block for a pipeline to record performance metrics. This example shows how to wrap pipeline execution with ActiveSupport::Notifications. ```ruby pipeline = ImageProcessing::Vips.instrumenter do |**options, &processing| options[:source] #=> # options[:loader] #=> { fail: true } options[:saver] #=> { quality: 85 } options[:format] #=> "png" options[:operations] #=> [[:resize_to_limit, 500, 500], [:flip, [:horizontal]]] options[:processor] #=> ImageProcessing::Vips::Processor ActiveSupport::Notifications.instrument("process.image_processing", **options) do processing.call # calls the pipeline end end pipeline .source(image) .loader(fail: true) .saver(quality: 85) .convert("png") .resize_to_limit(500, 500) .flip(:horizontal) .call # calls instrumenter ``` -------------------------------- ### Install ImageMagick on macOS Source: https://github.com/janko/image_processing/blob/master/README.md Use Homebrew to install ImageMagick on macOS. This is required if you plan to use ImageMagick as the backend for image processing. ```sh brew install imagemagick ``` -------------------------------- ### Split PDF to Images with Vips Source: https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images Utilize the Vips library for efficient PDF to image conversion, extracting each page. Vips must be installed and available. ```ruby page_count = Vips::Image.new_from_file(pdf_path).get("pdf-n_pages") vips = ImageProcessing::Vips .source(pdf_path) .convert("jpg") images = page_count.times.map do |page_number| vips.loader(page: page_number).call end images #=> # [ # #, (page 1) # #, (page 2) # ... # ] ``` -------------------------------- ### Configure MiniMagick to Use GraphicsMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md To use GraphicsMagick instead of ImageMagick, set `MiniMagick.graphicsmagick = true` before processing. This example shows resizing an image using GraphicsMagick. ```ruby require "image_processing/mini_magick" MiniMagick.graphicsmagick = true processed = ImageProcessing::MiniMagick .source(image) .resize_to_limit(400, 400) .strip .call # will use `gm convert` instead of `convert` processed #=> # ``` -------------------------------- ### Get Vips::Image Object Directly Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Allows more control by getting the Vips::Image object directly. Call the saver on the object afterwards. ```ruby vips_image = ImageProcessing::Vips .resize_to_limit(400, 400) .call(save: false) vips_image #=> # vips_image.write_to_file("/path/to/destination", **options) ``` -------------------------------- ### Split PDF to Images with MiniMagick Source: https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images Use MiniMagick to process a PDF and extract each page as a separate image. Ensure MiniMagick is installed and configured. ```ruby page_count = MiniMagick::Image.new(pdf_path).pages.count magick = ImageProcessing::MiniMagick .source(pdf_path) .convert("jpg") images = page_count.times.map do |page_number| magick.loader(page: page_number).call end images #=> # [ # #, (page 1) # #, (page 2) # ... # ] ``` -------------------------------- ### Inspecting Pipeline Options Source: https://github.com/janko/image_processing/blob/master/README.md Demonstrates how to inspect the configuration options of an image processing pipeline before execution. This allows for verification of loaders, savers, format, and operations. ```ruby pipeline = ImageProcessing::MiniMagick .source(image) .loader(page: 1) .convert("png") .resize_to_limit(400, 400) .strip pipeline.options # => {:source=>#, # :loader=>{:page=>1}, # :saver=>{}, # :format=>"png", # :operations=>[[:resize_to_limit, [400, 400]], [:strip, []]], # :processor_class=>ImageProcessing::MiniMagick::Processor} ``` -------------------------------- ### Resize-on-load Optimization Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Illustrates the correct and incorrect ways to chain resize operations for optimal performance using resize-on-load. ```ruby # BAD: cannot utilize resize-on-load ImageProcessing::Vips .source(image) .colourspace(:grey16) .resize_to_limit(400, 400) # GOOD: utilizes resize-on-load ImageProcessing::Vips .source(image) .resize_to_limit(400, 400) .colourspace(:grey16) ``` -------------------------------- ### Create MiniMagick::Tool directly Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md For more control over loading, you can create a `MiniMagick::Tool` object directly and pass it as the source file. ```ruby magick = MiniMagick.convert magick << "..." << "..." << "..." ImageProcessing::MiniMagick .source(magick) ``` -------------------------------- ### Executing Image Processing with Different Methods Source: https://github.com/janko/image_processing/blob/master/README.md Illustrates various ways to execute an image processing pipeline, including using `#call` with an argument, calling `#call` without arguments after defining the source, and using bang methods for immediate execution. ```ruby processed = ImageProcessing::MiniMagick .convert("png") .resize_to_limit(400, 400) .call(image) # OR processed = ImageProcessing::MiniMagick .source(image) # declare source image .convert("png") .resize_to_limit(400, 400) .call # OR processed = ImageProcessing::MiniMagick .source(image) .convert("png") .resize_to_limit!(400, 400) # bang method ``` -------------------------------- ### Basic Image Processing with Vips Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Demonstrates a typical image processing chain using the ImageProcessing::Vips module. It resizes an image and saves it with metadata stripped. ```ruby require "image_processing/vips" processed = ImageProcessing::Vips .source(image) .resize_to_limit(400, 400) .saver(strip: true) .call processed #=> # ``` -------------------------------- ### Specify Loader Options Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Forwards options to Vips::Image.new_from_file for loading images. Options like 'access' can be set to control loading behavior. ```ruby ImageProcessing::Vips .loader(access: :sequential) # ... ``` -------------------------------- ### Generate Thumbnails with JPEG Size Hint Source: https://github.com/janko/image_processing/wiki/Improving-ImageMagick-performance Use the `define` loader option to provide a JPEG size hint for efficient thumbnail generation. This reduces memory usage and speeds up processing for large images. ```ruby pipeline = ImageProcessing::MiniMagick .source(image) .loader(define: { jpeg: { size: "1600x1600" } }) large = pipeline.resize_to_limit!(800, 800) medium = pipeline.resize_to_limit!(500, 500) small = pipeline.resize_to_limit!(300, 300) icon = pipeline.resize_to_limit!(150, 150) ``` -------------------------------- ### Apply multiple commands using a hash Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#apply` method provides a convenient way to send multiple commands to the builder using a hash. Hash keys correspond to builder methods, and values can be single arguments, arrays, or booleans. ```ruby ImageProcessing::MiniMagick .apply( strip: true, crop: "200x200+0+0", resize_to_limit: [400, 400], convert: "jpg", saver: { quality: 100 }, loader: { define: { jpeg: { size: "1600x1600" } } } ) ``` -------------------------------- ### Basic Image Processing with MiniMagick Source: https://github.com/janko/image_processing/blob/master/README.md Demonstrates a simple image processing pipeline using the MiniMagick processor. The processing is executed using the `#call` method. ```ruby require "image_processing/mini_magick" processed = ImageProcessing::MiniMagick .source(file) .resize_to_limit(400, 400) .convert("png") .call processed #=> # ``` -------------------------------- ### Configure Saver Options Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Specifies options to be forwarded to Vips::Image#write_to_file. Options are merged if the saver clause is repeated. ```ruby ImageProcessing::Vips .saver(quality: 100) # alias for :Q # ... ``` ```ruby ImageProcessing::Vips .saver(tile: true) .saver(compression: :lzw) # resolves to ImageProcessing::Vips .saver(tile: true, compression: :lzw) ``` -------------------------------- ### Apply Sharpening During Resize Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Use the :sharpen option with the resize_to_limit! method to sharpen thumbnails. Adjust radius and sigma for desired sharpening effect. Higher sigma means more sharpening. ```ruby ImageProcessing::MiniMagick .source(image) .resize_to_limit!(400, 400, sharpen: { radius: 0, sigma: 1 }) ``` ```bash # convert input.jpg -resize 400x400> -sharpen 0x1 output.jpg ``` -------------------------------- ### Generating Multiple Image Derivatives with Vips Source: https://github.com/janko/image_processing/blob/master/README.md Shows how to create multiple image derivatives of different sizes from a single pipeline definition using the Vips processor. Bang methods (`!`) are used to execute processing. ```ruby require "image_processing/vips" pipeline = ImageProcessing::Vips .source(file) .convert("png") large = pipeline.resize_to_limit!(800, 800) medium = pipeline.resize_to_limit!(500, 500) small = pipeline.resize_to_limit!(300, 300) ``` -------------------------------- ### ImageProcessing::MiniMagick Usage Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Demonstrates how to use the ImageProcessing::MiniMagick module with ImageMagick and GraphicsMagick. ```APIDOC ## ImageMagick Usage ### Description Example of using ImageProcessing::MiniMagick with the default ImageMagick backend. ### Method ```ruby require "image_processing/mini_magick" processed = ImageProcessing::MiniMagick .source(image) .resize_to_limit(400, 400) .strip .call processed #=> # ``` ## GraphicsMagick Usage ### Description Example of using ImageProcessing::MiniMagick with the GraphicsMagick backend. ### Method ```ruby require "image_processing/mini_magick" MiniMagick.graphicsmagick = true processed = ImageProcessing::MiniMagick .source(image) .resize_to_limit(400, 400) .strip .call # will use `gm convert` instead of `convert` processed #=> # ``` ``` -------------------------------- ### loader Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Specifies options to be forwarded to Vips::Image.new_from_file for image loading. ```APIDOC ## #loader ### Description Specifies options that will be forwarded to `Vips::Image.new_from_file` when loading an image. Options from multiple `#loader` calls are merged. ### Method `loader(option: value, ...)` ### Parameters * `option` (Symbol | String) - The loading option key (e.g., `:access`, `:page`, `:dpi`, `:loader`, `:autorot`). * `value` - The value for the specified option. ### Request Example ```rb ImageProcessing::Vips.loader(access: :sequential) ImageProcessing::Vips.loader(page: 0, dpi: 300) ImageProcessing::Vips.loader(loader: :svg) # Calls Vips::Image.svgload ImageProcessing::Vips.loader(autorot: false) # Disables automatic rotation ``` ### Notes Supports format-specific load options and an `:autorot` option to control automatic rotation. Allows passing a pre-loaded `Vips::Image` object as the source. ``` -------------------------------- ### Basic Image Processing with MiniMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md This snippet demonstrates basic image processing using ImageProcessing::MiniMagick. It resizes the image and removes metadata. Ensure the 'image' object is defined. ```ruby require "image_processing/mini_magick" processed = ImageProcessing::MiniMagick .source(image) .resize_to_limit(400, 400) .strip .call processed #=> # ``` -------------------------------- ### Individual JPEG Hints for Each Thumbnail Source: https://github.com/janko/image_processing/wiki/Improving-ImageMagick-performance For maximum performance, specify individual JPEG size hints for each thumbnail by dynamically calculating the hint based on the desired output dimensions. This ensures only the necessary image portion is loaded for each thumbnail. ```ruby definitions = { large: [800, 800], medium: [500, 500], small: [300, 300], icon: [150, 150], } definitions.inject({}) do |hash, (name, (width, height))| thumbnail = ImageProcessing::MiniMagick .source(image) .loader(define: { jpeg: { size: "#{width*2}x#{height*2}" } }) .resize_to_limit!(width, height) hash.merge!(name => thumbnail) end ``` -------------------------------- ### Resize with Default Sharpening Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Applies resizing to a limit while enabling the default sharpening filter. Use this for a quick sharpening effect during thumbnail generation. ```ruby ImageProcessing::Vips .source(image) .resize_to_limit!(400, 400, sharpen: true) ``` -------------------------------- ### Resize to Fill with Vips Source: https://github.com/janko/image_processing/wiki/Resize-To-Fill Use this for resizing images to exact dimensions with the Vips backend. The `:crop` option determines the cropping behavior. ```ruby ImageProcessing::Vips .resize_to_fill(400, 400, crop: :centre) ``` -------------------------------- ### Control saving process with call(save: false) Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md To gain more control over the saving process, call `#call(save: false)` to obtain the `MiniMagick::Tool` object and complete the saving manually. ```ruby magick = ImageProcessing::MiniMagick .resize_to_limit(400, 400) .call(save: false) ``` ```ruby magick #=> # ``` ```ruby magick << "output.png" magick.call ``` -------------------------------- ### Set Resource Limits with MiniMagick Source: https://github.com/janko/image_processing/wiki/Limiting-resources Use the `limits` method to set memory, width, and time constraints for MiniMagick image processing. This helps control resource usage and prevent attacks. ```ruby ImageProcessing::MiniMagick .limits(memory: "50MiB", width: "10MP", time: 30) .resize_to_limit(400, 400) .call(image) ``` -------------------------------- ### Configure image saver options Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#saver` method allows configuration of saving options, including definitions for coders/decoders and whether to allow splitting multi-layer images. Other options are applied as ImageMagick operations before saving. ```ruby ImageProcessing::MiniMagick.saver(define: { jpeg: { optimize_coding: false } }).call(image) ``` ```shell # convert input.jpg -auto-orient -define jpeg:optimize-coding=false output.jpg ``` ```ruby ImageProcessing::MiniMagick.convert("png").call(pdf_document) ``` ```ruby # raises ImageProcessing::Error ``` ```ruby ImageProcessing::MiniMagick.convert("png").saver(allow_splitting: true).call(pdf_document) ``` ```shell # lets ImageMagick generate a "*-{idx}.png" image for each page ``` ```ruby ImageProcessing::MiniMagick .saver(quality: 80, interlace: "Line") .call(image) ``` ```shell # convert input.jpg ... -quality 80 -interlace Line output.jpg ``` -------------------------------- ### Resize and Pad Image Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Resizes an image to fit within specified dimensions while maintaining aspect ratio, then pads the remaining area. Padding color is transparent if the image has an alpha channel, otherwise black. Additional options are forwarded to Vips::Image#thumbnail_image. ```ruby pipeline = ImageProcessing::Vips.source(image) # 600x800 result = pipeline.resize_and_pad!(400, 400) Vips::Image.new_from_file(result.path).size #=> [400, 400] ``` ```ruby pipeline.resize_and_pad!(400, 400, alpha: true) ``` ```ruby pipeline.resize_and_pad!(400, 400, extend: :copy) ``` ```ruby pipeline.resize_and_pad!(400, 400, gravity: "north-west") ``` -------------------------------- ### ImageProcessing::MiniMagick Methods Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Documentation for various image processing methods available in ImageProcessing::MiniMagick. ```APIDOC ## .valid_image? ### Description Tries to recompress the image, and returns `true` if no exception was raised, otherwise returns `false`. ### Method ```ruby ImageProcessing::MiniMagick.valid_image?(normal_image) #=> true ImageProcessing::MiniMagick.valid_image?(invalid_image) #=> false ``` ## #resize_to_limit ### Description Downsizes the image to fit within the specified dimensions while retaining the original aspect ratio. Will only resize the image if it's larger than the specified dimensions. ### Method ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_limit!(400, 400) MiniMagick::Image.new(result.path).dimensions #=> [300, 400] ``` ### Parameters #### Query Parameters - **width** (Integer) - Optional - The maximum width. - **height** (Integer) - Optional - The maximum height. ### Request Example ```ruby # Resize to fit within 400x400 pipeline.resize_to_limit!(400, 400) # Resize only by width pipeline.resize_to_limit!(400, nil) # Resize only by height pipeline.resize_to_limit!(nil, 400) ``` ## #resize_to_fit ### Description Resizes the image to fit within the specified dimensions while retaining the original aspect ratio. Will downsize the image if it's larger than the specified dimensions or upsize if it's smaller. ### Method ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_fit!(400, 400) MiniMagick::Image.new(result.path).dimensions #=> [300, 400] ``` ### Parameters #### Query Parameters - **width** (Integer) - Optional - The target width. - **height** (Integer) - Optional - The target height. ### Request Example ```ruby # Resize to fit within 400x400 pipeline.resize_to_fit!(400, 400) # Resize only by width pipeline.resize_to_fit!(400, nil) # Resize only by height pipeline.resize_to_fit!(nil, 400) ``` ## #resize_to_fill ### Description Resizes the image to fill the specified dimensions while retaining the original aspect ratio. If necessary, will crop the image in the larger dimension. ### Method ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_fill!(400, 400) MiniMagick.new(result.path).dimensions #=> [400, 400] ``` ### Parameters #### Query Parameters - **width** (Integer) - Required - The target width. - **height** (Integer) - Required - The target height. - **gravity** (String) - Optional - The direction of the image crop (defaults to "Center"). ### Request Example ```ruby # Resize to fill 400x400, cropping from the center pipeline.resize_to_fill!(400, 400) # Resize to fill 400x400, cropping from the north-west pipeline.resize_to_fill!(400, 400, gravity: "north-west") ``` ``` -------------------------------- ### Delegate Unknown Methods to Vips::Image Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Handles unknown methods by delegating them to the Vips::Image object, allowing direct use of Vips operations like crop, invert, and blur. ```ruby ImageProcessing::Vips .crop(0, 0, 300, 300) .invert .set("icc-profile-data", custom_profile) .remove("xmp-data") .gaussblur(2) # ... ``` -------------------------------- ### Append arguments to convert command Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Use the `#append` method to add arguments directly to the `convert` command. This is useful for setting options like quality or flip. ```ruby ImageProcessing::MiniMagick .append("-quality", 100) .append("-flip") ``` -------------------------------- ### Conditional Image Processing Pipeline Source: https://github.com/janko/image_processing/wiki/Doing-conditional-processing Builds an image processing pipeline that conditionally sharpens JPEGs. Requires the 'image_processing' gem. ```ruby pipeline = ImageProcessing::Vips .source(image) .resize_to_limit(400, 400) if image.path.match?(/.jpe?g$/i) pipeline = pipeline .saver(interlace: true) # create only interlaced JPEGs, excluding PNGs .sharpen(sigma: 0.5, x1: 2, y2: 10, y3: 20, m1: 0, m2: 3) # sharpen only JPEGs end pipeline.call #=> # ``` -------------------------------- ### #resize_and_pad Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Resizes an image to fit within specified dimensions, maintaining aspect ratio and padding the remaining area. ```APIDOC ## #resize_and_pad ### Description Resizes the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with transparent color if source image has alpha channel, black otherwise. ### Method Instance method ### Parameters - **width** (Integer) - The target width. - **height** (Integer) - The target height. - **options** (Hash) - Additional options. Accepts `:alpha`, `:extend`, `:background`, `:gravity`. Options like `:extend` and `:background` are forwarded to `Vips::Image#gravity`. Any other options are forwarded to `Vips::Image#thumbnail_image`. ### Request Example ```rb pipeline = ImageProcessing::Vips.source(image) result = pipeline.resize_and_pad!(400, 400, alpha: true, gravity: "north-west") ``` ### Response #### Success Response - Returns the processed image object. ### Response Example ```rb Vips::Image.new_from_file(result.path).size #=> [400, 400] ``` ``` -------------------------------- ### Specifying Image Source Types Source: https://github.com/janko/image_processing/blob/master/README.md Shows the different types of objects that can be used as a source for image processing, including File objects, Strings, Pathnames, and specific image objects from Vips or MiniMagick. ```ruby ImageProcessing::Vips.source(File.open("source.jpg")) ImageProcessing::Vips.source("source.jpg") ImageProcessing::Vips.source(Pathname.new("source.jpg")) ImageProcessing::Vips.source(Vips::Image.new_from_file("source.jpg")) ``` -------------------------------- ### Vips Crop: Low Behavior (libvips < 8.8) Source: https://github.com/janko/image_processing/wiki/Resize-To-Fill Achieve `:low` cropping behavior for libvips versions prior to 8.8. This involves calculating target dimensions and then cropping from the top-left. ```ruby image = Vips::Image.new_from_file(source_path) factor = Rational(1000, [image.width, image.height].min) target_width = (image.width * factor).ceil target_height = (image.height * factor).ceil ImageProcessing::Vips .source(source_path) .resize_to_fit(target_width, target_height) .crop(0, 0, 1000, 1000) .call ``` -------------------------------- ### Configure image loader options Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#loader` method accepts special options to control how images are loaded, such as specifying input file type, page, geometry, auto-orientation, and definitions for decoders. ```ruby ImageProcessing::MiniMagick.loader(loader: "jpg").call(image) ``` ```shell # convert jpg:input.jpg -auto-orient output.jpg ``` ```ruby ImageProcessing::MiniMagick.loader(page: 0).convert("png").call(pdf) ``` ```shell # convert input.pdf[0] -auto-orient output.png ``` ```ruby ImageProcessing::MiniMagick.loader(geometry: "300x300").call(image) ``` ```shell # convert input.jpg[300x300] -auto-orient output.jpg ``` ```ruby ImageProcessing::MiniMagick.loader(auto_orient: false).call(image) ``` ```shell # convert input.jpg output.jpg ``` ```ruby ImageProcessing::MiniMagick.loader(define: { jpeg: { size: "300x300" } }).call(image) ``` ```shell # convert -define jpeg:size=300x300 input.jpg -auto-orient output.jpg ``` ```ruby ImageProcessing::MiniMagick .loader(strip: true, type: "TrueColorMatte") .call(image) ``` ```shell # convert -strip -type TrueColorMatte input.jpg ... output.jpg ``` -------------------------------- ### Convert Image to PNG Format Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Specifies the output format for an image pipeline, converting it to PNG. The original format is retained by default unless specified otherwise. ```ruby pipeline = ImageProcessing::Vips.source(image) result = pipeline.convert!("png") File.extname(result.path) #=> ".png" ``` -------------------------------- ### method_missing Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Delegates any unknown methods to the underlying Vips::Image object for direct manipulation. ```APIDOC ## #method_missing ### Description Any methods called on the pipeline that are not explicitly defined are delegated to the underlying `Vips::Image` object. ### Request Example ```rb ImageProcessing::Vips.crop(0, 0, 300, 300).invert.set("icc-profile-data", custom_profile).remove("xmp-data").gaussblur(2) ``` ### Notes This allows for direct use of `Vips::Image` methods within the processing pipeline. ``` -------------------------------- ### Define Coder/Decoder Options Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Adds coder/decoder options using the -define flag. Options are provided as a hash where keys correspond to the coder and values are option hashes. ```ruby ImageProcessing::MiniMagick .define(png: { compression_level: 8, format: "png8" }) # -define png:compression-level=8 -define png:format=png8 # ... ``` -------------------------------- ### Define Coder/Decoder Options Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Adds coder/decoder options using the `-define` flag. ```APIDOC ## define ### Description Adds coder/decoder options with `-define` from the specified Hash. ### Method POST ### Endpoint /janko/image_processing/define ### Parameters #### Query Parameters - **options** (object) - Required - A hash of coder/decoder options (e.g., `{ png: { compression_level: 8, format: "png8" } }`). ### Request Example ```json { "image_path": "path/to/your/image.jpg", "options": { "png": { "compression_level": 8, "format": "png8" } } } ``` ### Response #### Success Response (200) - **path** (string) - The path to the processed image with defined options. #### Response Example ```json { "path": "path/to/processed/image.jpg" } ``` ``` -------------------------------- ### Load Image Directly with Vips::Image Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Loads an image directly using Vips::Image and then passes the Vips::Image object as the source to the ImageProcessing::Vips pipeline. ```ruby vips_image = Vips::Image.magickload(file.path, n: -1) ImageProcessing::Vips .source(vips_image) # ... ``` -------------------------------- ### Resize and Pad Image Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Resizes an image to fit within specified dimensions while maintaining aspect ratio and padding the remaining area. Use the :background option for padding color and :gravity for positioning. ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_and_pad!(400, 400) MiniMagick::Image.new(result.path).dimensions #=> [400, 400] ``` ```ruby pipeline.resize_and_pad!(400, 400, background: :transparent) # default pipeline.resize_and_pad!(400, 400, background: [65, 105, 225]) # RGB value pipeline.resize_and_pad!(400, 400, background: [65, 105, 225, 1.0]) # RGBA value pipeline.resize_and_pad!(400, 400, background: "...") # any supported ImageMagick color value ``` ```ruby pipeline.resize_and_pad!(400, 400, gravity: "north-west") ``` -------------------------------- ### Controlling Image Processing Output Source: https://github.com/janko/image_processing/blob/master/README.md Explains how to manage the output of an image processing pipeline. It covers receiving a Tempfile by default, obtaining the raw image object, or saving to a specified destination. ```ruby pipeline = ImageProcessing::Vips.source(image) pipeline.call #=> # pipeline.call(save: false) #=> # pipeline.call(destination: "/path/to/destination") ``` -------------------------------- ### Resize with Custom Sharpening Mask Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Applies resizing with a user-defined convolution mask for sharpening. This allows for fine-tuning the sharpening effect. ```ruby sharpen_mask = Vips::Image.new_from_array [ [-1, -1, -1], [-1, 24, -1], [-1, -1, -1]], 16 ImageProcessing::Vips .source(image) .resize_to_limit!(400, 400, sharpen: sharpen_mask) ``` -------------------------------- ### Merge loader options Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md When the `#loader` clause is repeated, its options are merged. This allows for building complex loading configurations incrementally. ```ruby ImageProcessing::MiniMagick .loader(page: 0) .loader(geometry: "300x300") ``` ```ruby # resolves to ImageProcessing::MiniMagick .loader(page: 0, geometry: "300x300") ``` -------------------------------- ### Resize Image to Fill Dimensions and Crop Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#resize_to_fill!` method resizes an image to fill specified dimensions, maintaining aspect ratio. If necessary, it crops the image in the larger dimension. The `image` object must be defined. ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_fill!(400, 400) MiniMagick.new(result.path).dimensions #=> [400, 400] ``` -------------------------------- ### Delegate Unknown Methods to MiniMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Unknown methods are delegated to MiniMagick.convert. This allows direct use of ImageMagick convert options. ```ruby ImageProcessing::MiniMagick .quality(100) # -quality 100 .crop("300x300+0+0") # -crop 300x300+0+0 .resample("300x300") # -resample 300x300 .stack { |cmd| ... } # ( ... ) # ... ``` -------------------------------- ### Vips Crop: High Behavior (libvips < 8.8) Source: https://github.com/janko/image_processing/wiki/Resize-To-Fill Achieve `:high` cropping behavior for libvips versions prior to 8.8. This requires custom cropping from the bottom-right after resizing. ```ruby image = Vips::Image.new_from_file(source_path) factor = Rational(1000, [image.width, image.height].min) target_width = (image.width * factor).ceil target_height = (image.height * factor).ceil ImageProcessing::Vips .source(source_path) .resize_to_fit(target_width, target_height) .custom { |thumb| thumb.crop(thumb.width - 1000, thumb.height - 1000, 1000, 1000) } .call ``` -------------------------------- ### Resize Image to Fit Specified Dimensions (One Dimension Omitted) Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Similar to `#resize_to_limit!`, you can omit one dimension when using `#resize_to_fit!` to resize based on only the provided dimension while keeping the aspect ratio. Ensure the `pipeline` object is initialized. ```ruby pipeline.resize_to_fit!(400, nil) # or pipeline.resize_to_fit!(nil, 400) ``` -------------------------------- ### Resize Image to Fit Within Limits (One Dimension Omitted) Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md When using `#resize_to_limit!`, you can omit one dimension (width or height) to resize only based on the provided dimension while maintaining aspect ratio. Ensure the `pipeline` object is initialized. ```ruby pipeline.resize_to_limit!(400, nil) # or pipeline.resize_to_limit!(nil, 400) ``` -------------------------------- ### Apply Watermark with Vips Source: https://github.com/janko/image_processing/wiki/Watermarking Use the `composite` method with Vips to overlay a watermark. This method wraps the `vips_composite()` libvips function. Specify the watermark source, composition mode, gravity, and offset. ```ruby watermark # can be String, Pathname, File, Tempfile, or Vips::Image ImageProcessing::Vips .source(original) .composite(watermark, # Set the watermark as the overlay image mode: "over", # Apply the watermark 'over' the source (default) gravity: "south-east", # Apply the watermark in the bottom right corner offset: [10, 10], # Apply the watermark 10 pixels up and left from ':gravity' setting ) .call # Apply operation ``` -------------------------------- ### Resize to Fill with MiniMagick Source: https://github.com/janko/image_processing/wiki/Resize-To-Fill Use this when resizing an image to exact dimensions with the MiniMagick backend. The `:gravity` option controls the cropping area. ```ruby ImageProcessing::MiniMagick .resize_to_fill(400, 400, gravity: "Center") # ... ``` -------------------------------- ### Resize Image to Fit Specified Dimensions Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#resize_to_fit!` method resizes an image to fit within specified dimensions, maintaining aspect ratio. It will downsize or upsize the image as needed. The `image` object must be defined. ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_fit!(400, 400) MiniMagick::Image.new(result.path).dimensions #=> [300, 400] ``` -------------------------------- ### Set JPEG Quality with MiniMagick Source: https://github.com/janko/image_processing/wiki/Specifying-image-quality Use the `saver` option with the `quality` parameter to control JPEG compression. This is useful for lossy formats like JPEG. ```ruby ImageProcessing::MiniMagick .resize_to_limit(400, 400) .saver(quality: 75) # specify quality .call(image) ``` -------------------------------- ### Convert Image Format Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Specifies the output format for the image. Defaults to the original format or JPEG if the source has no extension. ```ruby pipeline = ImageProcessing::MiniMagick.source(image) result = pipeline.convert!("png") File.extname(result.path) #=> ".png" ``` -------------------------------- ### Add ImageProcessing Gem to Gemfile Source: https://github.com/janko/image_processing/blob/master/README.md Add the image_processing gem to your Gemfile to include it in your project. Specify the version constraint as needed. ```ruby gem "image_processing", "~> 2.0" ``` -------------------------------- ### Apply Watermark with MiniMagick Source: https://github.com/janko/image_processing/wiki/Watermarking Use the `composite` method with MiniMagick to overlay a watermark. Specify the watermark source, composition mode, gravity, and offset. ```ruby watermark # can be String, Pathname, File, or Tempfile ImageProcessing::MiniMagick .source(original) .composite(watermark, # Set the watermark as the overlay image mode: "over", # Apply the watermark 'over' the source (default) gravity: "south-east", # Apply the watermark in the bottom right corner offset: [10, 10], # Apply the watermark 10 pixels up and left from ':gravity' setting ) .call # Apply operation ``` -------------------------------- ### Set JPEG Quality with Vips Source: https://github.com/janko/image_processing/wiki/Specifying-image-quality Use the `saver` option with the `quality` parameter (or its alias `:Q`) to control JPEG compression. This is useful for lossy formats like JPEG. ```ruby ImageProcessing::Vips .resize_to_limit(400, 400) .saver(quality: 75) # specify quality .call(image) ``` -------------------------------- ### Resize and Pad Image Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Resizes an image to fit within specified dimensions while maintaining aspect ratio and padding the remaining area with a specified color. ```APIDOC ## resize_and_pad ### Description Resizes the image to fit within the specified dimensions while retaining the original aspect ratio. If necessary, will pad the remaining area with the given color. ### Method POST ### Endpoint /janko/image_processing/resize_and_pad ### Parameters #### Query Parameters - **width** (integer) - Required - The target width for the resized image. - **height** (integer) - Required - The target height for the resized image. - **background** (string or array) - Optional - The color to use for padding. Can be a color name, RGB array, RGBA array, or any ImageMagick supported color value. Defaults to transparent/white. - **gravity** (string) - Optional - The gravity to apply while cropping. Defaults to "Center". ### Request Example ```json { "image_path": "path/to/your/image.jpg", "width": 400, "height": 400, "background": "transparent", "gravity": "north-west" } ``` ### Response #### Success Response (200) - **path** (string) - The path to the processed image. #### Response Example ```json { "path": "path/to/processed/image.jpg" } ``` ``` -------------------------------- ### Composite Image with Premultiplied Alpha Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Composites an image with an overlay, enabling premultiplied alpha blending. ```ruby composite(overlay, premultiplied: true) ``` -------------------------------- ### Resize Image to Fill with Gravity Option Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md When using `#resize_to_fill!`, you can specify the image's gravity direction using the `:gravity` option to control how cropping occurs. The default gravity is 'Center'. Ensure the `pipeline` object is initialized. ```ruby pipeline.resize_to_fill!(400, 400, gravity: "north-west") ``` -------------------------------- ### Add Ruby-vips Gem to Gemfile Source: https://github.com/janko/image_processing/blob/master/README.md Add the ruby-vips gem to your Gemfile if you are using libvips as the backend. Specify the version constraint as needed. ```ruby gem "ruby-vips", "~> 2.0" ``` -------------------------------- ### Convert Image Format Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Specifies the output format for the image. ```APIDOC ## convert ### Description Specifies the output format. By default, the original format is retained. If the source file doesn't have a file extension, the format will default to JPEG. ### Method POST ### Endpoint /janko/image_processing/convert ### Parameters #### Query Parameters - **format** (string) - Required - The desired output format (e.g., "png", "jpg"). ### Request Example ```json { "image_path": "path/to/your/image.jpg", "format": "png" } ``` ### Response #### Success Response (200) - **path** (string) - The path to the processed image in the new format. #### Response Example ```json { "path": "path/to/processed/image.png" } ``` ``` -------------------------------- ### Validate Image with MiniMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Use the `.valid_image?` class method to check if an image file is valid. It returns `true` if recompression succeeds without errors, `false` otherwise. Ensure `normal_image` and `invalid_image` are defined. ```ruby ImageProcessing::MiniMagick.valid_image?(normal_image) #=> true ImageProcessing::MiniMagick.valid_image?(invalid_image) #=> false ``` -------------------------------- ### Resize Image to Fit Within Limits Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md The `#resize_to_limit!` method downsizes an image to fit within specified dimensions, maintaining aspect ratio. It only resizes if the image is larger than the limits. The `image` object must be defined. ```ruby pipeline = ImageProcessing::MiniMagick.source(image) # 600x800 result = pipeline.resize_to_limit!(400, 400) MiniMagick::Image.new(result.path).dimensions #=> [300, 400] ``` -------------------------------- ### Direct Vips::Image Composite Call Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Invokes the Vips::Image#composite method directly, passing the blend mode as the second argument. ```ruby composite(overlay, :over) # calls Vips::Image#composite ``` -------------------------------- ### Configure Vips Cache Settings Source: https://github.com/janko/image_processing/wiki/Limiting-resources Control libvips caching behavior by setting the maximum number of operations, memory usage, and tracked files. This optimizes processing speed by managing cached data. ```ruby Vips.vips_cache_set_max 500 # up to 500 last operations ``` ```ruby Vips.vips_cache_set_max_mem 100*1024*1024 # up to 100MB of data ``` ```ruby Vips.vips_cache_set_max_files 500 # up to 500 tracked files ``` -------------------------------- ### Set resource limits for ImageMagick Source: https://github.com/janko/image_processing/blob/master/doc/minimagick.md Use the `#limits` method to set pixel cache resource limits for the ImageMagick command, such as memory, width, and time. This helps manage resource consumption during image processing. ```ruby ImageProcessing::MiniMagick .limits(memory: "50MiB", width: "10MP", time: 30) .resize_to_limit(400, 400) .call(image) ``` ```shell # convert -limit memory 50MiB -limit width 10MP -limit time 30 input.jpg ... output.jpg ``` -------------------------------- ### custom Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Yields the intermediary Vips::Image object to a block for custom processing logic. ```APIDOC ## #custom ### Description Yields the current `Vips::Image` object to a block, allowing for custom image manipulations. The block's return value determines the next step in the pipeline. ### Method `custom { |image| ... }` ### Parameters * `image` (Vips::Image) - The current image object within the pipeline. ### Request Example ```rb ImageProcessing::Vips.custom { |image| image + image.invert if invert? } ``` ### Notes If the block returns a `Vips::Image` object, it's used for further processing. If `nil` is returned, the original `Vips::Image` object is retained. ``` -------------------------------- ### Force Specific Image Loader Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Forces libvips to use a specific loader (e.g., :svg) when it cannot determine the image type automatically. ```ruby ImageProcessing::Vips .loader(loader: :svg) # calls `Vips::Image.svgload` # ... ``` -------------------------------- ### Resize Image to Fit Source: https://github.com/janko/image_processing/blob/master/doc/vips.md Resizes an image to fit within specified dimensions, maintaining aspect ratio. Upsizes or downsizes as needed. Additional options are forwarded to Vips::Image#thumbnail_image. ```ruby pipeline = ImageProcessing::Vips.source(image) # 600x800 result = pipeline.resize_to_fit!(400, 400) Vips::Image.new_from_file(result.path).size #=> [300, 400] ``` ```ruby pipeline.resize_to_fit!(400, nil) # or pipeline.resize_to_fit!(nil, 400) ``` ```ruby pipeline.resize_to_fit!(400, 400, linear: true) ```