rscale 0.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +80 -0
- data/lib/rscale.rb +6 -2
- data/lib/rscale/configuration.rb +2 -0
- data/lib/rscale/format.rb +2 -1
- data/lib/rscale/rscale.rb +12 -1
- data/lib/rscale/uuid.rb +24 -0
- metadata +8 -5
data/README.rdoc
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
= RScale -- ImageMagick-based image processing for Ruby
|
2
|
+
|
3
|
+
This is a simple image processing library for ruby scripts based on ImageMagick terminal tool.
|
4
|
+
Allows you to define a set of image formats and its dimensions and generate thumbnails just with one call.
|
5
|
+
It does not have any other features than making thumbnails, neither it keeps the original source.
|
6
|
+
Rails 2/3 compatible.
|
7
|
+
|
8
|
+
* Main page: http://github.com/sosedoff/rscale
|
9
|
+
* ImageMagick: http://www.imagemagick.org
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
You have to install ImageMagick in order to use this library.
|
14
|
+
To check installation run this command in terminal:
|
15
|
+
|
16
|
+
identify --version
|
17
|
+
|
18
|
+
Install via gem:
|
19
|
+
|
20
|
+
gem install rscale
|
21
|
+
|
22
|
+
== Configuration
|
23
|
+
|
24
|
+
require 'rscale'
|
25
|
+
|
26
|
+
# Initial configuration
|
27
|
+
# This 'public' path has to be writable
|
28
|
+
RScale.configure do |c|
|
29
|
+
c.public = "PATH_TO_YOUR_OUTPUT_DIR"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Lets add avatar format with 3 different sizes
|
33
|
+
RScale.format :avatar do |f|
|
34
|
+
f.url = '/static/:format/:style/:uuid_dir/:uuid.jpg' # optional
|
35
|
+
f.style :small, :size => '64x64'
|
36
|
+
f.style :medium, :size => '128x128'
|
37
|
+
f.style :large, :size => '256x256'
|
38
|
+
end
|
39
|
+
|
40
|
+
# Another format, generates 100x100 PNG thumbnails
|
41
|
+
RScale.format :profile do |f|
|
42
|
+
f.url = '/:format/:style/:uuid_dir/:uuid.png' # optional
|
43
|
+
f.style :default, :size => '100x200'
|
44
|
+
end
|
45
|
+
|
46
|
+
URL parameter is just a path to store generated thumbnails, relative to public path defined in configuration block.
|
47
|
+
Available URL parameters:
|
48
|
+
|
49
|
+
* :uuid 32-byte UUID string
|
50
|
+
* :uuid_dir /xx/xx directory structure generated from uuid string
|
51
|
+
* :md5 32-byte source image MD5 checksum
|
52
|
+
* :time Unix timestamp
|
53
|
+
* :extension Original extension of source image
|
54
|
+
* :filename Original filename of source image
|
55
|
+
* :format Name of user-defined format
|
56
|
+
* :style Name of user-defined format style (ex. :small, :medium, :large)
|
57
|
+
|
58
|
+
== Usage
|
59
|
+
|
60
|
+
path = '/tmp/.....' # path to the source image
|
61
|
+
result = RScale.image_for :avatar, path
|
62
|
+
|
63
|
+
# If source file cannot be processed result will always be null
|
64
|
+
unless result.nil?
|
65
|
+
# result will contain processed thumbnails with path relative to public path
|
66
|
+
result[:small] # 64x64
|
67
|
+
result[:medium] # 128x128
|
68
|
+
result[:large] # 256x256
|
69
|
+
end
|
70
|
+
|
71
|
+
== Limitation
|
72
|
+
|
73
|
+
* No support for AmazonS3/CloudFiles
|
74
|
+
* No support for keeping source images
|
75
|
+
* Only specific image dimensions (no relative Ax? sizes)
|
76
|
+
|
77
|
+
== Authors
|
78
|
+
|
79
|
+
Dan Sosedoff, 2010
|
80
|
+
|
data/lib/rscale.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'ftools'
|
2
|
+
require 'digest'
|
3
|
+
|
2
4
|
require 'rscale/errors'
|
5
|
+
require 'rscale/uuid'
|
3
6
|
require 'rscale/configuration'
|
4
7
|
require 'rscale/format'
|
5
8
|
require 'rscale/helpers'
|
6
9
|
require 'rscale/processor'
|
7
|
-
require 'rscale/geometry'
|
10
|
+
require 'rscale/geometry'
|
11
|
+
require 'rscale/rscale'
|
data/lib/rscale/configuration.rb
CHANGED
@@ -2,10 +2,12 @@ module RScale
|
|
2
2
|
class Configuration
|
3
3
|
attr_accessor :root
|
4
4
|
attr_accessor :public
|
5
|
+
attr_accessor :uuid_system
|
5
6
|
|
6
7
|
def initialize(root=nil, public=nil)
|
7
8
|
@root = root || File.dirname(__FILE__)
|
8
9
|
@public = public || @root + '/public'
|
10
|
+
@uuid_system = !`which uuid-gen`.to_s.strip.empty?
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
data/lib/rscale/format.rb
CHANGED
@@ -6,9 +6,10 @@ module RScale
|
|
6
6
|
def initialize(name)
|
7
7
|
@name = name.to_s
|
8
8
|
@styles = {}
|
9
|
-
@url = '/:format/:style/:
|
9
|
+
@url = '/:format/:style/:uuid_dir/:uuid.:extension'
|
10
10
|
end
|
11
11
|
|
12
|
+
# Add a new style
|
12
13
|
def style(name, opts={})
|
13
14
|
raise FormatError, 'Options required!' if opts.empty?
|
14
15
|
raise FormatError, 'Options must be a Hash!' unless opts.kind_of?(Hash)
|
data/lib/rscale/rscale.rb
CHANGED
@@ -1,12 +1,21 @@
|
|
1
1
|
module RScale
|
2
|
+
extend RScale::UUID
|
3
|
+
|
2
4
|
@@config = nil
|
3
5
|
@@formats = {}
|
4
6
|
|
7
|
+
# Configure RScale
|
5
8
|
def self.configure
|
6
9
|
@@config = Configuration.new if @@config.nil?
|
7
10
|
yield @@config
|
8
11
|
end
|
9
12
|
|
13
|
+
# Get current RScale configuration
|
14
|
+
def self.config
|
15
|
+
@@config
|
16
|
+
end
|
17
|
+
|
18
|
+
# Add new format
|
10
19
|
def self.format(name)
|
11
20
|
unless @@formats.key?(name)
|
12
21
|
@@formats[name] = Format.new(name)
|
@@ -16,10 +25,12 @@ module RScale
|
|
16
25
|
end
|
17
26
|
end
|
18
27
|
|
28
|
+
# Get list of formats
|
19
29
|
def self.formats
|
20
30
|
@@formats
|
21
31
|
end
|
22
32
|
|
33
|
+
# Generate thumbnails for the image format
|
23
34
|
def self.image_for(format, file)
|
24
35
|
if @@formats.key?(format.to_sym)
|
25
36
|
fmt = @@formats[format.to_sym]
|
@@ -32,7 +43,7 @@ module RScale
|
|
32
43
|
options[:time] = Time.now.to_i unless url[':time'].nil?
|
33
44
|
options[:md5] = Digest::MD5.filedigest(file) unless url[':md5'].nil?
|
34
45
|
unless url[':uuid'].nil?
|
35
|
-
options[:uuid] =
|
46
|
+
options[:uuid] = uuid
|
36
47
|
options[:uuid_dir] = "#{options[:uuid][0,2]}/#{options[:uuid][2,2]}"
|
37
48
|
end
|
38
49
|
|
data/lib/rscale/uuid.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module RScale
|
2
|
+
module UUID
|
3
|
+
# Generate uuid based on current configuration
|
4
|
+
def uuid
|
5
|
+
RScale.config.uuid_system ? uuid_system : uuid_local
|
6
|
+
end
|
7
|
+
|
8
|
+
# Generate pseudo-unique string if local system does not have uuid binary installed
|
9
|
+
def uuid_local
|
10
|
+
values = [
|
11
|
+
rand(0x0010000), rand(0x0010000),
|
12
|
+
rand(0x0010000), rand(0x0010000),
|
13
|
+
rand(0x0010000), rand(0x1000000),
|
14
|
+
rand(0x1000000),
|
15
|
+
]
|
16
|
+
"%04x%04x%04x%04x%04x%06x%06x" % values
|
17
|
+
end
|
18
|
+
|
19
|
+
# Generate uuid using system binary
|
20
|
+
def uuid_system
|
21
|
+
`uuidgen`.strip.gsub(/-/,'')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rscale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 31
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Dan Sosedoff
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-01-06 00:00:00 -06:00
|
18
19
|
default_executable:
|
19
20
|
dependencies: []
|
20
21
|
|
@@ -30,11 +31,13 @@ files:
|
|
30
31
|
- lib/rscale.rb
|
31
32
|
- lib/rscale/rscale.rb
|
32
33
|
- lib/rscale/errors.rb
|
34
|
+
- lib/rscale/uuid.rb
|
33
35
|
- lib/rscale/configuration.rb
|
34
36
|
- lib/rscale/format.rb
|
35
37
|
- lib/rscale/helpers.rb
|
36
38
|
- lib/rscale/processor.rb
|
37
39
|
- lib/rscale/geometry.rb
|
40
|
+
- README.rdoc
|
38
41
|
has_rdoc: true
|
39
42
|
homepage: http://github.com/sosedoff/rscale
|
40
43
|
licenses: []
|
@@ -65,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
68
|
requirements: []
|
66
69
|
|
67
70
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.
|
71
|
+
rubygems_version: 1.4.1
|
69
72
|
signing_key:
|
70
73
|
specification_version: 3
|
71
74
|
summary: Image scaling wrapper based on ImageMagick console utils
|