thumbnailer 1.1.1 → 1.2.0

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.
Files changed (3) hide show
  1. data/Rakefile +1 -0
  2. data/lib/thumbnailer.rb +47 -6
  3. metadata +3 -2
data/Rakefile CHANGED
@@ -16,6 +16,7 @@ spec = Gem::Specification.new do |s|
16
16
 
17
17
  s.files = FileList['README', 'COPY*', 'Rakefile', 'lib/**/*.rb']
18
18
  s.requirements << 'gd2'
19
+ s.requirements << 'exifr'
19
20
  s.test_files = FileList['test/*.rb']
20
21
 
21
22
  s.has_rdoc = true
data/lib/thumbnailer.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  require 'gd2'
2
+ require 'exifr'
2
3
 
3
4
  # Scales the given image to fit within a box of the required
4
5
  # size with either a transparent background or one specified
5
- # by the user.
6
+ # by the user. Should the source image be a JPEG with the
7
+ # image orientation set in the EXIF data then the thumbnail
8
+ # will be rotated to the correct orientation.
6
9
  #
7
10
  # The return value is the thumbnail data in +png+ format. Both the
8
11
  # default colour and the output type can be changed.
@@ -33,7 +36,7 @@ require 'gd2'
33
36
  # f.close
34
37
 
35
38
  class ThumbNailer
36
- VERSION = '1.1.1'
39
+ VERSION = '1.2.0'
37
40
 
38
41
  # The default colour, an impure black to be used for
39
42
  # transparency or background
@@ -64,7 +67,7 @@ class ThumbNailer
64
67
  end
65
68
  thumbnail.copy_from(image,dstX,dstY,0,0,dstW,dstH)
66
69
 
67
- return create_output(thumbnail)
70
+ return create_output(thumbnail, source)
68
71
  end
69
72
 
70
73
  # Given the filename of an image and the required size
@@ -74,7 +77,7 @@ class ThumbNailer
74
77
  # If the image is smaller than the box then it will be
75
78
  # centered within the box as is.
76
79
  def self.box_with_background(source, size, r = @@r, g = @@g, b = @@b)
77
- image = GD2::Image.import(source)
80
+ image = get_image(source)
78
81
 
79
82
  dstX, dstY, dstW, dstH, image = resize_source(image, size)
80
83
 
@@ -86,7 +89,7 @@ class ThumbNailer
86
89
  end
87
90
  thumbnail.copy_from(image,dstX,dstY,0,0,dstW,dstH)
88
91
 
89
- return create_output(thumbnail)
92
+ return create_output(thumbnail, source)
90
93
  end
91
94
 
92
95
  # Change the output type to +png+ (default)
@@ -115,7 +118,9 @@ class ThumbNailer
115
118
 
116
119
  private
117
120
 
118
- def self.create_output(image)
121
+ def self.create_output(image, source)
122
+ image = correct_orientation(image, source)
123
+
119
124
  case @@OUTPUT_TYPE
120
125
  when 'png'
121
126
  return image.png
@@ -168,4 +173,40 @@ class ThumbNailer
168
173
 
169
174
  return dstX, dstY, dstW, dstH, image
170
175
  end
176
+
177
+ def self.correct_orientation(image, source)
178
+ orientation = 0
179
+
180
+ begin
181
+ info = EXIFR::JPEG.new(source)
182
+ orientation = info.orientation.to_i
183
+ rescue Exception => e
184
+ # Either not a jpg or has no EXIF data
185
+ end
186
+
187
+ case orientation
188
+ when 2
189
+ # ) transform="-flip horizontal";;
190
+ when 3
191
+ image.rotate!(degrees_to_rad(-180.0), image.width.to_f / 2.0, image.height.to_f / 2.0)
192
+ when 4
193
+ # ) transform="-flip vertical";;
194
+ when 5
195
+ # ) transform="-transpose";;
196
+ when 6
197
+ image.rotate!(degrees_to_rad(-90.0), image.width.to_f / 2.0, image.height.to_f / 2.0)
198
+ when 7
199
+ # ) transform="-transverse";;
200
+ when 8
201
+ image.rotate!(degrees_to_rad(-270.0), image.width.to_f / 2.0, image.height.to_f / 2.0)
202
+ else
203
+ # No rotation required
204
+ end
205
+
206
+ return image
207
+ end
208
+
209
+ def self.degrees_to_rad(degrees)
210
+ return (degrees * (Math::PI / 180.0))
211
+ end
171
212
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thumbnailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Hickman
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-27 00:00:00 +00:00
12
+ date: 2008-12-30 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -52,6 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  version:
53
53
  requirements:
54
54
  - gd2
55
+ - exifr
55
56
  rubyforge_project: thumbnailer
56
57
  rubygems_version: 1.3.1
57
58
  signing_key: