diff --git a/Rakefile b/Rakefile index 8c0ad93..6a0e31b 100644 --- a/Rakefile +++ b/Rakefile @@ -24,7 +24,7 @@ spec = Gem::Specification.new do |s| s.add_development_dependency("kelredd-useful", [">= 0.4.0"]) s.add_development_dependency("kelredd-simple-gem", [">= 0.7.0"]) - s.add_dependency('mini_magick') + s.add_dependency('mini_magick', "~>2.1") s.add_dependency('flvtool2') end diff --git a/lib/pruview/document.rb b/lib/pruview/document.rb index d91b1ec..33cbf87 100644 --- a/lib/pruview/document.rb +++ b/lib/pruview/document.rb @@ -1,9 +1,9 @@ module Pruview class Document - + TMP_DIR = '/tmp' - + def initialize(source, target_dir, target_permission=0666) raise Pruview::Exceptions::InvalidError, "Invalid source file: #{source.to_s}" if !File.file?(source) raise Pruview::Exceptions::InvalidError, "Invalid target directory: #{target_dir.to_s}" if !File.directory?(target_dir) @@ -14,11 +14,11 @@ def initialize(source, target_dir, target_permission=0666) @image = process_image(get_image(source)) @tempfile = nil end - + def to_jpg(name, width, height, crop = false) scale_img = scale_image(width, height, crop) + scale_img.format 'jpg' scale_img.combine_options do |img| - img.format 'jpg' img.quality '90' img.interlace 'plane' end @@ -29,9 +29,9 @@ def to_jpg(name, width, height, crop = false) FileUtils.mv(tmp_target, target, :force => true) return target end - + protected - + def format_supported?(source) file_ext = file_extension(source) #return true if file_ext == PSD_EXT # don't support photoshop for now @@ -39,17 +39,17 @@ def format_supported?(source) IMAGE_EXT.each { |extension| return true if file_ext == extension } return false end - + def format_postscript?(source) file_ext = file_extension(source) POSTSCRIPT_EXT.each { |extension| return true if file_ext == extension } return false end - + def file_extension(source_file) - File.extname(source_file).downcase.chomp + File.extname(source_file).downcase.chomp end - + def get_image(source) source = get_postscript_source(source) if format_postscript?(source) begin @@ -58,7 +58,7 @@ def get_image(source) raise "Error reading source image: #{err.message}" end end - + def get_postscript_source(source) begin @tempfile = MiniMagick::ImageTempFile.new("pruview.jpg") @@ -69,14 +69,14 @@ def get_postscript_source(source) run_system_command("convert -format jpg \"#{source}[0]\" \"#{@tempfile.path}\"", "Error processing postscript document") return @tempfile.path end - + def process_image(image) image.format PROCESS_FORMAT set_RGB_colorspace(image) image.strip return image end - + def set_RGB_colorspace(image) colorspace = run_system_command("identify -format \"%r\" #{image.path}", "Error reading document colorspace") puts "Colorspace: #{colorspace}" @@ -93,13 +93,13 @@ def scale_image(width, height, crop = false) begin image = MiniMagick::Image.from_file(@image.path) crop_image(image, crop) - image.resize "#{width}x#{height}" if crop || @image[:width].to_i > width || @image[:height] > height + image.resize "#{width}x#{height}" if crop || @image[:width].to_i > width || @image[:height] > height return image rescue Exception => err raise "Error scaling image: #{err.message}" end end - + def crop_image(image, ratio) if ratio.kind_of?(Array) && ratio.length == 2 ratio_width, ratio_height = ratio @@ -124,18 +124,18 @@ def crop_image(image, ratio) end return image end - + def run_system_command(command, error_message) output = `#{command}` raise "#{error_message}: error given #{$?}\n#{output}" if $? != 0 return output end - + end - + # Configurations Document::PROCESS_FORMAT = 'jpg' - + Document::PSD_EXT = '.psd' Document::POSTSCRIPT_EXT = ['.pdf', '.eps', '.ai'] Document::IMAGE_EXT = ['.bmp', '.gif', '.jpg', '.jpeg', '.png', '.tga']