lizard 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e61377b87baa780930681ab9a2a270f3c5446ca4
4
- data.tar.gz: 98770224e4aeb9f75ec7fc11aafe4f3f1694f184
2
+ SHA256:
3
+ metadata.gz: 63f00a1bac30a62ffd03c7cc7a0660150769272cc316a12685ef59ccd3c3c0be
4
+ data.tar.gz: 8ad60b2e0f4c93fb5c102cffabeec6b3ea3b76b1d3fcb0f8fd628ec6d19be590
5
5
  SHA512:
6
- metadata.gz: c694fd18380abcbfdf58ded15f14c6991b041a8f6eb910fedf62a118498580c117c660a5cfd2ba3710d09daf25d96d982ead0110074eed9d5d4d905f968d21ae
7
- data.tar.gz: 2998c50c993a9eceefb752da2840b77a84935082b56fa0c58b93cfb6dab89d99f18f857cdf602e35c75bf0a251d90829b9fb40b9231615dea0f21262271777b9
6
+ metadata.gz: 6a8e9d888e276ba5a5f4dda4c0f3da9f645cb412058a02b8d0036ffd00ff64d602a2e99cce0b72eff6d93054b18a749a88cf11233634e214445037a3af01c08d
7
+ data.tar.gz: 49fd5024b86b62fc7e145b750b81246cc19eedee09e49fd6f3c4c25024923050ebc4be81b1bbeacb3a2f5462af21062097063212440121496728b351c253e297
@@ -1,16 +1,20 @@
1
1
  require 'open3'
2
2
 
3
3
  module Lizard
4
-
5
4
  def self.root
6
5
  File.expand_path('../../', __FILE__)
7
6
  end
8
7
 
9
8
  def self.run_command(command, input = nil)
10
- stdin, stdout, stderr, wait_thr = Open3.popen3(command)
9
+ command = [command] unless command.is_a?(Array)
10
+ stdin, stdout, stderr, wait_thr = Open3.popen3(*command)
11
+ stdin.binmode
12
+ stdout.binmode
13
+ stderr.binmode
11
14
  stdin.write(input) if input
12
15
  stdin.close
13
16
  [stdout.read, stderr.read, wait_thr.value.to_i]
14
17
  end
15
-
16
18
  end
19
+
20
+ require 'lizard/image'
@@ -9,4 +9,13 @@ module Lizard
9
9
  class ResizeFailed < Error
10
10
  end
11
11
 
12
+ class CropFailed < Error
13
+ end
14
+
15
+ class InvalidResizeMode < Error
16
+ end
17
+
18
+ class InvalidFileType < Error
19
+ end
20
+
12
21
  end
@@ -1,11 +1,13 @@
1
- require 'tempfile'
2
- require 'lizard/color_profiles'
3
- require 'lizard/error'
4
- require 'lizard/histogram'
1
+ require "tempfile"
2
+ require "lizard/color_profiles"
3
+ require "lizard/error"
4
+ require "lizard/histogram"
5
5
 
6
6
  module Lizard
7
7
  class Image
8
8
 
9
+ TYPES = ['jpeg', 'png', 'gif']
10
+
9
11
  def self.is_image?(data)
10
12
  image = Image.new(data)
11
13
  true
@@ -18,6 +20,10 @@ module Lizard
18
20
  @properties = identify
19
21
  end
20
22
 
23
+ def inspect
24
+ "#<Lizard::Image type=#{type}, size=#{width}x#{height}, bytes=#{@data.bytesize}>"
25
+ end
26
+
21
27
  def data
22
28
  @data
23
29
  end
@@ -44,19 +50,41 @@ module Lizard
44
50
 
45
51
  def color_model
46
52
  case color_space
47
- when/CMYK/i then 'CMYK'
48
- when /RGB/i then 'RBG'
53
+ when/CMYK/i then "CMYK"
54
+ when /RGB/i then "RBG"
49
55
  else nil
50
56
  end
51
57
  end
52
58
 
53
- def resize(width, height)
54
- size = "#{width}x#{height}\\>"
55
- profile = "-profile " + Lizard::COLOR_PROFILES['RGB']
56
- if self.color_model == 'CMYK'
57
- profile = "-profile " + COLOR_PROFILES[self.color_model].to_s + " " + profile
59
+ def resize(width, height, mode = :resize_down_only, type = "jpeg")
60
+ case mode
61
+ when :default
62
+ operator = ""
63
+ when :resize_down_only
64
+ operator = ">"
65
+ when :fill
66
+ operator = "^"
67
+ when :ignore_aspect
68
+ operator = "!"
69
+ else
70
+ raise InvalidResizeMode, "#{mode} is not a valid"
58
71
  end
59
- stdout, stderr, exit_code = Lizard.run_command("convert - -flatten #{profile} -resize #{size} -", @data)
72
+
73
+ unless TYPES.include?(type)
74
+ raise InvalidFileType, "#{type} is not valid. Choose from #{TYPES.join(', ')}"
75
+ end
76
+
77
+ command = [
78
+ 'convert', '-', '-profile', Lizard::COLOR_PROFILES["RGB"], '-flatten',
79
+ '-resize', "#{width.to_i}x#{height.to_i}#{operator}",
80
+ "#{type}:-"
81
+ ]
82
+ if self.color_model == "CMYK"
83
+ command.insert(4, '-profile')
84
+ command.insert(5, COLOR_PROFILES[self.color_model].to_s)
85
+ end
86
+
87
+ stdout, stderr, exit_code = Lizard.run_command(command, @data)
60
88
  if exit_code == 0
61
89
  Image.new(stdout)
62
90
  else
@@ -64,6 +92,20 @@ module Lizard
64
92
  end
65
93
  end
66
94
 
95
+ def crop(width, height, type = "jpeg")
96
+ unless TYPES.include?(type)
97
+ raise InvalidFileType, "#{type} is not valid. Choose from #{TYPES.join(', ')}"
98
+ end
99
+
100
+ command = ['convert', '-', '-gravity', 'center', '-extent', "#{width.to_i}x#{height.to_i}", "#{type}:-"]
101
+ stdout, stderr, exit_code = Lizard.run_command(command, @data)
102
+ if exit_code == 0
103
+ Image.new(stdout)
104
+ else
105
+ raise CropFailed, "Image could not be cropped (#{stderr})"
106
+ end
107
+ end
108
+
67
109
  def histogram
68
110
  @histogram ||= Histogram.new(@data)
69
111
  end
@@ -74,7 +116,7 @@ module Lizard
74
116
  stdout, stderr, exit_code = Lizard.run_command(%Q{identify -format "Lizard||%m||%[resolution.x]x%[resolution.y]||%wx%h||%r" -}, @data)
75
117
  if exit_code == 0 && stdout =~ /\ALizard\|\|/
76
118
  _, type, resolution, size, color_space = stdout.split("||")
77
- width, height = size.split('x')
119
+ width, height = size.split("x")
78
120
  {:type => type, :resolution => resolution, :width => width, :height => height, :size => size, :color_space => color_space}
79
121
  else
80
122
  raise NotAnImage, stderr
@@ -1,3 +1,3 @@
1
1
  module Lizard
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lizard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-10 00:00:00.000000000 Z
11
+ date: 2018-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Very simple ImageMagick interface for Ruby
14
14
  email:
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.5.1
75
+ rubygems_version: 2.7.4
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: Very simple ImageMagick interface for Ruby