Sha256: 08847c6b27225f92173d739b307c637193e78f4dde896a99ae41c6467f29141b

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

module DragonflyLibvips
  module Processors
    class Encode
      FORMATS_WITHOUT_PROFILE_SUPPORT = %w[dz webp hdr]

      def call(content, format, options = {})
        raise UnsupportedFormat unless SUPPORTED_FORMATS.include?(content.ext)

        format = format.to_s
        format = 'tif' if format == 'tiff'
        format = 'jpg' if format == 'jpeg'

        raise UnsupportedOutputFormat unless SUPPORTED_OUTPUT_FORMATS.include?(format)

        if content.mime_type == Rack::Mime.mime_type(".#{format}")
          content.ext ||= format
          content.meta['format'] = format
          return
        end

        options = options.each_with_object({}) { |(k, v), memo| memo[k.to_s] = v } # stringify keys

        input_options = options.fetch('input_options', {})
        output_options = options.fetch('output_options', {})

        input_options['access'] ||= 'sequential'
        if content.mime_type == 'image/jpeg'
          input_options['autorotate'] = true unless input_options.has_key?('autorotate')
        end

        output_options['profile'] ||= EPROFILE_PATH
        output_options.delete('profile') if FORMATS_WITHOUT_PROFILE_SUPPORT.include?(format)

        require 'vips'
        img = ::Vips::Image.new_from_file(content.path, input_options)

        content.update(
          img.write_to_buffer(".#{format}", output_options),
          'name' => "temp.#{format}",
          'format' => format
        )
        content.ext = format
      end

      def update_url(url_attributes, format, options = {})
        url_attributes.ext = format.to_s
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dragonfly_libvips-2.3.0 lib/dragonfly_libvips/processors/encode.rb
dragonfly_libvips-2.2.0 lib/dragonfly_libvips/processors/encode.rb