Skip to content

Commit

Permalink
convert should support any output format
Browse files Browse the repository at this point in the history
  • Loading branch information
Darren Hardy committed Aug 31, 2016
1 parent 278fb31 commit 77a41e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
19 changes: 11 additions & 8 deletions app/processors/geo_concerns/processors/base_geo_processor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'mini_magick'
require 'pathname'

module GeoConcerns
module Processors
Expand Down Expand Up @@ -34,26 +35,27 @@ def self.run_commands(in_path, out_path, method_queue, options)
# @return [String] tempfile path
def self.temp_path(path)
time = (Time.now.to_f * 1000).to_i
"#{File.dirname(path)}/#{File.basename(path, File.extname(path))}_#{time}"
path = Pathname.new(path)
"#{path.dirname}/#{path.basename(path.extname)}_#{time}"
end

# Uses imagemagick to resize an image and convert it to jpeg format.
# Uses imagemagick to resize an image and convert it to the output format.
# Keeps the aspect ratio of the original image and adds padding to
# to the ouput image.
# to the output image. The file extension is the output format.
# @param in_path [String] file input path
# @param out_path [String] processor output file path
# @param out_path [String] processor output file path.
# @param options [Hash] creation options
# @option options [String] `:output_size` as "w h" or "wxh"
def self.convert(in_path, out_path, options)
size = options[:output_size].tr(' ', 'x')
# byebug
image = MiniMagick::Image.open(in_path)
image.format 'jpg'
image.combine_options do |i|
size = options[:output_size].tr(' ', 'x')
i.resize size
i.background 'white'
i.gravity 'center'
i.extent size
end
image.format Pathname.new(out_path).extname
image.write(out_path)
end
end
Expand Down Expand Up @@ -89,7 +91,8 @@ def output_srid
# Extracts the base file name (without extension) from the source file path.
# @return [String] base file name for source
def basename
File.basename(source_path, File.extname(source_path))
path = Pathname.new(source_path)
path.basename(path.extname)
end
end
end
Expand Down
17 changes: 13 additions & 4 deletions spec/processors/geo_concerns/processors/base_geo_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def source_path
subject { TestProcessor.new }

let(:directives) { { format: 'png', size: '200x400' } }
let(:output_file) { 'output/geo.png' }
let(:output_file_jpg) { 'output/geo.jpg' }
let(:output_file_png) { 'output/geo.png' }
let(:output_file) { output_file_png }
let(:file_name) { 'files/geo.tif' }
let(:options) { { output_size: '150 150' } }

Expand All @@ -43,11 +45,18 @@ def source_path
allow(MiniMagick::Image).to receive(:open).and_return(image)
end

it 'transforms the image and saves it as a jpeg' do
it 'transforms the image and saves it as a PNG' do
expect(image).to receive(:format).with('png')
expect(image).to receive(:combine_options)
expect(image).to receive(:write).with(output_file_png)
subject.class.convert(file_name, output_file_png, options)
end

it 'transforms the image and saves it as a JPG' do
expect(image).to receive(:format).with('jpg')
expect(image).to receive(:combine_options)
expect(image).to receive(:write).with(output_file)
subject.class.convert(file_name, output_file, options)
expect(image).to receive(:write).with(output_file_jpg)
subject.class.convert(file_name, output_file_jpg, options)
end
end

Expand Down

0 comments on commit 77a41e4

Please sign in to comment.