Skip to content

Instantly share code, notes, and snippets.

@swistaczek
Created June 20, 2011 08:38
Show Gist options
  • Save swistaczek/1035314 to your computer and use it in GitHub Desktop.
Save swistaczek/1035314 to your computer and use it in GitHub Desktop.
Reduce Paperclip processor (loseless opt js, css, jpg, png or gif files)
#lib/paperclip_processors/loseless_reduce.rb
require 'reduce'
## LoseLessReduce
# This module is able to reduce js, css, jpg, png or gif files
# thanks to gem reduce by grosser (via yui compressor or punnyPng)
# https://github.com/grosser/reduce
module Paperclip
class LoselessReduce < Processor
class InstanceNotGiven < ArgumentError; end
def compress_asset
Reduce.reduce(@file.path)
end
def initialize(file, options = {})
super
@file = file
@instance = options[:instance]
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
end
def make
@file.pos = 0
dst = Tempfile.new([@basename, @format].compact.join("."))
dst << compress_asset
dst
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment