mooktakim-fastimage_resize 1.0.2
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/README +27 -0
- data/README.textile +100 -0
- data/Rakefile +20 -0
- data/VERSION.yml +4 -0
- data/lib/fastimage_resize.rb +137 -0
- data/test/test.rb +1 -0
- metadata +79 -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,100 @@
|
|
1
|
+
h1. FastImage Resize
|
2
|
+
|
3
|
+
h4. FastImage Resize is an extremely light solution for resizing images in ruby by using libgd
|
4
|
+
|
5
|
+
FastImage Resize will resize gifs, jpegs, and png files.
|
6
|
+
|
7
|
+
It uses resampling to get good looking results.
|
8
|
+
|
9
|
+
And it doesn't rely on installing external heavy libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).
|
10
|
+
|
11
|
+
h2. Examples
|
12
|
+
|
13
|
+
<pre>
|
14
|
+
<code>
|
15
|
+
require 'fastimage_resize'
|
16
|
+
|
17
|
+
FastImage.resize("http://stephensykes.com/images/ss.com_x.gif", "my.gif", 100, 20)
|
18
|
+
=> 1
|
19
|
+
</pre>
|
20
|
+
</code>
|
21
|
+
|
22
|
+
The file my.gif is created locally, containing the resized image.
|
23
|
+
|
24
|
+
h2. Installation
|
25
|
+
|
26
|
+
First check the requirements section below.
|
27
|
+
|
28
|
+
h4. Gem
|
29
|
+
|
30
|
+
<pre>
|
31
|
+
<code>
|
32
|
+
sudo gem install sdsykes-fastimage_resize -s http://gems.github.com
|
33
|
+
</pre>
|
34
|
+
</code>
|
35
|
+
|
36
|
+
h4. Rails
|
37
|
+
|
38
|
+
Install the gem as above, and configure it in your environment.rb file as below:
|
39
|
+
<pre>
|
40
|
+
<code>
|
41
|
+
...
|
42
|
+
Rails::Initializer.run do |config|
|
43
|
+
...
|
44
|
+
config.gem "sdsykes-fastimage_resize", :lib=>"fastimage_resize"
|
45
|
+
...
|
46
|
+
end
|
47
|
+
...
|
48
|
+
</code>
|
49
|
+
</pre>
|
50
|
+
|
51
|
+
You may also need this in your environment.rb so that the rails process puts the compiled C code in a place it can access:
|
52
|
+
|
53
|
+
<pre>
|
54
|
+
<code>
|
55
|
+
ENV['INLINEDIR'] = RAILS_ROOT + "/tmp" # for RubyInline
|
56
|
+
</code>
|
57
|
+
</pre>
|
58
|
+
|
59
|
+
Then you're off - just use FastImage.resize() in your code as in the examples.
|
60
|
+
|
61
|
+
h2. Requirements
|
62
|
+
|
63
|
+
* RubyInline
|
64
|
+
|
65
|
+
<pre>
|
66
|
+
<code>
|
67
|
+
sudo gem install RubyInline
|
68
|
+
</pre>
|
69
|
+
</code>
|
70
|
+
|
71
|
+
* FastImage
|
72
|
+
|
73
|
+
<pre>
|
74
|
+
<code>
|
75
|
+
sudo gem install sdsykes-fastimage -s http://gems.github.com
|
76
|
+
</pre>
|
77
|
+
</code>
|
78
|
+
|
79
|
+
* Libgd
|
80
|
+
|
81
|
+
See "http://www.libgd.org/":http://www.libgd.org/
|
82
|
+
|
83
|
+
Libgd is commonly available on most unix platforms, including OSX.
|
84
|
+
|
85
|
+
h2. Documentation
|
86
|
+
|
87
|
+
"http://rdoc.info/projects/sdsykes/fastimage_resize":http://rdoc.info/projects/sdsykes/fastimage_resize
|
88
|
+
|
89
|
+
h2. Caveats
|
90
|
+
|
91
|
+
Because of the way that libgd works, gif files that have transparency may not always come through with the transparency perfectly retained.
|
92
|
+
|
93
|
+
|
94
|
+
h2. Tests
|
95
|
+
|
96
|
+
You'll need to 'sudo gem install fakeweb' to be able to run the tests
|
97
|
+
|
98
|
+
h2. References
|
99
|
+
|
100
|
+
* "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,20 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "fastimage_resize"
|
7
|
+
s.summary = "FastImage Resize - Image resizing fast and simple"
|
8
|
+
s.email = "sdsykes@gmail.com"
|
9
|
+
s.homepage = "http://github.com/sdsykes/fastimage_resize"
|
10
|
+
s.description = "FastImage Rssize is an extremely light solution for resizing images in ruby by using libgd."
|
11
|
+
s.authors = ["Stephen Sykes"]
|
12
|
+
s.files = FileList["[A-Z]*", "{lib,test}/**/*"]
|
13
|
+
s.requirements << 'libgd, see www.libgd.org'
|
14
|
+
s.add_dependency('RubyInline', '>= 3.8.2')
|
15
|
+
s.add_dependency('sdsykes-fastimage', '>= 1.1.2')
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://
|
19
|
+
gems.github.com"
|
20
|
+
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,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mooktakim-fastimage_resize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Sykes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: RubyInline
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.8.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sdsykes-fastimage
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.2
|
34
|
+
version:
|
35
|
+
description: FastImage Rssize is an extremely light solution for resizing images in ruby by using libgd.
|
36
|
+
email: sdsykes@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
- README.textile
|
44
|
+
files:
|
45
|
+
- Rakefile
|
46
|
+
- README
|
47
|
+
- README.textile
|
48
|
+
- VERSION.yml
|
49
|
+
- lib/fastimage_resize.rb
|
50
|
+
- test/test.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/sdsykes/fastimage_resize
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --inline-source
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements:
|
72
|
+
- libgd, see www.libgd.org
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.2.0
|
75
|
+
signing_key:
|
76
|
+
specification_version: 2
|
77
|
+
summary: FastImage Resize - Image resizing fast and simple
|
78
|
+
test_files: []
|
79
|
+
|