sdsykes-fastimage 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +27 -0
- data/README.textile +42 -0
- data/Rakefile +17 -0
- data/VERSION.yml +4 -0
- data/lib/fastimage_resize.rb +137 -0
- data/test/test.rb +1 -0
- metadata +60 -0
data/README
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
FastImage Resize is an extremely light solution for resizing images in ruby by using libgd
|
2
|
+
|
3
|
+
=== Examples
|
4
|
+
|
5
|
+
require 'fastimage_resize'
|
6
|
+
|
7
|
+
FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
|
8
|
+
=> 1
|
9
|
+
|
10
|
+
=== Requirements
|
11
|
+
|
12
|
+
RubyInline
|
13
|
+
|
14
|
+
sudo gem install RubyInline
|
15
|
+
|
16
|
+
FastImage
|
17
|
+
|
18
|
+
sudo gem install sdsykes-fastimage -s http://gems.github.com
|
19
|
+
|
20
|
+
Libgd
|
21
|
+
|
22
|
+
See http://www.libgd.org/
|
23
|
+
Libgd is commonly available on most unix platforms, including OSX.
|
24
|
+
|
25
|
+
=== References
|
26
|
+
|
27
|
+
* http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing
|
data/README.textile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
h1. FastImage Resize
|
2
|
+
|
3
|
+
h4. FastImage Resize is an extremely light solution for resizing images in ruby by using libgd
|
4
|
+
|
5
|
+
h2. Examples
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
<code>
|
9
|
+
require 'fastimage_resize'
|
10
|
+
|
11
|
+
FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
|
12
|
+
=> 1
|
13
|
+
</pre>
|
14
|
+
</code>
|
15
|
+
|
16
|
+
h2. Requirements
|
17
|
+
|
18
|
+
* RubyInline
|
19
|
+
|
20
|
+
<pre>
|
21
|
+
<code>
|
22
|
+
sudo gem install RubyInline
|
23
|
+
</pre>
|
24
|
+
</code>
|
25
|
+
|
26
|
+
* FastImage
|
27
|
+
|
28
|
+
<pre>
|
29
|
+
<code>
|
30
|
+
sudo gem install sdsykes-fastimage -s http://gems.github.com
|
31
|
+
</pre>
|
32
|
+
</code>
|
33
|
+
|
34
|
+
* Libgd
|
35
|
+
|
36
|
+
See "http://www.libgd.org/":http://www.libgd.org/
|
37
|
+
|
38
|
+
Libgd is commonly available on most unix platforms, including OSX.
|
39
|
+
|
40
|
+
h2. References
|
41
|
+
|
42
|
+
* "http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing":http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "fastimage"
|
7
|
+
s.summary = "FastImage - Image info fast"
|
8
|
+
s.email = "sdsykes@gmail.com"
|
9
|
+
s.homepage = "http://github.com/sdsykes/fastimage"
|
10
|
+
s.description = "FastImage finds the size or type of an image given its uri by fetching as little as needed."
|
11
|
+
s.authors = ["Stephen Sykes"]
|
12
|
+
s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://
|
16
|
+
gems.github.com"
|
17
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# FastImage Resize is an extremely light solution for resizing images in ruby by using libgd
|
2
|
+
#
|
3
|
+
# === Examples
|
4
|
+
#
|
5
|
+
# require 'fastimage_resize'
|
6
|
+
#
|
7
|
+
# FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
|
8
|
+
# => 1
|
9
|
+
#
|
10
|
+
# === Requirements
|
11
|
+
#
|
12
|
+
# RubyInline
|
13
|
+
#
|
14
|
+
# sudo gem install RubyInline
|
15
|
+
#
|
16
|
+
# FastImage
|
17
|
+
#
|
18
|
+
# sudo gem install sdsykes-fastimage -s http://gems.github.com
|
19
|
+
#
|
20
|
+
# Libgd
|
21
|
+
#
|
22
|
+
# See http://www.libgd.org/
|
23
|
+
# Libgd is commonly available on most unix platforms, including OSX.
|
24
|
+
#
|
25
|
+
# === References
|
26
|
+
#
|
27
|
+
# * http://blog.new-bamboo.co.uk/2007/12/3/super-f-simple-resizing
|
28
|
+
|
29
|
+
require 'inline'
|
30
|
+
require 'open-uri'
|
31
|
+
require 'tempfile'
|
32
|
+
require 'fastimage'
|
33
|
+
|
34
|
+
class FastImage
|
35
|
+
SUPPORTED_FORMATS = [:jpg, :png, :gif]
|
36
|
+
|
37
|
+
class FormatNotSupported < FastImageException # :nodoc:
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.resize(uri_in, file_out, w, h, options={})
|
41
|
+
jpeg_quality = options[:jpeg_quality] || -1
|
42
|
+
|
43
|
+
u = URI.parse(uri_in)
|
44
|
+
if u.scheme == "http" || u.scheme == "https" || u.scheme == "ftp"
|
45
|
+
f = Tempfile.new(name)
|
46
|
+
f.write(open(u).read)
|
47
|
+
f.close
|
48
|
+
resize_local(f.path, file_out, w, h, jpeg_quality)
|
49
|
+
File.unlink(f.path)
|
50
|
+
else
|
51
|
+
resize_local(uri_in, file_out, w, h, jpeg_quality)
|
52
|
+
end
|
53
|
+
rescue OpenURI::HTTPError, SocketError
|
54
|
+
raise ImageFetchFailure
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.resize_local(file_in, file_out, w, h, jpeg_quality)
|
58
|
+
fi = new(file_in, :raise_on_failure=>true, :type_only=>true)
|
59
|
+
type_index = SUPPORTED_FORMATS.index(fi.type)
|
60
|
+
raise FormatNotSupported unless type_index
|
61
|
+
fi.resize_image(file_in, file_out, w, h, type_index, jpeg_quality)
|
62
|
+
end
|
63
|
+
|
64
|
+
def resize_image(filename_in, filename_out, w, h, image_type, jpeg_quality); end
|
65
|
+
|
66
|
+
inline do |builder|
|
67
|
+
builder.include '"gd.h"'
|
68
|
+
builder.add_link_flags "-lgd"
|
69
|
+
|
70
|
+
builder.c <<-"END"
|
71
|
+
void resize_image(char *filename_in, char *filename_out, int w, int h, int image_type, int jpeg_quality) {
|
72
|
+
gdImagePtr im_in, im_out;
|
73
|
+
FILE *in, *out;
|
74
|
+
int trans, trans_r, trans_g, trans_b;
|
75
|
+
int x, y, f = 0;
|
76
|
+
|
77
|
+
in = fopen(filename_in, "rb");
|
78
|
+
if (!in) return;
|
79
|
+
|
80
|
+
im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */
|
81
|
+
switch(image_type) {
|
82
|
+
case 0: im_in = gdImageCreateFromJpeg(in);
|
83
|
+
break;
|
84
|
+
case 1: im_in = gdImageCreateFromPng(in);
|
85
|
+
gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */
|
86
|
+
gdImageSaveAlpha(im_out, 1);
|
87
|
+
break;
|
88
|
+
case 2: im_in = gdImageCreateFromGif(in);
|
89
|
+
trans = gdImageGetTransparent(im_in);
|
90
|
+
/* find a transparent pixel, then turn off transparency
|
91
|
+
so that it copies correctly */
|
92
|
+
if (trans >= 0) {
|
93
|
+
for (x=0; x<gdImageSX(im_in); x++) {
|
94
|
+
for (y=0; y<gdImageSY(im_in); y++) {
|
95
|
+
if (gdImageGetPixel(im_in, x, y) == trans) {
|
96
|
+
f = 1;
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
if (f) break;
|
101
|
+
}
|
102
|
+
gdImageColorTransparent(im_in, -1);
|
103
|
+
if (!f) trans = -1; /* no transparent pixel found */
|
104
|
+
}
|
105
|
+
break;
|
106
|
+
}
|
107
|
+
|
108
|
+
fclose(in);
|
109
|
+
|
110
|
+
/* Now copy the original */
|
111
|
+
gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0,
|
112
|
+
gdImageSX(im_out), gdImageSY(im_out),
|
113
|
+
gdImageSX(im_in), gdImageSY(im_in));
|
114
|
+
|
115
|
+
out = fopen(filename_out, "wb");
|
116
|
+
if (out) {
|
117
|
+
switch(image_type) {
|
118
|
+
case 0: gdImageJpeg(im_out, out, jpeg_quality);
|
119
|
+
break;
|
120
|
+
case 1: gdImagePng(im_out, out);
|
121
|
+
break;
|
122
|
+
case 2: gdImageTrueColorToPalette(im_out, 0, 256);
|
123
|
+
if (trans >= 0) {
|
124
|
+
trans = gdImageGetPixel(im_out, x, y); /* get the color index of our transparent pixel */
|
125
|
+
gdImageColorTransparent(im_out, trans); /* may not always work as hoped */
|
126
|
+
}
|
127
|
+
gdImageGif(im_out, out);
|
128
|
+
break;
|
129
|
+
}
|
130
|
+
fclose(out);
|
131
|
+
}
|
132
|
+
gdImageDestroy(im_in);
|
133
|
+
gdImageDestroy(im_out);
|
134
|
+
}
|
135
|
+
END
|
136
|
+
end
|
137
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# todo
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sdsykes-fastimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-08 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: FastImage finds the size or type of an image given its uri by fetching as little as needed.
|
17
|
+
email: sdsykes@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- README.textile
|
25
|
+
files:
|
26
|
+
- Rakefile
|
27
|
+
- README
|
28
|
+
- README.textile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/fastimage_resize.rb
|
31
|
+
- test/test.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/sdsykes/fastimage
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: FastImage - Image info fast
|
59
|
+
test_files: []
|
60
|
+
|