Method: Vips::Image#write_to_buffer

Defined in:
lib/vips/image.rb

#write_to_buffer(format_string, **opts) ⇒ String

Write this image to a memory buffer. Save options may be encoded in the format_string or given as a hash. For example:

buffer = image.write_to_buffer ".jpg[Q=90]"

or equivalently:

image.write_to_buffer ".jpg", Q: 90

The full set of save options depend on the selected saver. Try something like:

$ vips jpegsave

to see all the available options for JPEG save.

Parameters:

  • format_string (String)

    save format plus options

  • opts (Hash)

    set of options

Options Hash (**opts):

  • :strip (Boolean) — default: false

    Strip all metadata from image

  • :background (Array<Float>) — default: 0

    Background colour to flatten alpha against, if necessary

Returns:

  • (String)

    the image saved in the specified format

Raises:



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/vips/image.rb', line 633

def write_to_buffer format_string, **opts
  raise Vips::Error, "filename is nil" if format_string.nil?
  filename = Vips.p2str(Vips.vips_filename_get_filename(format_string))
  option_string = Vips.p2str(Vips.vips_filename_get_options(format_string))

  # try to save with the new target API first, only fall back to the old
  # buffer API if there's no target save for this filetype
  saver = nil
  if Vips.at_least_libvips?(8, 9)
    Vips.vips_error_freeze
    saver = Vips.vips_foreign_find_save_target filename
    Vips.vips_error_thaw
  end

  if !saver.nil?
    target = Vips::Target.new_to_memory
    Vips::Operation.call saver, [self, target], opts, option_string
    buffer = target.get("blob")
  else
    saver = Vips.vips_foreign_find_save_buffer filename
    raise Vips::Error if saver.nil?

    buffer = Vips::Operation.call saver, [self], opts, option_string
    raise Vips::Error if buffer.nil?
  end

  write_gc

  buffer
end