workspace-media 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/workspace-media.rb +65 -0
  3. metadata +88 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd9f38a74821f07bf754d56f85f10ae86455296e
4
+ data.tar.gz: 0de1057bbcae94e5c5b4fdfbbff92ee8ef36b377
5
+ SHA512:
6
+ metadata.gz: 15e105a18bf8e188e238d13e59462c0810ac4253bb144c7f7488b558124632cdfc1f4b2afade93ca811e1870007f07f8e1c680ae51c9b3d9509ab9942ef12e82
7
+ data.tar.gz: 17662040b8d4c20b3460f124f134f5c9cc42563b3d519e7b04377e032632bb48204e036213dbc4ae83988f2888f31ea680164e12e1f9af89e8ff01a6065be002
@@ -0,0 +1,65 @@
1
+ require "piet"
2
+ require "rmagick"
3
+
4
+ module Workspace
5
+ class File
6
+ def image?
7
+ ["jpg", "jpeg", "gif", "png"].include?(extension)
8
+ end
9
+
10
+ def video?
11
+ ["mp4"].include?(extension)
12
+ end
13
+
14
+ def audio?
15
+ ["mp3"].include?(extension)
16
+ end
17
+
18
+ def image_size
19
+ image = Magick::Image.read(to_s).first
20
+ size = { width: image.columns, height: image.rows }
21
+ image.destroy!
22
+ size
23
+ end
24
+
25
+ def fit(width: nil, height: nil, quality: 85)
26
+ image = Magick::Image.read(to_s).first
27
+ width = [image.columns, image.rows].min if width.nil? and height.nil?
28
+ width = image.columns if width.nil?
29
+ height = width if height.nil?
30
+ image.change_geometry!("#{width}x^#{height}") { |cols, rows| image.thumbnail!(cols, rows) }
31
+ image.crop!(0, 0, width, height)
32
+ image.write(to_s) { self.quality = quality }
33
+ image.destroy!
34
+ self
35
+ end
36
+
37
+ def optimize!(options = {})
38
+ return false if !exists? or !image?
39
+ optimize_png!(options) if ["png"].include?(extension)
40
+ optimize_jpg!(options) if ["jpg", "jpeg"].include?(extension)
41
+ Piet.optimize(to_s, verbose: false)
42
+ self
43
+ end
44
+
45
+ private
46
+
47
+ def optimize_jpg!(image_max_width: 2048, quality: 85)
48
+ image = Magick::Image.read(to_s).first
49
+ image.change_geometry!("#{image_max_width}>x") { |cols, rows, img| img.resize!(cols, rows) } if image.columns > image_max_width
50
+ image.write(to_s) { self.quality = quality }
51
+ image.destroy!
52
+ end
53
+
54
+ def optimize_png!(image_max_width: 2048, quality: 85)
55
+ image = Magick::Image.read(to_s).first
56
+ image.change_geometry!("#{image_max_width}>x") { |cols, rows, img| img.resize!(cols, rows) } if image.columns > image_max_width
57
+ if !image.alpha? or image.resize(1, 1).pixel_color(0, 0).opacity == 0
58
+ image.format = "JPG"
59
+ rename("#{basename}.jpg")
60
+ end
61
+ image.write(to_s) { self.quality = quality }
62
+ image.destroy!
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workspace-media
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Tobias Strebitzer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: workspace
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rmagick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.16'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: piet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ description: Workspace makes it a breeze to work with files and directories
56
+ email:
57
+ - tobias.strebitzer@magloft.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - workspace-media.rb
63
+ homepage: https://github.com/magloft/workspace
64
+ licenses:
65
+ - BSD-3-Clause
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - "."
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '2.4'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.6.10
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Workspace gem extension to allow transformation and optimization of media
87
+ files
88
+ test_files: []