Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/light_resizer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'light_resizer/version'
require 'light_resizer/resizer'
require 'light_resizer/compressor'
require 'light_resizer/dimension_not_found'
require 'light_resizer/middleware'
require 'light_resizer/carrierwave_resize'
Expand All @@ -11,9 +12,13 @@ module LightResizer
include Configurations

configurable Array, :allowed_dimensions
configurable Integer, :jpeg_quality
configurable Integer, :png_compression

configuration_defaults do |default_config|
default_config.allowed_dimensions = []
default_config.jpeg_quality = 100
default_config.png_compression = 0
end

end
end
2 changes: 1 addition & 1 deletion lib/light_resizer/carrierwave_resize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def resized_image_path(width, height, with_crop)
end

end
end
end
19 changes: 19 additions & 0 deletions lib/light_resizer/compressor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# encoding: utf-8

require 'piet'

module LightResizer
class Compressor

def initialize(path_to_file)
@path_to_file = path_to_file
end

def process
options = { quality: LightResizer.configuration.jpeg_quality, level: LightResizer.configuration.png_compression, verbose: true }
result = Piet.optimize(@path_to_file, options)
Rails.logger.debug "Optimize passsed with result: #{result}"
end

end
end
2 changes: 1 addition & 1 deletion lib/light_resizer/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def resize_url_regex
end

end
end
end
19 changes: 17 additions & 2 deletions lib/light_resizer/resizer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# encoding: utf-8

module LightResizer
class Resizer

IMAGE_EXTENSION = 'png'
IMAGE_EXTENSION = 'jpeg'

def initialize(public_path, options)
@public_path = public_path
Expand All @@ -14,6 +15,7 @@ def process
resize_or_crop
create_directory
save_image
compress_image
end

def upload_path
Expand All @@ -22,6 +24,15 @@ def upload_path

protected

def compress_image
LightResizer::Compressor.new(full_path).process
end

def compress_original_image
path_to_original_image = "#{@public_path}#{@options[:directory]}/#{@options[:image]}#{@options[:ext]}"
LightResizer::Compressor.new(path_to_original_image).process
end

def image_exists?
File.exists? full_path
end
Expand All @@ -38,7 +49,7 @@ def check_allowed_dimension
end

def crop
image.resize_to_fill! width, height
image.resize_to_fill!(width, height, Magick::NorthGravity)
end

def resize
Expand All @@ -62,6 +73,10 @@ def image
@image ||= ::Magick::Image.read("#{@public_path}#{@options[:directory]}/#{@options[:image]}#{@options[:ext]}").first
end

def full_path
@full_path ||= "#{@public_path}#{@options}"
end

def full_path
@full_path ||= "#{@public_path}#{upload_path}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/light_resizer/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module LightResizer
VERSION = '0.2.0'
VERSION = '0.2.1'
end
3 changes: 2 additions & 1 deletion light_resizer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ Gem::Specification.new do |spec|
spec.add_dependency 'rmagick'
spec.add_dependency 'rack'
spec.add_dependency 'configurations', '~> 2.2.0'

spec.add_dependency 'piet'
spec.add_dependency 'piet-binary'
end