thumbnailer 1.3.0 → 1.4.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.
- data/lib/thumbnailer.rb +82 -8
- metadata +2 -2
data/lib/thumbnailer.rb
CHANGED
|
@@ -35,6 +35,15 @@ require 'exifr'
|
|
|
35
35
|
# f.puts ThumbNailer.box_with_background('happytree.jpg', 125, 0.8, 0.2, 0.7)
|
|
36
36
|
# f.close
|
|
37
37
|
#
|
|
38
|
+
# Whatever colour is used for transparancy there is always a chance that
|
|
39
|
+
# it is used in a picture and that the resulting thumbnail will look like
|
|
40
|
+
# Swiss Cheese. The alternative is to use the box_calc_transparancy method
|
|
41
|
+
# which will search the image for an unused colour to use for the transparancy
|
|
42
|
+
# colour. As this is extra work over and above that done by box_with_transparancy
|
|
43
|
+
# the method is a little slower. However the search is conducted over the scaled
|
|
44
|
+
# image and not the original source so if your thumbnails are small (125 pixels
|
|
45
|
+
# or less) then you can still use this method on the fly.
|
|
46
|
+
#
|
|
38
47
|
# A few extra function that I have found useful have been added to the
|
|
39
48
|
# package just so everything is in one place.
|
|
40
49
|
#
|
|
@@ -48,7 +57,7 @@ require 'exifr'
|
|
|
48
57
|
# image if either width or height is larger than +size+.
|
|
49
58
|
|
|
50
59
|
class ThumbNailer
|
|
51
|
-
VERSION = '1.
|
|
60
|
+
VERSION = '1.4.0'
|
|
52
61
|
|
|
53
62
|
# The default colour, an impure black to be used for
|
|
54
63
|
# transparency or background
|
|
@@ -82,6 +91,35 @@ class ThumbNailer
|
|
|
82
91
|
return create_output(thumbnail, source)
|
|
83
92
|
end
|
|
84
93
|
|
|
94
|
+
# Like box_with_transparancy this method scales the image within
|
|
95
|
+
# a box of +size+ width and height. This method searhes the image
|
|
96
|
+
# for an unused color and selects that for transparency. If no
|
|
97
|
+
# unused colour is found then it uses the default or given colour.
|
|
98
|
+
#
|
|
99
|
+
# Given that it must find all the colours that are used in an image
|
|
100
|
+
# to find just one unused colour this is much slower than the plain
|
|
101
|
+
# box_with_transparancy. However the search for an unused colour is
|
|
102
|
+
# conducted against the scaled image so it will not necessarily be
|
|
103
|
+
# as bad as you might imagine.
|
|
104
|
+
def self.box_calc_transparancy(source, size, r = @@r, g = @@g, b = @@b)
|
|
105
|
+
image = GD2::Image.import(source)
|
|
106
|
+
|
|
107
|
+
dstX, dstY, dstW, dstH, image = resize_source(image, size)
|
|
108
|
+
|
|
109
|
+
r, g, b = find_unused_color(image, r, g, b)
|
|
110
|
+
|
|
111
|
+
thumbnail = GD2::Image.new(size, size)
|
|
112
|
+
thumbnail.transparent = GD2::Color[r, g, b, GD2::ALPHA_TRANSPARENT]
|
|
113
|
+
thumbnail.draw do |canvas|
|
|
114
|
+
canvas.color = GD2::Color[r, g, b]
|
|
115
|
+
canvas.point(1,1)
|
|
116
|
+
canvas.fill()
|
|
117
|
+
end
|
|
118
|
+
thumbnail.copy_from(image,dstX,dstY,0,0,dstW,dstH)
|
|
119
|
+
|
|
120
|
+
return create_output(thumbnail, source)
|
|
121
|
+
end
|
|
122
|
+
|
|
85
123
|
# Given the filename of an image and the required size
|
|
86
124
|
# of the thumbnail this will scale and center the image
|
|
87
125
|
# to fit within the box against the background colour.
|
|
@@ -206,14 +244,14 @@ class ThumbNailer
|
|
|
206
244
|
|
|
207
245
|
def self.resize_source(image, size)
|
|
208
246
|
# Save us from having to put to_f on the end all the time
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
247
|
+
iw = image.width.to_f
|
|
248
|
+
ih = image.height.to_f
|
|
249
|
+
is = size.to_f
|
|
212
250
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
251
|
+
dstX = 0
|
|
252
|
+
dstY = 0
|
|
253
|
+
dstW = 0
|
|
254
|
+
dstH = 0
|
|
217
255
|
|
|
218
256
|
if iw < is and ih < is
|
|
219
257
|
# Image is smaller than the box
|
|
@@ -277,4 +315,40 @@ class ThumbNailer
|
|
|
277
315
|
def self.degrees_to_rad(degrees)
|
|
278
316
|
return (degrees * (Math::PI / 180.0))
|
|
279
317
|
end
|
|
318
|
+
|
|
319
|
+
# Convert the rgb into an integer
|
|
320
|
+
def self.rgb_to_i(r, g, b)
|
|
321
|
+
return (r * 65536) + (g * 256) + b
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Convert an integer into rgb
|
|
325
|
+
def self.i_to_rgb(i)
|
|
326
|
+
b = i % 256
|
|
327
|
+
i -= b
|
|
328
|
+
i /= 256
|
|
329
|
+
g = i % 256
|
|
330
|
+
i -= g
|
|
331
|
+
i /= 256
|
|
332
|
+
r = i
|
|
333
|
+
|
|
334
|
+
return r,g,b
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def self.find_unused_color(image, r, g, b)
|
|
338
|
+
x = Array.new(256*256*256,0)
|
|
339
|
+
|
|
340
|
+
image.each do |row|
|
|
341
|
+
row.each do |pixel|
|
|
342
|
+
rgb = image.pixel2color(pixel)
|
|
343
|
+
x[rgb_to_i(rgb.r, rgb.g, rgb.b)] += 1
|
|
344
|
+
end
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
y = x.index(0)
|
|
348
|
+
if y != nil
|
|
349
|
+
return i_to_rgb(y)
|
|
350
|
+
else
|
|
351
|
+
return r, g, b
|
|
352
|
+
end
|
|
353
|
+
end
|
|
280
354
|
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.
|
|
4
|
+
version: 1.4.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: 2009-01-
|
|
12
|
+
date: 2009-01-12 00:00:00 +00:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|