class Paperclip::Vips

Attributes

auto_orient[RW]
convert_options[RW]
current_geometry[RW]
format[RW]
source_file_options[RW]
target_geometry[RW]
whiny[RW]

Public Class Methods

new(file, options = {}, attachment = nil) click to toggle source
Calls superclass method
# File lib/paperclip-vips/paperclip/vips.rb, line 6
def initialize(file, options = {}, attachment = nil)
  super

  geometry = options[:geometry].to_s
  @should_crop = geometry[-1,1] == '#'
  @target_geometry = options.fetch(:string_geometry_parser, Geometry).parse(geometry)
  @current_geometry = options.fetch(:file_geometry_parser, Geometry).from_file(@file)
  @whiny = options.fetch(:whiny, true)
  
  @current_format = current_format(file).downcase
  @format = options[:format] || @current_format

  @basename = File.basename(@file.path, @current_format)
end

Public Instance Methods

make() click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 21
def make
  source = @file
  filename = [@basename, @format ? ".#{@format}" : ""].join
  destination = TempfileFactory.new.generate(filename)

  begin
    thumbnail = ::Vips::Image.thumbnail(source.path, width, { height: crop ? height : nil, crop: crop }) if @target_geometry
    thumbnail = ::Vips::Image.new_from_file(source.path) if !defined?(thumbnail) || thumbnail.nil?
    thumbnail = process_convert_options(thumbnail)
    save_thumbnail(thumbnail, destination.path)
    
  rescue => e
    if @whiny
      message = "There was an error processing the thumbnail for #{@basename}:\n" + e.message
      raise Paperclip::Error, message
    end
  end

  return destination
end

Private Instance Methods

crop() click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 43
def crop
  if @should_crop
    return @options[:crop] || :centre
  end

  nil
end
current_format(file) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 51
def current_format(file)
  extension = File.extname(file.path)
  return extension if extension.present?

  extension = File.extname(file.original_filename)
  return extension if extension.present?

  return ""
end
height() click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 65
def height
  @target_geometry&.height || @current_geometry.height
end
process_convert_options(image) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 69
def process_convert_options(image)
  if image && @options[:convert_options].present?
    commands = JSON.parse(@options[:convert_options], symbolize_names: true)
    commands.each do |cmd|
      image = ::Vips::Operation.call(cmd[:cmd], [image, *cmd[:args]], cmd[:optional] || {})
    end
  end

  return image
end
save_gif(thumbnail, path) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 95
def save_gif(thumbnail, path)
  thumbnail.magicksave(path)
end
save_jpg(thumbnail, path) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 91
def save_jpg(thumbnail, path)
  thumbnail.jpegsave(path)
end
save_png(thumbnail, path) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 99
def save_png(thumbnail, path)
  thumbnail.pngsave(path)
end
save_thumbnail(thumbnail, path) click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 80
def save_thumbnail(thumbnail, path)
  case @current_format
  when ".jpeg", ".jpg"
    save_jpg(thumbnail, path)
  when ".gif"
    save_gif(thumbnail, path)
  when ".png"
    save_png(thumbnail, path)
  end
end
width() click to toggle source
# File lib/paperclip-vips/paperclip/vips.rb, line 61
def width
  @target_geometry&.width || @current_geometry.width
end