paperclip-compression 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/paperclip-compression.rb +41 -0
  2. metadata +46 -0
@@ -0,0 +1,41 @@
1
+ require "paperclip"
2
+
3
+ module Paperclip
4
+ # Compresses the JPEG and PNG files
5
+ class Compression < Processor
6
+
7
+ attr_accessor :format
8
+
9
+ def initialize(file, options = {}, attachment = nil)
10
+ super
11
+ @format = options[:format]
12
+ @current_format = File.extname(@file.path)
13
+ @basename = File.basename(@file.path, @current_format)
14
+ end
15
+
16
+ def make
17
+ src = @file
18
+ dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
19
+ dst.binmode
20
+
21
+ src_path = File.expand_path(src.path)
22
+ dst_path = File.expand_path(dst.path)
23
+
24
+ begin
25
+ if @attachment.content_type.eql?("image/jpeg")
26
+ Paperclip.run("jpegtran", "-copy none -optimize -perfect #{src_path} > #{dst_path}")
27
+ elsif @attachment.content_type.eql?("image/png")
28
+ Paperclip.run("optipng", "-o 5 #{src_path}")
29
+ return src
30
+ else
31
+ return src
32
+ end
33
+ rescue
34
+ # TODO
35
+ end
36
+ dst
37
+ end
38
+
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-compression
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - İ. Emre Kutlu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-24 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: JPEG and PNG compression for Paperclip gem
15
+ email:
16
+ - emrekutlu@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/paperclip-compression.rb
22
+ homepage: http://github.com/dakick/paperclip-compression
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.6
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Image compression for Paperclip
46
+ test_files: []