imgkit 1.3.6 → 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source :rubygems
2
2
 
3
+ gemspec
4
+
3
5
  group :test do
4
6
  gem "rake"
5
7
  gem "rspec", "~> 2"
@@ -1,3 +1,8 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ imgkit (1.3.7)
5
+
1
6
  GEM
2
7
  remote: http://rubygems.org/
3
8
  specs:
@@ -40,6 +45,7 @@ PLATFORMS
40
45
  ruby
41
46
 
42
47
  DEPENDENCIES
48
+ imgkit!
43
49
  mocha
44
50
  rack
45
51
  rake
data/README.md CHANGED
@@ -12,24 +12,24 @@ Heavily based on [PDFKit](http://github.com/jdpace/pdfkit/).
12
12
  gem install imgkit
13
13
 
14
14
  ### wkhtmltoimage
15
- * **Automatic**: `sudo imgkit --install-wkhtmltoimage`
16
- install latest version into /usr/local/bin
15
+ * **Automatic**: `sudo imgkit --install-wkhtmltoimage`
16
+ install latest version into /usr/local/bin
17
17
  (overwrite defaults with e.g. ARCHITECTURE=amd64 TO=/home/foo/bin)
18
18
  * By hand: http://code.google.com/p/wkhtmltopdf/downloads/list
19
19
 
20
20
  ## Usage
21
-
21
+
22
22
  # IMGKit.new takes the HTML and any options for wkhtmltoimage
23
23
  # run `wkhtmltoimage --extended-help` for a full list of options
24
24
  kit = IMGKit.new(html, :quality => 50)
25
25
  kit.stylesheets << '/path/to/css/file'
26
-
26
+
27
27
  # Get the image BLOB
28
28
  img = kit.to_img
29
29
 
30
30
  # New in 1.3!
31
31
  img = kit.to_img(:jpg) #default
32
- img = kit.to_img(:jpeg)
32
+ img = kit.to_img(:jpeg)
33
33
  img = kit.to_img(:png)
34
34
  img = kit.to_img(:tif)
35
35
  img = kit.to_img(:tiff)
@@ -37,7 +37,7 @@ Heavily based on [PDFKit](http://github.com/jdpace/pdfkit/).
37
37
  # Save the image to a file
38
38
  file = kit.to_file('/path/to/save/file.jpg')
39
39
  file = kit.to_file('/path/to/save/file.png')
40
-
40
+
41
41
  # IMGKit.new can optionally accept a URL or a File.
42
42
  # Stylesheets can not be added when source is provided as a URL of File.
43
43
  kit = IMGKit.new('http://google.com')
@@ -47,12 +47,14 @@ Heavily based on [PDFKit](http://github.com/jdpace/pdfkit/).
47
47
  IMGKit.new('<html><head><meta name="imgkit-quality" content="75"...
48
48
 
49
49
  # Format shortcuts - New in 1.3!
50
- IMGKit.new("hello").to_jpg
51
- IMGKit.new("hello").to_jpeg
52
- IMGKit.new("hello").to_png
53
- IMGKit.new("hello").to_tif
54
- IMGKit.new("hello").to_tiff
55
-
50
+ IMGKit.new("hello").to_jpg
51
+ IMGKit.new("hello").to_jpeg
52
+ IMGKit.new("hello").to_png
53
+ IMGKit.new("hello").to_tif
54
+ IMGKit.new("hello").to_tiff
55
+
56
+ Note: Ruby's buffered I/O means that if you want to write the string data to a file or tempfile make sure to call `#flush` to ensure the contents don't get stuck in the buffer.
57
+
56
58
  ## Configuration
57
59
 
58
60
  If you're on Windows or you installed wkhtmltoimage by hand to a location other than /usr/local/bin you will need to tell IMGKit where the binary is. You can configure IMGKit like so:
@@ -68,10 +70,14 @@ If you're on Windows or you installed wkhtmltoimage by hand to a location other
68
70
 
69
71
  ## Heroku
70
72
 
71
- get a version of `wkhtmltoimage` as an amd64 binary and commit it
73
+ get a version of `wkhtmltoimage` as an amd64 binary and commit it
72
74
  to your git repo. I like to put mine in "./bin/wkhtmltoimage-amd64"
73
75
 
74
- assuming its in that location you can just do:
76
+ <a href="http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2&can=1&q=amd64">
77
+ version 0.10.0 has worked best for me
78
+ </a>
79
+
80
+ assuming its in that location you can just do:
75
81
 
76
82
  IMGKit.configure do |config|
77
83
  config.wkhtmltoimage = Rails.root.join('bin', 'wkhtmltoimage-amd64').to_s if ENV['RACK_ENV'] == 'production'
@@ -80,15 +86,15 @@ assuming its in that location you can just do:
80
86
  If you're not using Rails just replace Rails.root with the root dir of your app.
81
87
 
82
88
 
83
- ## Rails
89
+ ## Rails
84
90
 
85
91
  ### Mime Types
86
- register a .jpg mime type in:
92
+ register a .jpg mime type in:
87
93
 
88
94
  #config/initializers/mime_type.rb
89
95
  Mime::Type.register "image/jpeg", :jpg
90
96
 
91
- register a .png mime type in:
97
+ register a .png mime type in:
92
98
 
93
99
  #config/initializers/mime_type.rb
94
100
  Mime::Type.register "image/png", :png
@@ -111,7 +117,7 @@ You can respond in a controller with:
111
117
  - or -
112
118
 
113
119
  respond_to do |format|
114
- send_data(@kit.to_img(format.to_sym),
120
+ send_data(@kit.to_img(format.to_sym),
115
121
  :type => "image/png", :disposition => 'inline')
116
122
  end
117
123
 
@@ -143,6 +149,38 @@ To overcome the lack of support for --user-style-sheet option by wkhtmltoimage 0
143
149
 
144
150
  kit.stylesheets << css
145
151
 
152
+ ## Paperclip Example
153
+
154
+ Model:
155
+
156
+ ```ruby
157
+ class Model < ActiveRecord::Base
158
+ # attr_accessible :title, :body
159
+ has_attached_file :snapshot, :storage => :s3,
160
+ :s3_credentials => "#{Rails.root}/config/s3.yml"
161
+ end
162
+ ```
163
+
164
+ Controller:
165
+
166
+ ```ruby
167
+ def upload_image
168
+ model = Model.find(params[:id])
169
+ html = render_to_string
170
+ kit = IMGKit.new(html)
171
+ img = kit.to_img(:png)
172
+ file = Tempfile.new(["template_#{model.id}", 'png'], 'tmp',
173
+ :encoding => 'ascii-8bit')
174
+ file.write(img)
175
+ file.flush
176
+ model.snapshot = file
177
+ model.save
178
+ file.unlink
179
+ end
180
+ ```
181
+
182
+
183
+
146
184
  ## CarrierWave Workaround
147
185
 
148
186
  Contributed by @ticktricktrack
@@ -158,16 +196,17 @@ Contributed by @ticktricktrack
158
196
  def take_snapshot
159
197
  file = Tempfile.new(["template_#{self.id.to_s}", 'jpg'], 'tmp', :encoding => 'ascii-8bit')
160
198
  file.write(IMGKit.new(self.html_body, quality: 50, width: 600).to_jpg)
199
+ file.flush
161
200
  self.snapshot = file
162
- file.unlink
163
201
  self.save
202
+ file.unlink
164
203
  end
165
204
  end
166
205
  ```
167
206
 
168
207
 
169
208
  ## Note on Patches/Pull Requests
170
-
209
+
171
210
  * Fork the project.
172
211
  * Setup your development environment with: gem install bundler; bundle install
173
212
  * Make your feature addition or bug fix.
@@ -180,5 +219,5 @@ Contributed by @ticktricktrack
180
219
  ## Copyright
181
220
 
182
221
  Copyright (c) 2010 <a href="mailto:christopher.continanza@gmail.com">Chris Continanza</a>
183
- Based on work by Jared Pace
222
+ Based on work by Jared Pace
184
223
  See LICENSE for details.
data/bin/imgkit CHANGED
@@ -51,17 +51,17 @@ end
51
51
  def install(download, arch, install_to)
52
52
  puts "Installing #{download} to #{install_to}"
53
53
  if download =~ /.tar.bz2$/
54
- `sudo tar xjvf #{download}`
55
- `sudo mv wkhtmltoimage-#{arch} #{install_to}`
54
+ `tar xjvf #{download}`
55
+ `mv wkhtmltoimage #{install_to}`
56
56
  elsif download =~ /.tar.lzma$/
57
57
  raise "couldn't extract archive: lzcat not found" unless system("which lzcat > /dev/null 2>/dev/null")
58
58
  puts "Warning: lzcat is broken on Ubuntu. Re-run with --use-bzip to install alternate version"
59
- `sudo lzcat #{download} | tar x`
60
- `sudo mv wkhtmltoimage-#{arch} #{install_to}`
59
+ `lzcat #{download} | tar x`
60
+ `mv wkhtmltoimage-#{arch} #{install_to}`
61
61
  else
62
- `sudo mv #{download} #{install_to}`
62
+ `mv #{download} #{install_to}`
63
63
  end
64
- `sudo chmod +x #{install_to}`
64
+ `chmod +x #{install_to}`
65
65
  end
66
66
 
67
67
  @command = Proc.new { puts "Nothing to do: use --help"}
@@ -76,7 +76,7 @@ OptionParser.new do |parser|
76
76
  parser.on("--install-wkhtmltoimage", "Install wkhtmltoimage binaries (TO=/usr/local/bin ARCHITECTURE=i386)") do
77
77
  @command = Proc.new do
78
78
  architecture = ENV['ARCHITECTURE'] || detect_architecture
79
- install_to = ENV['TO']+'/wkhtmltoimage' || IMGKit.configuration.wkhtmltoimage
79
+ install_to = ENV['TO'] ? ENV['TO']+'/wkhtmltoimage' : IMGKit.configuration.wkhtmltoimage
80
80
 
81
81
  Dir.chdir '/tmp'
82
82
 
@@ -1,3 +1,3 @@
1
1
  class IMGKit
2
- VERSION = "1.3.6"
2
+ VERSION = "1.3.7"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe IMGKit do
4
4
  context "initialization" do
@@ -7,37 +7,37 @@ describe IMGKit do
7
7
  imgkit.source.should be_html
8
8
  imgkit.source.to_s.should == '<h1>Oh Hai</h1>'
9
9
  end
10
-
10
+
11
11
  it "should accept a URL as the source" do
12
12
  imgkit = IMGKit.new('http://google.com')
13
13
  imgkit.source.should be_url
14
14
  imgkit.source.to_s.should == 'http://google.com'
15
15
  end
16
-
16
+
17
17
  it "should accept a File as the source" do
18
18
  file_path = File.join(SPEC_ROOT,'fixtures','example.html')
19
19
  imgkit = IMGKit.new(File.new(file_path))
20
20
  imgkit.source.should be_file
21
21
  imgkit.source.to_s.should == file_path
22
22
  end
23
-
23
+
24
24
  it "should provide no default options" do
25
25
  imgkit = IMGKit.new('<h1>Oh Hai</h1>')
26
26
  imgkit.options.should be_empty
27
27
  end
28
-
28
+
29
29
  =begin
30
30
  it "should default to 'UTF-8' encoding" do
31
31
  imgkit = IMGKit.new('Captación')
32
32
  end
33
33
  =end
34
-
34
+
35
35
  it "should not have any stylesheet by default" do
36
36
  imgkit = IMGKit.new('<h1>Oh Hai</h1>')
37
37
  imgkit.stylesheets.should be_empty
38
38
  end
39
39
  end
40
-
40
+
41
41
  context "command" do
42
42
  it "should contstruct the correct command" do
43
43
  imgkit = IMGKit.new('html')
@@ -49,7 +49,7 @@ describe IMGKit do
49
49
  imgkit = IMGKit.new('html', :quality => 75)
50
50
  imgkit.command.should include('--quality')
51
51
  end
52
-
52
+
53
53
  it "will not include default options it is told to omit" do
54
54
  imgkit = IMGKit.new('html')
55
55
  imgkit = IMGKit.new('html', :disable_smart_shrinking => false)
@@ -65,17 +65,17 @@ describe IMGKit do
65
65
  imgkit.command[imgkit.command.index('--custom-header') + 1].should == 'User-Agent'
66
66
  imgkit.command[imgkit.command.index('--custom-header') + 2].should == 'some user agent'
67
67
  end
68
-
68
+
69
69
  it "read the source from stdin if it is html" do
70
70
  imgkit = IMGKit.new('html')
71
71
  imgkit.command[-2..-1].should == ['-', '-']
72
72
  end
73
-
73
+
74
74
  it "specify the URL to the source if it is a url" do
75
75
  imgkit = IMGKit.new('http://google.com')
76
76
  imgkit.command[-2..-1].should == ['http://google.com', '-']
77
77
  end
78
-
78
+
79
79
  it "should specify the path to the source if it is a file" do
80
80
  file_path = File.join(SPEC_ROOT,'fixtures','example.html')
81
81
  imgkit = IMGKit.new(File.new(file_path))
@@ -88,19 +88,21 @@ describe IMGKit do
88
88
  <head>
89
89
  <meta name="imgkit-page_size" content="Legal"/>
90
90
  <meta name="imgkit-orientation" content="Landscape"/>
91
+ <meta name="imgkit-crop-h" content="900"/>
91
92
  </head>
92
93
  </html>
93
94
  }
94
95
  imgkit = IMGKit.new(body)
95
96
  imgkit.command[imgkit.command.index('--page-size') + 1].should == 'Legal'
96
97
  imgkit.command[imgkit.command.index('--orientation') + 1].should == 'Landscape'
98
+ imgkit.command[imgkit.command.index('--crop-h') + 1].should == '900'
97
99
  end
98
100
  end
99
-
101
+
100
102
  context "#to_img(format = nil)" do
101
- def filetype_of(img)
103
+ def filetype_of(img)
102
104
  result = nil
103
- tmpfile = Tempfile.new('imgkit')
105
+ tmpfile = Tempfile.new('imgkit')
104
106
  File.open(tmpfile.path, 'w') { |f| f << img }
105
107
  result = `file #{tmpfile.path}`
106
108
  tmpfile.unlink()
@@ -112,13 +114,13 @@ describe IMGKit do
112
114
  img = imgkit.to_img
113
115
  filetype_of(img).should include('JPEG')
114
116
  end
115
-
117
+
116
118
  it "should generate an Image with a numerical parameter" do
117
119
  imgkit = IMGKit.new('html', :quality => 50)
118
120
  img = imgkit.to_img
119
121
  filetype_of(img).should include('JPEG')
120
122
  end
121
-
123
+
122
124
  it "should generate an Image with a symbol parameter" do
123
125
  imgkit = IMGKit.new('html', :username => 'chris')
124
126
  img = imgkit.to_img
@@ -148,7 +150,7 @@ describe IMGKit do
148
150
  imgkit.to_img
149
151
  imgkit.source.to_s.should include("<style>#{File.read(css)}</style><html>")
150
152
  end
151
-
153
+
152
154
  it "should throw an error if the source is not html and stylesheets have been added" do
153
155
  imgkit = IMGKit.new('http://google.com')
154
156
  css = File.join(SPEC_ROOT,'fixtures','example.css')
@@ -233,17 +235,17 @@ describe IMGKit do
233
235
  lambda { IMGKit.new("Hello, world").to_blah }.should raise_error(IMGKit::UnknownFormatError)
234
236
  end
235
237
  end
236
-
238
+
237
239
  context "#to_file" do
238
240
  before do
239
241
  @file_path = File.join(SPEC_ROOT,'fixtures','test.jpg')
240
242
  File.delete(@file_path) if File.exist?(@file_path)
241
243
  end
242
-
244
+
243
245
  after do
244
246
  File.delete(@file_path) if File.exist?(@file_path)
245
247
  end
246
-
248
+
247
249
  it "should create a file with the result of :to_img as content" do
248
250
  imgkit = IMGKit.new('html', :quality => 50)
249
251
  imgkit.expects(:to_img).returns('CONTENT')
@@ -264,28 +266,28 @@ describe IMGKit do
264
266
 
265
267
  it "should raise UnknownFormatError when format is unknown" do
266
268
  kit = IMGKit.new("html")
267
- lambda {
268
- kit.to_file("file.bad_format")
269
+ lambda {
270
+ kit.to_file("file.bad_format")
269
271
  }.should raise_error(IMGKit::UnknownFormatError)
270
272
  end
271
273
 
272
274
  it "should not create the file if format is unknown" do
273
275
  kit = IMGKit.new("html")
274
- kit.to_file("file.bad_format") rescue nil
276
+ kit.to_file("file.bad_format") rescue nil
275
277
  File.exist?("file.bad_format").should be_false
276
278
  end
277
279
  end
278
-
280
+
279
281
  context "security" do
280
282
  before do
281
283
  @test_path = File.join(SPEC_ROOT,'fixtures','security-oops')
282
284
  File.delete(@test_path) if File.exist?(@test_path)
283
285
  end
284
-
286
+
285
287
  after do
286
288
  File.delete(@test_path) if File.exist?(@test_path)
287
289
  end
288
-
290
+
289
291
  it "should not allow shell injection in options" do
290
292
  imgkit = IMGKit.new('html', :password => "blah\"; touch #{@test_path} #")
291
293
  imgkit.to_img
metadata CHANGED
@@ -1,33 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: imgkit
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.7
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 3
9
- - 6
10
- version: 1.3.6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - csquared
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-24 00:00:00 -07:00
19
- default_executable:
12
+ date: 2012-09-19 00:00:00.000000000Z
20
13
  dependencies: []
21
-
22
14
  description: Uses wkhtmltoimage to create Images using HTML
23
15
  email: christopher.continanza@gmail.com
24
- executables:
16
+ executables:
25
17
  - imgkit
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
20
+ files:
31
21
  - .document
32
22
  - .gitignore
33
23
  - .rspec
@@ -52,54 +42,42 @@ files:
52
42
  - spec/source_spec.rb
53
43
  - spec/spec_helper.rb
54
44
  - spec/warning_binary
55
- has_rdoc: true
56
45
  homepage: http://rubygems.org/gems/imgkit
57
46
  licenses: []
58
-
59
- post_install_message: |
60
- ******************************************************************
61
-
62
- Now install wkhtmltoimage binaries:
63
- Global: sudo `which imgkit` --install-wkhtmltoimage
64
- rvmsudo imgkit --install-wkhtmltoimage
65
- (installs to default /usr/local/bin/wkhtmltoimage)
66
-
67
- inside RVM folder: export TO=`which imgkit | sed 's:/imgkit:/wkhtmltoimage:'` && imgkit --install-wkhtmltoimage
68
- (you'll have to configure the location of the binary if you do this!)
69
-
70
- (run imgkit --help to see more options)
71
- ******************************************************************
72
-
47
+ post_install_message: ! "******************************************************************\n\nNow
48
+ install wkhtmltoimage binaries:\nGlobal: sudo `which imgkit` --install-wkhtmltoimage\n
49
+ \ rvmsudo imgkit --install-wkhtmltoimage\n(installs to default /usr/local/bin/wkhtmltoimage)\n\ninside
50
+ RVM folder: export TO=`which imgkit | sed 's:/imgkit:/wkhtmltoimage:'` && imgkit
51
+ --install-wkhtmltoimage\n(you'll have to configure the location of the binary if
52
+ you do this!)\n\n(run imgkit --help to see more options)\n******************************************************************\n"
73
53
  rdoc_options: []
74
-
75
- require_paths:
54
+ require_paths:
76
55
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
56
+ required_ruby_version: !ruby/object:Gem::Requirement
78
57
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
84
63
  - 0
85
- version: "0"
86
- required_rubygems_version: !ruby/object:Gem::Requirement
64
+ hash: -4046794491537096375
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
66
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ segments:
93
72
  - 0
94
- version: "0"
73
+ hash: -4046794491537096375
95
74
  requirements: []
96
-
97
75
  rubyforge_project: imgkit
98
- rubygems_version: 1.6.2
76
+ rubygems_version: 1.8.10
99
77
  signing_key:
100
78
  specification_version: 3
101
79
  summary: HTML+CSS -> JPG
102
- test_files:
80
+ test_files:
103
81
  - spec/error_binary
104
82
  - spec/fixtures/example.css
105
83
  - spec/fixtures/example.html