html2img 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 791e6e996cee67f7a04b4e4068e970d1b14ba7a2
4
+ data.tar.gz: 007e585f07062839704e5a17ddb6360c32b37d14
5
+ SHA512:
6
+ metadata.gz: 0f24976559d9752f6de183b2ff4ec445ba4a5e295388d53667e338181c4dc413e00928cbb5cd5638a79bcf0f431133408c7e7841c4fc11edab7dd4dd5d59c3f8
7
+ data.tar.gz: ad2c75e633f3e84633696ce9944f415f75a9e680ccd8158392009796675166f332ce5129de49428a896dfc58b4d83f6e3c00963a70df8793f363f992e9d3cbb6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in html2img.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 edmothershaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Html2img
2
+
3
+ This gem enables you to download HTML elements at the click of a button. Requires JQuery.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'html2img'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install html2img
20
+
21
+ ## Usage
22
+
23
+ On the button that you want to start the download with add in the HTML of the element the class ```html2img-#download-#<id>``` where <id> is the id attribute of the element you want to download.
24
+
25
+ An example
26
+ ```html
27
+ <div id="block" style="height: 200px; width: 200px; background-color: pink;"></div>
28
+
29
+ <input type="button" class="html2img-#download-#block btn" value="Download"/>
30
+
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/edmothershaw/html2img. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1 @@
1
+ (function(global){"use strict";var _Base64=global.Base64;var version="2.1.9";var buffer;if(typeof module!=="undefined"&&module.exports){try{buffer=require("buffer").Buffer}catch(err){}}var b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64tab=function(bin){var t={};for(var i=0,l=bin.length;i<l;i++)t[bin.charAt(i)]=i;return t}(b64chars);var fromCharCode=String.fromCharCode;var cb_utob=function(c){if(c.length<2){var cc=c.charCodeAt(0);return cc<128?c:cc<2048?fromCharCode(192|cc>>>6)+fromCharCode(128|cc&63):fromCharCode(224|cc>>>12&15)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}else{var cc=65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320);return fromCharCode(240|cc>>>18&7)+fromCharCode(128|cc>>>12&63)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}};var re_utob=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob=function(u){return u.replace(re_utob,cb_utob)};var cb_encode=function(ccc){var padlen=[0,2,1][ccc.length%3],ord=ccc.charCodeAt(0)<<16|(ccc.length>1?ccc.charCodeAt(1):0)<<8|(ccc.length>2?ccc.charCodeAt(2):0),chars=[b64chars.charAt(ord>>>18),b64chars.charAt(ord>>>12&63),padlen>=2?"=":b64chars.charAt(ord>>>6&63),padlen>=1?"=":b64chars.charAt(ord&63)];return chars.join("")};var btoa=global.btoa?function(b){return global.btoa(b)}:function(b){return b.replace(/[\s\S]{1,3}/g,cb_encode)};var _encode=buffer?function(u){return(u.constructor===buffer.constructor?u:new buffer(u)).toString("base64")}:function(u){return btoa(utob(u))};var encode=function(u,urisafe){return!urisafe?_encode(String(u)):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=new RegExp(["[À-ß][€-¿]","[à-ï][€-¿]{2}","[ð-÷][€-¿]{3}"].join("|"),"g");var cb_btou=function(cccc){switch(cccc.length){case 4:var cp=(7&cccc.charCodeAt(0))<<18|(63&cccc.charCodeAt(1))<<12|(63&cccc.charCodeAt(2))<<6|63&cccc.charCodeAt(3),offset=cp-65536;return fromCharCode((offset>>>10)+55296)+fromCharCode((offset&1023)+56320);case 3:return fromCharCode((15&cccc.charCodeAt(0))<<12|(63&cccc.charCodeAt(1))<<6|63&cccc.charCodeAt(2));default:return fromCharCode((31&cccc.charCodeAt(0))<<6|63&cccc.charCodeAt(1))}};var btou=function(b){return b.replace(re_btou,cb_btou)};var cb_decode=function(cccc){var len=cccc.length,padlen=len%4,n=(len>0?b64tab[cccc.charAt(0)]<<18:0)|(len>1?b64tab[cccc.charAt(1)]<<12:0)|(len>2?b64tab[cccc.charAt(2)]<<6:0)|(len>3?b64tab[cccc.charAt(3)]:0),chars=[fromCharCode(n>>>16),fromCharCode(n>>>8&255),fromCharCode(n&255)];chars.length-=[0,0,2,1][padlen];return chars.join("")};var atob=global.atob?function(a){return global.atob(a)}:function(a){return a.replace(/[\s\S]{1,4}/g,cb_decode)};var _decode=buffer?function(a){return(a.constructor===buffer.constructor?a:new buffer(a,"base64")).toString()}:function(a){return btou(atob(a))};var decode=function(a){return _decode(String(a).replace(/[-_]/g,function(m0){return m0=="-"?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))};var noConflict=function(){var Base64=global.Base64;global.Base64=_Base64;return Base64};global.Base64={VERSION:version,atob:atob,btoa:btoa,fromBase64:decode,toBase64:encode,utob:utob,encode:encode,encodeURI:encodeURI,btou:btou,decode:decode,noConflict:noConflict};if(typeof Object.defineProperty==="function"){var noEnum=function(v){return{value:v,enumerable:false,writable:true,configurable:true}};global.Base64.extendString=function(){Object.defineProperty(String.prototype,"fromBase64",noEnum(function(){return decode(this)}));Object.defineProperty(String.prototype,"toBase64",noEnum(function(urisafe){return encode(this,urisafe)}));Object.defineProperty(String.prototype,"toBase64URI",noEnum(function(){return encode(this,true)}))}}if(global["Meteor"]){Base64=global.Base64}})(this);
@@ -0,0 +1,265 @@
1
+ /**
2
+ * covert canvas to image
3
+ * and save the image file
4
+ */
5
+
6
+ var Canvas2Image = function () {
7
+
8
+ // check if support sth.
9
+ var $support = function () {
10
+ var canvas = document.createElement('canvas'),
11
+ ctx = canvas.getContext('2d');
12
+
13
+ return {
14
+ canvas: !!ctx,
15
+ imageData: !!ctx.getImageData,
16
+ dataURL: !!canvas.toDataURL,
17
+ btoa: !!window.btoa
18
+ };
19
+ }();
20
+
21
+ var downloadMime = 'image/octet-stream';
22
+
23
+ function scaleCanvas (canvas, width, height) {
24
+ var w = canvas.width,
25
+ h = canvas.height;
26
+ if (width == undefined) {
27
+ width = w;
28
+ }
29
+ if (height == undefined) {
30
+ height = h;
31
+ }
32
+
33
+ var retCanvas = document.createElement('canvas');
34
+ var retCtx = retCanvas.getContext('2d');
35
+ retCanvas.width = width;
36
+ retCanvas.height = height;
37
+ retCtx.drawImage(canvas, 0, 0, w, h, 0, 0, width, height);
38
+ return retCanvas;
39
+ }
40
+
41
+ function getDataURL (canvas, type, width, height) {
42
+ canvas = scaleCanvas(canvas, width, height);
43
+ return canvas.toDataURL(type);
44
+ }
45
+
46
+ function saveFile (strData) {
47
+ document.location.href = strData;
48
+ }
49
+
50
+ function genImage(strData) {
51
+ var img = document.createElement('img');
52
+ img.src = strData;
53
+ return img;
54
+ }
55
+ function fixType (type) {
56
+ type = type.toLowerCase().replace(/jpg/i, 'jpeg');
57
+ var r = type.match(/png|jpeg|bmp|gif/)[0];
58
+ return 'image/' + r;
59
+ }
60
+ function encodeData (data) {
61
+ if (!window.btoa) { throw 'btoa undefined' }
62
+ var str = '';
63
+ if (typeof data == 'string') {
64
+ str = data;
65
+ } else {
66
+ for (var i = 0; i < data.length; i ++) {
67
+ str += String.fromCharCode(data[i]);
68
+ }
69
+ }
70
+
71
+ return btoa(str);
72
+ }
73
+ function getImageData (canvas) {
74
+ var w = canvas.width,
75
+ h = canvas.height;
76
+ return canvas.getContext('2d').getImageData(0, 0, w, h);
77
+ }
78
+ function makeURI (strData, type) {
79
+ return 'data:' + type + ';base64,' + strData;
80
+ }
81
+
82
+
83
+ /**
84
+ * create bitmap image
85
+ * 按照规则生成图片响应头和响应体
86
+ */
87
+ var genBitmapImage = function (oData) {
88
+
89
+ //
90
+ // BITMAPFILEHEADER: http://msdn.microsoft.com/en-us/library/windows/desktop/dd183374(v=vs.85).aspx
91
+ // BITMAPINFOHEADER: http://msdn.microsoft.com/en-us/library/dd183376.aspx
92
+ //
93
+
94
+ var biWidth = oData.width;
95
+ var biHeight = oData.height;
96
+ var biSizeImage = biWidth * biHeight * 3;
97
+ var bfSize = biSizeImage + 54; // total header size = 54 bytes
98
+
99
+ //
100
+ // typedef struct tagBITMAPFILEHEADER {
101
+ // WORD bfType;
102
+ // DWORD bfSize;
103
+ // WORD bfReserved1;
104
+ // WORD bfReserved2;
105
+ // DWORD bfOffBits;
106
+ // } BITMAPFILEHEADER;
107
+ //
108
+ var BITMAPFILEHEADER = [
109
+ // WORD bfType -- The file type signature; must be "BM"
110
+ 0x42, 0x4D,
111
+ // DWORD bfSize -- The size, in bytes, of the bitmap file
112
+ bfSize & 0xff, bfSize >> 8 & 0xff, bfSize >> 16 & 0xff, bfSize >> 24 & 0xff,
113
+ // WORD bfReserved1 -- Reserved; must be zero
114
+ 0, 0,
115
+ // WORD bfReserved2 -- Reserved; must be zero
116
+ 0, 0,
117
+ // DWORD bfOffBits -- The offset, in bytes, from the beginning of the BITMAPFILEHEADER structure to the bitmap bits.
118
+ 54, 0, 0, 0
119
+ ];
120
+
121
+ //
122
+ // typedef struct tagBITMAPINFOHEADER {
123
+ // DWORD biSize;
124
+ // LONG biWidth;
125
+ // LONG biHeight;
126
+ // WORD biPlanes;
127
+ // WORD biBitCount;
128
+ // DWORD biCompression;
129
+ // DWORD biSizeImage;
130
+ // LONG biXPelsPerMeter;
131
+ // LONG biYPelsPerMeter;
132
+ // DWORD biClrUsed;
133
+ // DWORD biClrImportant;
134
+ // } BITMAPINFOHEADER, *PBITMAPINFOHEADER;
135
+ //
136
+ var BITMAPINFOHEADER = [
137
+ // DWORD biSize -- The number of bytes required by the structure
138
+ 40, 0, 0, 0,
139
+ // LONG biWidth -- The width of the bitmap, in pixels
140
+ biWidth & 0xff, biWidth >> 8 & 0xff, biWidth >> 16 & 0xff, biWidth >> 24 & 0xff,
141
+ // LONG biHeight -- The height of the bitmap, in pixels
142
+ biHeight & 0xff, biHeight >> 8 & 0xff, biHeight >> 16 & 0xff, biHeight >> 24 & 0xff,
143
+ // WORD biPlanes -- The number of planes for the target device. This value must be set to 1
144
+ 1, 0,
145
+ // WORD biBitCount -- The number of bits-per-pixel, 24 bits-per-pixel -- the bitmap
146
+ // has a maximum of 2^24 colors (16777216, Truecolor)
147
+ 24, 0,
148
+ // DWORD biCompression -- The type of compression, BI_RGB (code 0) -- uncompressed
149
+ 0, 0, 0, 0,
150
+ // DWORD biSizeImage -- The size, in bytes, of the image. This may be set to zero for BI_RGB bitmaps
151
+ biSizeImage & 0xff, biSizeImage >> 8 & 0xff, biSizeImage >> 16 & 0xff, biSizeImage >> 24 & 0xff,
152
+ // LONG biXPelsPerMeter, unused
153
+ 0,0,0,0,
154
+ // LONG biYPelsPerMeter, unused
155
+ 0,0,0,0,
156
+ // DWORD biClrUsed, the number of color indexes of palette, unused
157
+ 0,0,0,0,
158
+ // DWORD biClrImportant, unused
159
+ 0,0,0,0
160
+ ];
161
+
162
+ var iPadding = (4 - ((biWidth * 3) % 4)) % 4;
163
+
164
+ var aImgData = oData.data;
165
+
166
+ var strPixelData = '';
167
+ var biWidth4 = biWidth<<2;
168
+ var y = biHeight;
169
+ var fromCharCode = String.fromCharCode;
170
+
171
+ do {
172
+ var iOffsetY = biWidth4*(y-1);
173
+ var strPixelRow = '';
174
+ for (var x = 0; x < biWidth; x++) {
175
+ var iOffsetX = x<<2;
176
+ strPixelRow += fromCharCode(aImgData[iOffsetY+iOffsetX+2]) +
177
+ fromCharCode(aImgData[iOffsetY+iOffsetX+1]) +
178
+ fromCharCode(aImgData[iOffsetY+iOffsetX]);
179
+ }
180
+
181
+ for (var c = 0; c < iPadding; c++) {
182
+ strPixelRow += String.fromCharCode(0);
183
+ }
184
+
185
+ strPixelData += strPixelRow;
186
+ } while (--y);
187
+
188
+ var strEncoded = encodeData(BITMAPFILEHEADER.concat(BITMAPINFOHEADER)) + encodeData(strPixelData);
189
+
190
+ return strEncoded;
191
+ };
192
+
193
+ /**
194
+ * saveAsImage
195
+ * @param canvasElement
196
+ * @param {String} image type
197
+ * @param {Number} [optional] png width
198
+ * @param {Number} [optional] png height
199
+ */
200
+ var saveAsImage = function (canvas, width, height, type) {
201
+ if ($support.canvas && $support.dataURL) {
202
+ if (typeof canvas == "string") { canvas = document.getElementById(canvas); }
203
+ if (type == undefined) { type = 'png'; }
204
+ type = fixType(type);
205
+ if (/bmp/.test(type)) {
206
+ var data = getImageData(scaleCanvas(canvas, width, height));
207
+ var strData = genBitmapImage(data);
208
+ saveFile(makeURI(strData, downloadMime));
209
+ } else {
210
+ var strData = getDataURL(canvas, type, width, height);
211
+ saveFile(strData.replace(type, downloadMime));
212
+ }
213
+ }
214
+ };
215
+
216
+ var convertToImage = function (canvas, width, height, type) {
217
+ if ($support.canvas && $support.dataURL) {
218
+ if (typeof canvas == "string") { canvas = document.getElementById(canvas); }
219
+ if (type == undefined) { type = 'png'; }
220
+ type = fixType(type);
221
+
222
+ if (/bmp/.test(type)) {
223
+ var data = getImageData(scaleCanvas(canvas, width, height));
224
+ var strData = genBitmapImage(data);
225
+ return genImage(makeURI(strData, 'image/bmp'));
226
+ } else {
227
+ var strData = getDataURL(canvas, type, width, height);
228
+ return genImage(strData);
229
+ }
230
+ }
231
+ };
232
+
233
+
234
+
235
+ return {
236
+ saveAsImage: saveAsImage,
237
+ saveAsPNG: function (canvas, width, height) {
238
+ return saveAsImage(canvas, width, height, 'png');
239
+ },
240
+ saveAsJPEG: function (canvas, width, height) {
241
+ return saveAsImage(canvas, width, height, 'jpeg');
242
+ },
243
+ saveAsGIF: function (canvas, width, height) {
244
+ return saveAsImage(canvas, width, height, 'gif');
245
+ },
246
+ saveAsBMP: function (canvas, width, height) {
247
+ return saveAsImage(canvas, width, height, 'bmp');
248
+ },
249
+
250
+ convertToImage: convertToImage,
251
+ convertToPNG: function (canvas, width, height) {
252
+ return convertToImage(canvas, width, height, 'png');
253
+ },
254
+ convertToJPEG: function (canvas, width, height) {
255
+ return convertToImage(canvas, width, height, 'jpeg');
256
+ },
257
+ convertToGIF: function (canvas, width, height) {
258
+ return convertToImage(canvas, width, height, 'gif');
259
+ },
260
+ convertToBMP: function (canvas, width, height) {
261
+ return convertToImage(canvas, width, height, 'bmp');
262
+ }
263
+ };
264
+
265
+ }();