paperclip-compression 0.1.1 → 0.2.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 +65 -11
  2. metadata +12 -7
@@ -1,19 +1,60 @@
1
- require "paperclip"
1
+ require 'paperclip'
2
2
 
3
3
  module Paperclip
4
4
  # Compresses the JPEG and PNG files
5
5
  class Compression < Processor
6
6
 
7
- attr_accessor :format
7
+ JPEGTRAN_DEFAULT_OPTS = '-copy none -optimize -perfect'
8
+ OPTIPNG_DEFAULT_OPTS = '-o 5'
9
+
10
+ attr_accessor :format, :jpegtran_opts, :optipng_opts
8
11
 
9
12
  def initialize(file, options = {}, attachment = nil)
10
13
  super
11
14
  @format = options[:format]
12
15
  @current_format = File.extname(@file.path)
13
16
  @basename = File.basename(@file.path, @current_format)
17
+
18
+ if @options[:processor_options] && (compression_opts = @options[:processor_options][:compression])
19
+
20
+ @jpegtran_opts = if compression_opts.has_key?(:jpeg)
21
+ if (jpeg_opts = compression_opts[:jpeg])
22
+ jpeg_opts.kind_of?(String) ? jpeg_opts : JPEGTRAN_DEFAULT_OPTS
23
+ else
24
+ false
25
+ end
26
+ else
27
+ JPEGTRAN_DEFAULT_OPTS
28
+ end
29
+
30
+ @optipng_opts = if compression_opts.has_key?(:png)
31
+ if (png_opts = compression_opts[:png])
32
+ png_opts.kind_of?(String) ? png_opts : OPTIPNG_DEFAULT_OPTS
33
+ else
34
+ false
35
+ end
36
+ else
37
+ OPTIPNG_DEFAULT_OPTS
38
+ end
39
+
40
+ else
41
+ @jpegtran_opts = JPEGTRAN_DEFAULT_OPTS
42
+ @optipng_opts = OPTIPNG_DEFAULT_OPTS
43
+ end
14
44
  end
15
45
 
16
46
  def make
47
+ case @attachment.content_type
48
+ when 'image/jpeg' then process_jpegtran
49
+ when 'image/png' then process_optipgn
50
+ else
51
+ @file
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def process_jpegtran
17
58
  src = @file
18
59
  dst = Tempfile.new([@basename, @format ? ".#{@format}" : ''])
19
60
  dst.binmode
@@ -22,18 +63,31 @@ module Paperclip
22
63
  dst_path = File.expand_path(dst.path)
23
64
 
24
65
  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
66
+ if @jpegtran_opts
67
+ Paperclip.run('jpegtran', "#{@jpegtran_opts} #{src_path} > #{dst_path}")
68
+ dst
30
69
  else
31
- return src
70
+ src
32
71
  end
33
- rescue
34
- # TODO
72
+ rescue Cocaine::ExitStatusError => e
73
+ raise Paperclip::Error, "JPEGTRAN : There was an error processing the thumbnail for #{@basename}" if @whiny
74
+ rescue Cocaine::CommandNotFoundError => e
75
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'jpegtran'.Please install jpegtran.")
76
+ end
77
+ end
78
+
79
+ def process_optipgn
80
+ src = @file
81
+ src_path = File.expand_path(src.path)
82
+
83
+ begin
84
+ Paperclip.run('optipng', "#{@optipng_opts} #{src_path}") if @optipng_opts
85
+ src
86
+ rescue Cocaine::ExitStatusError => e
87
+ raise Paperclip::Error, "OPTIPNG : There was an error processing the thumbnail for #{@basename}" if @whiny
88
+ rescue Cocaine::CommandNotFoundError => e
89
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'optipng'.Please install optipng.")
35
90
  end
36
- dst
37
91
  end
38
92
 
39
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-compression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-24 00:00:00.000000000Z
12
+ date: 2011-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: paperclip
16
- requirement: &77593450 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '2.4'
21
+ version: '3.3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *77593450
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
25
30
  description: JPEG and PNG compression for Paperclip gem
26
31
  email: emrekutlu@gmail.com
27
32
  executables: []
@@ -29,7 +34,7 @@ extensions: []
29
34
  extra_rdoc_files: []
30
35
  files:
31
36
  - lib/paperclip-compression.rb
32
- homepage: http://github.com/dakick/paperclip-compression
37
+ homepage: http://github.com/emrekutlu/paperclip-compression
33
38
  licenses: []
34
39
  post_install_message:
35
40
  rdoc_options: []
@@ -51,7 +56,7 @@ requirements:
51
56
  - jpegtran for JPEG compression
52
57
  - optipng for PNG compression
53
58
  rubyforge_project:
54
- rubygems_version: 1.8.8
59
+ rubygems_version: 1.8.23
55
60
  signing_key:
56
61
  specification_version: 3
57
62
  summary: Image compression for Paperclip