rsqip 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f38434f30d936a77fa64cd8cb8779fcd0d77f78c
4
+ data.tar.gz: 640b40ebe94daf1e385aa0bcff9854d76a42c51b
5
+ SHA512:
6
+ metadata.gz: b6c0707a0f90c3538ead49f4327466f08d1cf338c687ebd1a0548a3b0825908c9a8e3d614dff80701b298220cc0681822fd2f8ffbd3b216b2d52b5751c3dcd14
7
+ data.tar.gz: a7f39a6b1b4b2d05e5a8828a4789bcb93100e9c3aaf3606dddbf3843a3139b59bd6bf5162d033a0c82b7fcc0d1184f77f9cae0fb20f8df05cd7c5d70918722ea
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ RSQIP - SVG-Based Image Placeholder... in ruby
2
+ ====================
3
+ ## Overview
4
+
5
+ Ruby port of npm's [sqip](https://github.com/technopagan/sqip) wrapping primitive and svgo
6
+
7
+
8
+ ## Requirements
9
+ * Primitive (https://github.com/fogleman/primitive)
10
+ * SVGO https://github.com/svg/svgo
11
+
12
+ ## Installation
13
+
14
+ **TODO**
15
+
16
+ ```bash
17
+ npm install -g svgo
18
+ go get -u github.com/fogleman/primitive
19
+
20
+ ```
21
+
22
+ ## Examples
23
+
24
+ **TODO**
25
+
26
+ ```ruby
27
+ rsqip = Rsqip.new(filename).run
28
+ width, height = rsqip.img_dimensions
29
+ style = "background-size: cover; background-image: url(data:image/svg+xml;base64,${rsqip.svg_base64encoded});"
30
+ html = %(<img width="#{width}" height="#{height}" src="#{filename}" style="#{style}" alt="Add descriptive alt text">)
31
+ ```
32
+
33
+ ## CLI Examples
34
+
35
+ just use npm
36
+
37
+ ## Licence
38
+
39
+ This is free and unencumbered software released into the public domain.
data/UNLICENSE.txt ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
@@ -0,0 +1,3 @@
1
+ module Rsqip
2
+ VERSION = '0.0.1'.freeze
3
+ end
data/lib/rsqip.rb ADDED
@@ -0,0 +1,113 @@
1
+ require 'tempfile'
2
+ require 'base64'
3
+ require 'dimensions'
4
+
5
+ class Rsqip
6
+ # Example:
7
+ # >> Rsqip.new('/tmp/picture.png').run.final_svg
8
+ # => <svg ...
9
+ #
10
+ # >> Rsqip.new('/tmp/picture.png').run.svg_base64encoded
11
+ # => PHN2ZyB4bWxucz0ia ...
12
+ #
13
+ # >> Rsqip.new('/tmp/picture.png').run.as_hash
14
+ # => { img_dimensions: [300, 100],
15
+ # final_svg: '<?xml version="1.0"?><svg ...',
16
+ # svg_base64encoded: 'PHN2ZyB4bWxucz0ia ...' }
17
+ #
18
+ def initialize(input_filename, number_of_primitives: 8)
19
+ @input_filename = input_filename
20
+ @number_of_primitives = number_of_primitives
21
+
22
+ unless system('type', 'primitive')
23
+ raise 'Please ensure that Primitive (https://github.com/fogleman/primitive, written in Golang) is installed and globally available'
24
+ end
25
+
26
+ unless system('type', 'svgo')
27
+ raise 'Please ensure that Svgo (https://github.com/svg/svgo, written in Nodejs) is installed and globally available'
28
+ end
29
+ end
30
+
31
+ def run
32
+ @primitive_output_filename = new_tmpname(@input_filename) + '.svg'
33
+ run_primitive(@input_filename,
34
+ @primitive_output_filename,
35
+ @number_of_primitives,
36
+ geometry_of(*img_dimensions))
37
+
38
+ @svgo_output_filename = new_tmpname(@input_filename) + '.svg'
39
+ run_svgo(@primitive_output_filename, @svgo_output_filename)
40
+
41
+ self
42
+ end
43
+
44
+ def img_dimensions
45
+ @img_dimensions ||= Dimensions.dimensions(@input_filename)
46
+ end
47
+
48
+ def final_svg
49
+ @final_svg ||= replace_attrs(File.read(@svgo_output_filename), *img_dimensions)
50
+ end
51
+
52
+ def svg_base64encoded
53
+ @svg_base64encoded ||= Base64.encode64(@final_svg).delete("\n")
54
+ end
55
+
56
+ def as_hash
57
+ { img_dimensions: img_dimensions,
58
+ final_svg: final_svg,
59
+ svg_base64encoded: svg_base64encoded }
60
+ end
61
+
62
+ # filenames and picture size
63
+ # utilities
64
+ #
65
+ def new_tmpname(filename)
66
+ Dir::Tmpname.make_tmpname(*split_basename_extension(filename))
67
+ end
68
+
69
+ def split_basename_extension(filename)
70
+ fparts = filename.split('/').last.split('.')
71
+ [fparts[0..-2].join('.'), fparts.last]
72
+ end
73
+
74
+ def geometry_of(width, height)
75
+ width > height ? width : height
76
+ end
77
+
78
+ # binary interfaces
79
+ # to primitive and svgo
80
+ #
81
+ def run_primitive(input, output, number_of_primitives, size)
82
+ system('primitive',
83
+ '-i', input,
84
+ '-o', output,
85
+ '-n', number_of_primitives.to_s,
86
+ '-m', '0',
87
+ '-s', size.to_s)
88
+ end
89
+
90
+ def run_svgo(input, ouput)
91
+ system('svgo',
92
+ input,
93
+ '--multipass',
94
+ '-p', '1',
95
+ '-o', ouput)
96
+ end
97
+
98
+ # Add viewbox and preserveAspectRatio attributes
99
+ # as well as a Gaussian Blur filter to the SVG
100
+ #
101
+ # original comment:
102
+ # We initially worked with a proper DOM parser to
103
+ # manipulate the SVG's XML, but it was very opinionated
104
+ # about SVG syntax and kept introducing unwanted tags.
105
+ # So we had to resort to RegEx replacements
106
+ def replace_attrs(svg, width, height)
107
+ ratio = '<svg xmlns="http://www.w3.org/2000/svg"' +
108
+ %(viewBox="0 0 #{width} #{height}">)
109
+ blur = '<filter id="b"><feGaussianBlur stdDeviation="12" /></filter>'
110
+ svg.sub(/(<svg)(.*?)(>)/, ratio + blur)
111
+ .gsub(/(<g)/, '<g filter="url(#b)"')
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsqip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Guilhem Augendre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dimensions
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ description: Ruby port of SVG LQIP
70
+ email: guilhem@augendre.fr
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - README.md
76
+ - UNLICENSE.txt
77
+ - lib/rsqip.rb
78
+ - lib/rsqip/version.rb
79
+ homepage: http://rubygems.org/gems/rsqip
80
+ licenses:
81
+ - UNLICENSE
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.14
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: ruby sqip
103
+ test_files: []