Class: TaliaUtil::ImageConversions
- 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.
See TaliaCore::Intializer for the options that can be set for the image conversions
Class Method Summary collapse
-
.convert_command ⇒ Object
Returns the command that is used for converting thumbnails.
-
.create_pyramid(source, destination) ⇒ Object
Creates the pyramid image for IIP by running the configured system command.
-
.create_thumb(source, destination) ⇒ Object
Create the thumbnail by running the configured creation command.
-
.thumb_options ⇒ Object
Returns the options for the thumbnail.
-
.to_png(source, destination) ⇒ Object
Transforms the given image into a PNG image.
-
.vips_command ⇒ Object
Returns the command that is used for converting images.
Class Method Details
.convert_command ⇒ Object
Returns the command that is used for converting thumbnails
28 29 30 31 32 33 34 35 36 |
# File 'lib/talia_util/image_conversions.rb', line 28 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)
61 62 63 64 65 |
# File 'lib/talia_util/image_conversions.rb', line 61 def create_pyramid(source, destination) # TODO: Options not configurable 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. Options are taken from talia_core.yml
51 52 53 54 55 56 |
# File 'lib/talia_util/image_conversions.rb', line 51 def create_thumb(source, destination) thumbnail_size = "#{['width']}x#{['height']}" thumbnail_force = %w(true yes).include?(['force']) ? "-background transparent -gravity center -extent #{thumbnail_size}" : '' thumbnail_command = "#{convert_command} \"#{source}\" -quality 85 -thumbnail \"#{thumbnail_size}>\" #{thumbnail_force} \"#{destination}\"" execute_command(thumbnail_command, destination) end |
.thumb_options ⇒ Object
Returns the options for the thumbnail
39 40 41 42 43 44 45 46 47 |
# File 'lib/talia_util/image_conversions.rb', line 39 def @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
69 70 71 72 73 |
# File 'lib/talia_util/image_conversions.rb', line 69 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_command ⇒ Object
Returns the command that is used for converting images
17 18 19 20 21 22 23 24 25 |
# File 'lib/talia_util/image_conversions.rb', line 17 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 |