Class: TaliaUtil::ImageConversions

Inherits:
Object
  • Object
show all
Defined in:
lib/talia_util/image_conversions.rb

Overview

Helper class that provides an interface to convert images, create thumbnails and pyramid images. This provides a central point from which to call external conversion tools.

Since it calls the command line, it should be compatible both with JRuby and plain Ruby.

Class Method Summary collapse

Class Method Details

.convert_commandObject

Returns the command that is used for converting thumbnails



25
26
27
28
29
30
31
32
33
# File 'lib/talia_util/image_conversions.rb', line 25

def convert_command
  @convert_command ||= if(defined?(TaliaCore))
    TaliaCore::CONFIG['convert_command'] || '/opt/local/bin/convert'
  elsif(defined?(CONVERT_COMMAND))
    CONVERT_COMMAND
  else
    raise(ArgumentError('Unconfigured convert command. If not in Talia, set CONVERT_COMMAND'))
  end
end

.create_pyramid(source, destination) ⇒ Object

Creates the pyramid image for IIP by running the configured system command. This automatically creates the file in the correct location (IIP root)



56
57
58
59
# File 'lib/talia_util/image_conversions.rb', line 56

def create_pyramid(source, destination)
  pyramid_command = "#{vips_command} im_vips2tiff \"#{source}\" \"#{destination}\":jpeg:85,tile:256x256,pyramid"
  execute_command(pyramid_command, destination)
end

.create_thumb(source, destination) ⇒ Object

Create the thumbnail by running the configured creation command.



47
48
49
50
51
# File 'lib/talia_util/image_conversions.rb', line 47

def create_thumb(source, destination)
  thumbnail_size = "#{thumb_options['width']}x#{thumb_options['height']}"
  thumbnail_command = "#{convert_command} \"#{source}\" -quality 85 -thumbnail \"#{thumbnail_size}>\" -background transparent -gravity center -extent #{thumbnail_size} \"#{destination}\""
  execute_command(thumbnail_command, destination)
end

.thumb_optionsObject

Returns the options for the thumbnail



36
37
38
39
40
41
42
43
44
# File 'lib/talia_util/image_conversions.rb', line 36

def thumb_options
  @thumb_options ||= if(defined?(TaliaCore))
    TaliaCore::CONFIG['thumb_options'] || { 'width' => '80', 'height' => '120' }
  elsif(defined?(THUMB_OPTIONS))
    THUMB_OPTIONS
  else
    raise(ArgumentError('Unconfigured thumbnail options. If not in Talia, set THUMB_OPTIONS'))
  end
end

.to_png(source, destination) ⇒ Object

Transforms the given image into a PNG image. Note that the .png suffix will automatically added to the destination name



63
64
65
66
67
# File 'lib/talia_util/image_conversions.rb', line 63

def to_png(source, destination)
  destination = "#{destination}.png" unless(File.extname(destination) == '.png')
  convert_line = "#{convert_command} \"#{source}\" \"#{destination}\""
  execute_command(convert_line, destination)
end

.vips_commandObject

Returns the command that is used for converting images



14
15
16
17
18
19
20
21
22
# File 'lib/talia_util/image_conversions.rb', line 14

def vips_command
  @vips_command ||= if(defined?(TaliaCore))
    TaliaCore::CONFIG['vips_command'] || '/opt/local/bin/vips'
  elsif(defined?(VIPS_COMMAND))
    VIPS_COMMAND
  else
    raise(ArgumentError('Unconfigured vips command. If not in Talia, set VIPS_COMMAND'))
  end
end