Search code examples
rubyimage-processingshrine

Add --density flag to libvips command with image_processing?


I'm trying to add the --density flag to the libvips command so that PDFs (or other vector images) convert to JPG at an acceptable resolution.

Context—before I upgraded to a recent version of image_processing, I was using MiniMagick with the older version and was able to add arbitrary flags using this code:

  MiniMagick::Tool::Convert.new do |convert|
    convert << "-verbose" 
    convert << "-background" << "white"
    convert << "-flatten"
    convert << "-density" << "300"
    convert << page.path          
    convert << "-quality" << "82"
    convert << page_image.path
  end

Solution

  • I think libvips equivalent of ImageMagick -density is the :dpi option on vips_pdfload(). With image_processing gem you can set it via loader:

    require "image_processing/vips"
    
    ImageProcessing::Vips
      .source(image)
      .loader(dpi: 300)
      .saver(quality: 82)
      # ...