imagecache 0.0.1 → 0.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.
- checksums.yaml +4 -4
- data/.gitignore +8 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +46 -0
- data/README.md +35 -0
- data/Rakefile +26 -0
- data/imagecache.gemspec +24 -0
- data/lib/imagecache/backends/filesystem.rb +61 -0
- data/lib/imagecache/backends/redis.rb +35 -0
- data/lib/imagecache/backends/s3.rb +40 -0
- data/lib/imagecache/base.rb +51 -0
- data/lib/imagecache/conversions/crop.rb +9 -0
- data/lib/imagecache/conversions/fit.rb +9 -0
- data/lib/imagecache/conversions/resize.rb +20 -0
- data/lib/imagecache/conversions/round.rb +35 -0
- data/lib/imagecache/converter.rb +68 -0
- data/lib/imagecache/identify.rb +15 -0
- data/lib/imagecache/url_reader.rb +37 -0
- data/lib/imagecache/url_writer.rb +31 -0
- data/lib/imagecache/version.rb +5 -0
- data/lib/imagecache.rb +16 -0
- data/test/filesystem_backend_test.rb +43 -0
- metadata +62 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 109d109c25bfc36aedc3ea41b67ab23a66b268ef
|
4
|
+
data.tar.gz: 49481e992d4b58f29243456cd060fdfe4d45cddc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05df5371a60dc3499938e8bbcd40ec08fa587027594bc8be6b451b62629bfc91b7917aad7a98446c088b425240170ed6925485ec271c0ea9cb73041a6e57e8bb
|
7
|
+
data.tar.gz: 554f86a72f3264c3219604f472208b606fdf8acf797369bb40e8e3396e983e2264a53af8467cd401767e53d37166a3931a789eda3aee602e0b83210684812518
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
imagecache (0.0.2)
|
5
|
+
activesupport (~> 4.0, >= 4.0)
|
6
|
+
aws-sdk (~> 1.60.2, >= 1.60.2)
|
7
|
+
redis (~> 3.0, >= 3.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (4.2.4)
|
13
|
+
i18n (~> 0.7)
|
14
|
+
json (~> 1.7, >= 1.7.7)
|
15
|
+
minitest (~> 5.1)
|
16
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
17
|
+
tzinfo (~> 1.1)
|
18
|
+
aws-sdk (1.60.2)
|
19
|
+
aws-sdk-v1 (= 1.60.2)
|
20
|
+
aws-sdk-v1 (1.60.2)
|
21
|
+
json (~> 1.4)
|
22
|
+
nokogiri (>= 1.4.4)
|
23
|
+
i18n (0.7.0)
|
24
|
+
json (1.8.3)
|
25
|
+
mini_portile2 (2.1.0)
|
26
|
+
minitest (5.9.1)
|
27
|
+
nokogiri (1.6.8)
|
28
|
+
mini_portile2 (~> 2.1.0)
|
29
|
+
pkg-config (~> 1.1.7)
|
30
|
+
pkg-config (1.1.7)
|
31
|
+
rake (11.3.0)
|
32
|
+
redis (3.3.1)
|
33
|
+
thread_safe (0.3.5)
|
34
|
+
tzinfo (1.2.2)
|
35
|
+
thread_safe (~> 0.1)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
imagecache!
|
42
|
+
minitest (~> 5.9.1, >= 5.9.1)
|
43
|
+
rake (~> 11.3.0, >= 11.3.0)
|
44
|
+
|
45
|
+
BUNDLED WITH
|
46
|
+
1.13.1
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Imagecache
|
2
|
+
An image cropping and caching library written in Ruby
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
```sh
|
7
|
+
gem install imagecache
|
8
|
+
```
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
In your routes file:
|
13
|
+
```Ruby
|
14
|
+
get 'imagecache/*permalink' => 'imagecache#show', :permalink => /imagecache\/.*/
|
15
|
+
```
|
16
|
+
|
17
|
+
In your controller:
|
18
|
+
```Ruby
|
19
|
+
class ImagecacheController < ApplicationController
|
20
|
+
|
21
|
+
def show
|
22
|
+
imagecache = Imagecache::Base.new
|
23
|
+
if image = imagecache.process(params[:permalink])
|
24
|
+
redirect_to params[:permalink], status: 301
|
25
|
+
else
|
26
|
+
render text: '', status: 404
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
If you have the file `image.jpg` in your `public/assets` folder, then point
|
34
|
+
your browser to `http://example.com/imagecache/assets/ft250x250/image.jpg` to
|
35
|
+
get a cropped version of `image.jpg` that fits within a 250x250 pixel cropping.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'bundler/version'
|
4
|
+
require './lib/imagecache'
|
5
|
+
require './lib/imagecache/version'
|
6
|
+
|
7
|
+
Rake::TestTask.new do |t|
|
8
|
+
t.libs << '.' << 'lib' << 'test'
|
9
|
+
t.test_files = FileList['test/*_test.rb']
|
10
|
+
t.verbose = false
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Build the gem"
|
14
|
+
task :build do
|
15
|
+
system "gem build imagecache.gemspec"
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "install the gem"
|
19
|
+
task :install do
|
20
|
+
system "gem install imagecache-#{Imagecache::VERSION}.gem"
|
21
|
+
end
|
22
|
+
|
23
|
+
desc "Build and release the gem"
|
24
|
+
task :release => :build do
|
25
|
+
system "gem push imagecache-#{Imagecache::VERSION}.gem"
|
26
|
+
end
|
data/imagecache.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'imagecache/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'imagecache'
|
8
|
+
gem.email = 'greg@thinktopography.com'
|
9
|
+
gem.description = 'Ruby Image Cropping and Caching Library'
|
10
|
+
gem.homepage = 'https://github.com/thinktopography/imagecache'
|
11
|
+
gem.version = Imagecache::VERSION
|
12
|
+
gem.summary = 'imagecache'
|
13
|
+
gem.authors = ['Greg Kops']
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.license = 'MIT'
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_development_dependency 'rake', '~> 11.3.0', '>= 11.3.0'
|
20
|
+
gem.add_development_dependency 'minitest', '~> 5.9.1', '>= 5.9.1'
|
21
|
+
gem.add_runtime_dependency 'activesupport', '~> 4.0', '>= 4.0'
|
22
|
+
gem.add_runtime_dependency 'redis', '~> 3.0', '>= 3.0'
|
23
|
+
gem.add_runtime_dependency 'aws-sdk', '~> 1.60.2', '>= 1.60.2'
|
24
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
module Backends
|
5
|
+
class Filesystem
|
6
|
+
|
7
|
+
def initialize(root = nil)
|
8
|
+
@root = root || "#{Rails.root}/public"
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(key)
|
12
|
+
File.open(path(key)).read
|
13
|
+
end
|
14
|
+
|
15
|
+
def set(key, value)
|
16
|
+
keypath = path(key)
|
17
|
+
mkdir(keypath)
|
18
|
+
File.open(keypath, 'wb') { |file| file.write(value) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete(key)
|
22
|
+
keypath = path(key)
|
23
|
+
File.unlink(keypath)
|
24
|
+
rmdir(keypath)
|
25
|
+
end
|
26
|
+
|
27
|
+
def exists?(key)
|
28
|
+
File.exist?(path(key))
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def path(key)
|
34
|
+
"#{@root}/#{key}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def mkdir(filepath)
|
38
|
+
directories = File.dirname(filepath).split("/").drop(1)
|
39
|
+
fullpath = ""
|
40
|
+
directories.each do |directory|
|
41
|
+
fullpath += "/#{directory}"
|
42
|
+
if !Dir.exists?(fullpath)
|
43
|
+
Dir.mkdir(fullpath)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def rmdir(filepath)
|
49
|
+
directories = File.dirname(filepath).split("/")
|
50
|
+
while(directories.any?)
|
51
|
+
fullpath = directories.join("/")
|
52
|
+
if Dir["#{fullpath}/*"].empty?
|
53
|
+
Dir.delete(fullpath)
|
54
|
+
end
|
55
|
+
directories.pop
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
module Backends
|
5
|
+
class Redis
|
6
|
+
|
7
|
+
def get(key)
|
8
|
+
redis.get(key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def set(key, value)
|
12
|
+
redis.set(key, value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def delete(key)
|
16
|
+
redis.del(key)
|
17
|
+
end
|
18
|
+
|
19
|
+
def exists?(key)
|
20
|
+
redis.exists(key)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def redis
|
26
|
+
@redis ||= ::Redis.new(:host => config['host'], :port => config['port'], :db => config['cache'])
|
27
|
+
end
|
28
|
+
|
29
|
+
def config
|
30
|
+
@config ||= YAML.load_file("#{Rails.root}/config/redis.yml")[Rails.env]
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
module Backends
|
5
|
+
class S3
|
6
|
+
|
7
|
+
def get(key)
|
8
|
+
objects[key].read
|
9
|
+
end
|
10
|
+
|
11
|
+
def set(key, value)
|
12
|
+
headers = { acl: 'public-read', cache_control: 'max-age=315360000, no-transform, public' }
|
13
|
+
objects.create(key, value, headers)
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete(key)
|
17
|
+
objects[key].delete
|
18
|
+
end
|
19
|
+
|
20
|
+
def exists?(key)
|
21
|
+
objects[key].exists?
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def objects
|
27
|
+
s3.buckets[config['bucket']].objects
|
28
|
+
end
|
29
|
+
|
30
|
+
def s3
|
31
|
+
@s3 ||= AWS::S3.new(:access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'])
|
32
|
+
end
|
33
|
+
|
34
|
+
def config
|
35
|
+
@config ||= YAML.load_file("#{Rails.root}/config/aws.yml")[Rails.env]
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
class Base
|
5
|
+
|
6
|
+
def process(path)
|
7
|
+
url = url_reader.read(path)
|
8
|
+
if original = source(url.assetpath)
|
9
|
+
if converted = converter.convert(original, url.conversions)
|
10
|
+
newurl = url_writer.write(url.conversions, url.assetpath)
|
11
|
+
filesystem.set(newurl, converted.data)
|
12
|
+
return converted
|
13
|
+
end
|
14
|
+
end
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def source(path)
|
21
|
+
if filesystem.exists?(path)
|
22
|
+
filesystem.get(path)
|
23
|
+
elsif s3.exists?(path)
|
24
|
+
s3.get(path)
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def converter
|
31
|
+
@converter ||= Imagecache::Converter.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def url_reader
|
35
|
+
@url_reader ||= Imagecache::UrlReader.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def url_writer
|
39
|
+
@url_writer ||= Imagecache::UrlWriter.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def filesystem
|
43
|
+
@filesystem ||= Imagecache::Backends::Filesystem.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def s3
|
47
|
+
@s3 ||= Imagecache::Backends::S3.new
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
module Conversions
|
5
|
+
class Resize
|
6
|
+
|
7
|
+
def process(file, metadata, conversion)
|
8
|
+
width = conversion.width
|
9
|
+
height = conversion.height
|
10
|
+
command = "convert #{file.path} -resize #{width}x#{height} #{file.path}"
|
11
|
+
Rails.logger.debug(command)
|
12
|
+
output = `#{command}`
|
13
|
+
data = File.open(file.path).read
|
14
|
+
File.unlink(file.path)
|
15
|
+
data
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
module Conversions
|
5
|
+
class Round
|
6
|
+
|
7
|
+
def process(file, metadata, conversion)
|
8
|
+
shortest = [metadata.width, metadata.height].min
|
9
|
+
Rails.logger.debug(conversion.radius)
|
10
|
+
Rails.logger.debug(shortest)
|
11
|
+
radius = (conversion.radius / 100.to_f) * shortest
|
12
|
+
command = command(file.path, radius)
|
13
|
+
Rails.logger.debug(command)
|
14
|
+
output = `#{command}`
|
15
|
+
data = File.open("#{file.path}.png").read
|
16
|
+
File.unlink("#{file.path}.png")
|
17
|
+
File.unlink(file.path)
|
18
|
+
data
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def command(filepath, radius)
|
24
|
+
arguments = []
|
25
|
+
arguments << '\( +clone -alpha extract'
|
26
|
+
arguments << "-draw 'fill black polygon 0,0 0,#{radius} #{radius},0 fill white circle #{radius},#{radius} #{radius},0'"
|
27
|
+
arguments << '\( +clone -flip \) -compose Multiply -composite'
|
28
|
+
arguments << '\( +clone -flop \) -compose Multiply -composite'
|
29
|
+
arguments << '\) -alpha off -compose CopyOpacity -composite'
|
30
|
+
"convert #{filepath} #{arguments.join(' ')} #{filepath}.png"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
class Converter
|
5
|
+
|
6
|
+
def convert(data, conversions)
|
7
|
+
(1..conversions.length).each do |i|
|
8
|
+
data = _convert(assetpath, conversions)
|
9
|
+
end
|
10
|
+
file = file(data)
|
11
|
+
metadata = identify.identify(file)
|
12
|
+
File.unlink(file.path)
|
13
|
+
OpenStruct.new(data: data, content_type: metadata.content_type)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def _convert(assetpath, conversions)
|
18
|
+
source = (conversions.length > 1) ? url_writer.write(conversions[0, (conversions.size - 1)], assetpath) : assetpath
|
19
|
+
destination = url_writer.write(conversions, assetpath)
|
20
|
+
if filesystem.exists?(destination)
|
21
|
+
data = filesystem.get(destination)
|
22
|
+
return data
|
23
|
+
elsif filesystem.exists?(source)
|
24
|
+
data = filesystem.get(source)
|
25
|
+
file = file(data)
|
26
|
+
metadata = identify.identify(file)
|
27
|
+
newdata = process(file, metadata, conversion)
|
28
|
+
newurl = url_writer.write(conversions, assetpath)
|
29
|
+
filesystem.set(newurl, converted.data)
|
30
|
+
return newdata
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def processor
|
37
|
+
@processor ||= Imagecache::Convert.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def process(file, metadata, conversion)
|
41
|
+
if conversion.action == 'resize'
|
42
|
+
converter = Imagecache::Conversions::Resize.new
|
43
|
+
elsif conversion.action == 'crop'
|
44
|
+
converter = Imagecache::Conversions::Crop.new
|
45
|
+
elsif conversion.action == 'round'
|
46
|
+
converter = Imagecache::Conversions::Round.new
|
47
|
+
elsif conversion.action == 'fit'
|
48
|
+
converter = Imagecache::Conversions::Fit.new
|
49
|
+
end
|
50
|
+
converter.process(file, metadata, conversion)
|
51
|
+
end
|
52
|
+
|
53
|
+
def file(data)
|
54
|
+
filename = SecureRandom.hex(32).to_s.upcase[0,8]
|
55
|
+
filepath = "#{Rails.root}/tmp/#{filename}"
|
56
|
+
file = File.open(filepath, 'wb')
|
57
|
+
file.write(data)
|
58
|
+
file.close if file && !file.closed?
|
59
|
+
file
|
60
|
+
end
|
61
|
+
|
62
|
+
def identify
|
63
|
+
@identify ||= Imagecache::Identify.new
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
class Identify
|
5
|
+
|
6
|
+
def identify(file)
|
7
|
+
output = `identify -verbose #{file.path}`
|
8
|
+
geometry = output.match(/Geometry: (\d*)x(\d*)\+(\d*)\+(\d*)$/)
|
9
|
+
created = output.match(/date:create: (.*)$/)
|
10
|
+
content_type = output.match(/Mime type: (.*)$/)
|
11
|
+
OpenStruct.new(width: geometry[1].to_i, height: geometry[2].to_i, created: created[1], content_type: content_type[1])
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
class UrlReader
|
5
|
+
|
6
|
+
def read(path)
|
7
|
+
if path =~ /imagecache\/([^\/]*)\/(.*)/
|
8
|
+
assetpath = $2
|
9
|
+
conversions = parse_conversions($1)
|
10
|
+
OpenStruct.new({ conversions: conversions, assetpath: assetpath })
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def parse_conversions(conversions)
|
17
|
+
output = []
|
18
|
+
conversions.split("-").each do |conversion|
|
19
|
+
if conversion.downcase =~ /^rs(\d*)x(\d*)$/
|
20
|
+
output << OpenStruct.new({ action: 'resize', width: $1.to_i, height: $2.to_i })
|
21
|
+
elsif conversion.downcase =~ /^wd(\d*)$/
|
22
|
+
output << OpenStruct.new({ action: 'width', width: $1.to_i })
|
23
|
+
elsif conversion.downcase =~ /^rd(\d*)$/
|
24
|
+
output << OpenStruct.new({ action: 'round', radius: $1.to_i })
|
25
|
+
elsif conversion.downcase =~ /^ht(\d*)$/
|
26
|
+
output << OpenStruct.new({ action: 'height', height: $1.to_i })
|
27
|
+
elsif conversion.downcase =~ /^ft(\d*)x(\d*)$/
|
28
|
+
output << OpenStruct.new({ action: 'fit', width: $1.to_i, height: $2.to_i })
|
29
|
+
elsif conversion.downcase =~ /^cp(\d*)x(\d*)\+(\d*)\+(\d*)$/
|
30
|
+
output << OpenStruct.new({ action: 'crop', width: $1.to_i, height: $2.to_i, x: $3.to_i, y: $4.to_i })
|
31
|
+
end
|
32
|
+
end
|
33
|
+
output
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
class UrlWriter
|
5
|
+
|
6
|
+
def write(conversions, assetpath)
|
7
|
+
"imagecache/#{join(conversions)}/#{assetpath}"
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def join(conversions)
|
13
|
+
output = []
|
14
|
+
conversions.each do |conversion|
|
15
|
+
if conversion.action == 'resize'
|
16
|
+
output << "rs#{conversion[:width]}x#{conversion[:height]}"
|
17
|
+
elsif conversion.action == 'width'
|
18
|
+
output << "wd#{conversion[:width]}"
|
19
|
+
elsif conversion.action == 'height'
|
20
|
+
output << "ht#{conversion[:height]}"
|
21
|
+
elsif conversion.action == 'round'
|
22
|
+
output << "rd#{conversion[:radius]}"
|
23
|
+
elsif conversion.action == 'crop'
|
24
|
+
output << "cp#{conversion[:width]}x#{conversion[:height]}+#{conversion[:x]}+#{conversion[:y]}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
output.join("-")
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/imagecache.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Imagecache
|
4
|
+
require 'imagecache/backends/filesystem'
|
5
|
+
require 'imagecache/backends/redis'
|
6
|
+
require 'imagecache/backends/s3'
|
7
|
+
require 'imagecache/conversions/crop'
|
8
|
+
require 'imagecache/conversions/fit'
|
9
|
+
require 'imagecache/conversions/resize'
|
10
|
+
require 'imagecache/conversions/round'
|
11
|
+
require 'imagecache/base'
|
12
|
+
require 'imagecache/identify'
|
13
|
+
require 'imagecache/converter'
|
14
|
+
require 'imagecache/url_reader'
|
15
|
+
require 'imagecache/url_writer'
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
class FilesystemBackendTest < Minitest::Test
|
4
|
+
|
5
|
+
def test_can_get_file
|
6
|
+
File.open("#{Rails.root}/tmp/#{key}") { |file| file.write(data) }
|
7
|
+
value = Imagecache::Backends::Filesystem.new("#{Rails.root}/tmp").get(key)
|
8
|
+
asset_equal(data, value)
|
9
|
+
File.unlink("#{Rails.root}/tmp/#{key}")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_can_set_file
|
13
|
+
Imagecache::Backends::Filesystem.new("#{Rails.root}/tmp").set(key, data)
|
14
|
+
value = File.open("#{Rails.root}/tmp/#{key}").read
|
15
|
+
asset_equal(data, value)
|
16
|
+
File.unlink("#{Rails.root}/tmp/#{key}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_can_delete_file
|
20
|
+
File.open("#{Rails.root}/tmp/#{key}") { |file| file.write(data) }
|
21
|
+
Imagecache::Backends::Filesystem.new("#{Rails.root}/tmp").delete(key)
|
22
|
+
exists = File.exist?("#{Rails.root}/tmp/#{key}")
|
23
|
+
asset_equal(exists, false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_for_file_existence
|
27
|
+
exists = Imagecache::Backends::Filesystem.new("#{Rails.root}/tmp").exists(key)
|
28
|
+
asset_equal(exists, false)
|
29
|
+
File.open("#{Rails.root}/tmp/#{key}") { |file| file.write(data) }
|
30
|
+
exists = Imagecache::Backends::Filesystem.new("#{Rails.root}/tmp").exists(key)
|
31
|
+
asset_equal(exists, true)
|
32
|
+
File.unlink("#{Rails.root}/tmp/#{key}")
|
33
|
+
end
|
34
|
+
|
35
|
+
def data
|
36
|
+
"This is data"
|
37
|
+
end
|
38
|
+
|
39
|
+
def key
|
40
|
+
"assets/12345/test.jpg"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imagecache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Kops
|
@@ -14,34 +14,49 @@ dependencies:
|
|
14
14
|
name: rake
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 11.3.0
|
17
20
|
- - ">="
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
22
|
+
version: 11.3.0
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 11.3.0
|
24
30
|
- - ">="
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: 11.3.0
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: minitest
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 5.9.1
|
31
40
|
- - ">="
|
32
41
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
42
|
+
version: 5.9.1
|
34
43
|
type: :development
|
35
44
|
prerelease: false
|
36
45
|
version_requirements: !ruby/object:Gem::Requirement
|
37
46
|
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 5.9.1
|
38
50
|
- - ">="
|
39
51
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
52
|
+
version: 5.9.1
|
41
53
|
- !ruby/object:Gem::Dependency
|
42
54
|
name: activesupport
|
43
55
|
requirement: !ruby/object:Gem::Requirement
|
44
56
|
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '4.0'
|
45
60
|
- - ">="
|
46
61
|
- !ruby/object:Gem::Version
|
47
62
|
version: '4.0'
|
@@ -49,6 +64,9 @@ dependencies:
|
|
49
64
|
prerelease: false
|
50
65
|
version_requirements: !ruby/object:Gem::Requirement
|
51
66
|
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '4.0'
|
52
70
|
- - ">="
|
53
71
|
- !ruby/object:Gem::Version
|
54
72
|
version: '4.0'
|
@@ -56,6 +74,9 @@ dependencies:
|
|
56
74
|
name: redis
|
57
75
|
requirement: !ruby/object:Gem::Requirement
|
58
76
|
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '3.0'
|
59
80
|
- - ">="
|
60
81
|
- !ruby/object:Gem::Version
|
61
82
|
version: '3.0'
|
@@ -63,6 +84,9 @@ dependencies:
|
|
63
84
|
prerelease: false
|
64
85
|
version_requirements: !ruby/object:Gem::Requirement
|
65
86
|
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
66
90
|
- - ">="
|
67
91
|
- !ruby/object:Gem::Version
|
68
92
|
version: '3.0'
|
@@ -70,6 +94,9 @@ dependencies:
|
|
70
94
|
name: aws-sdk
|
71
95
|
requirement: !ruby/object:Gem::Requirement
|
72
96
|
requirements:
|
97
|
+
- - "~>"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: 1.60.2
|
73
100
|
- - ">="
|
74
101
|
- !ruby/object:Gem::Version
|
75
102
|
version: 1.60.2
|
@@ -77,6 +104,9 @@ dependencies:
|
|
77
104
|
prerelease: false
|
78
105
|
version_requirements: !ruby/object:Gem::Requirement
|
79
106
|
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.60.2
|
80
110
|
- - ">="
|
81
111
|
- !ruby/object:Gem::Version
|
82
112
|
version: 1.60.2
|
@@ -85,9 +115,31 @@ email: greg@thinktopography.com
|
|
85
115
|
executables: []
|
86
116
|
extensions: []
|
87
117
|
extra_rdoc_files: []
|
88
|
-
files:
|
89
|
-
|
90
|
-
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- Gemfile
|
121
|
+
- Gemfile.lock
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- imagecache.gemspec
|
125
|
+
- lib/imagecache.rb
|
126
|
+
- lib/imagecache/backends/filesystem.rb
|
127
|
+
- lib/imagecache/backends/redis.rb
|
128
|
+
- lib/imagecache/backends/s3.rb
|
129
|
+
- lib/imagecache/base.rb
|
130
|
+
- lib/imagecache/conversions/crop.rb
|
131
|
+
- lib/imagecache/conversions/fit.rb
|
132
|
+
- lib/imagecache/conversions/resize.rb
|
133
|
+
- lib/imagecache/conversions/round.rb
|
134
|
+
- lib/imagecache/converter.rb
|
135
|
+
- lib/imagecache/identify.rb
|
136
|
+
- lib/imagecache/url_reader.rb
|
137
|
+
- lib/imagecache/url_writer.rb
|
138
|
+
- lib/imagecache/version.rb
|
139
|
+
- test/filesystem_backend_test.rb
|
140
|
+
homepage: https://github.com/thinktopography/imagecache
|
141
|
+
licenses:
|
142
|
+
- MIT
|
91
143
|
metadata: {}
|
92
144
|
post_install_message:
|
93
145
|
rdoc_options: []
|
@@ -109,4 +161,5 @@ rubygems_version: 2.6.6
|
|
109
161
|
signing_key:
|
110
162
|
specification_version: 4
|
111
163
|
summary: imagecache
|
112
|
-
test_files:
|
164
|
+
test_files:
|
165
|
+
- test/filesystem_backend_test.rb
|