Skip to content
This repository has been archived by the owner on Dec 9, 2017. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
…s#issue/1

just changed how the format is specified with converting documents
  • Loading branch information
kellyredding committed Sep 11, 2010
1 parent ab746b0 commit 4fbcd98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -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

Expand Down
38 changes: 19 additions & 19 deletions 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)
Expand All @@ -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
Expand All @@ -29,27 +29,27 @@ 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
POSTSCRIPT_EXT.each { |extension| return true if file_ext == extension }
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
Expand All @@ -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")
Expand All @@ -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}"
Expand All @@ -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
Expand All @@ -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']
Expand Down

0 comments on commit 4fbcd98

Please sign in to comment.