GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Rails plugin for uploading images as resources, with support for resizing, text stamping, and other special effects.
Homepage: http://fleximage.rubyforge.org
Clone URL: git://github.com/Squeegy/fleximage.git
Greatly DRYed up rake conversion tasks
Squeegy (author)
Thu Apr 03 15:06:08 -0700 2008
commit  861804e96a8f2b16f135e41116e561eb6189e272
tree    0e95c08848a5c55e422d3250abfeddb05b50285c
parent  8d749291ef45b94722408aa20f83ee0d6cdabd0d
...
1
2
 
3
4
5
6
7
 
 
 
 
 
 
 
8
 
 
9
10
11
12
 
 
 
13
14
15
16
17
 
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
27
28
29
 
30
31
32
...
35
36
37
38
39
40
 
41
42
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
45
46
 
47
48
49
 
 
50
51
52
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
68
69
70
...
 
 
1
2
3
 
 
 
4
5
6
7
8
9
10
11
12
13
14
15
 
 
16
17
18
19
 
 
 
 
20
21
22
23
24
25
26
27
 
 
 
 
 
 
 
 
 
 
28
29
30
31
32
...
35
36
37
 
 
 
38
39
 
 
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
60
61
62
 
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 
 
 
 
 
 
 
 
 
 
 
 
86
87
88
89
0
@@ -1,32 +1,32 @@
0
-desc "Explaining what the task does"
0
-namespace :fleximage do
0
+namespace :fleximage do
0
   namespace :convert do
0
     
0
- desc "Convert a flat images/123.png style image store to a images/2007/11/12/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
- task :to_nested => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ # Find the model class
0
+ def model_class
0
+ raise 'You must specify a FLEXIMAGE_CLASS=MyClass' unless ENV['FLEXIMAGE_CLASS']
0
+ @model_class ||= ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ end
0
+
0
+ def convert_directory_format(to_format)
0
       model_class.find(:all).each do |obj|
0
+
0
+ # Get the creation date
0
         creation = obj[:created_at] || obj[:created_on]
0
         
0
- old_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
- new_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
+ # Generate both types of file paths
0
+ flat_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
+ nested_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
         
0
- if old_path != new_path && File.exists?(old_path)
0
- FileUtils.mkdir_p(File.dirname(new_path))
0
- FileUtils.move old_path, new_path
0
- puts "#{old_path} -> #{new_path}"
0
+ # Assign old path and new path based on desired directory format
0
+ if to_format == :nested
0
+ old_path = flat_path
0
+ new_path = nested_path
0
+ else
0
+ old_path = nested_path
0
+ new_path = flat_path
0
         end
0
- end
0
- end
0
-
0
- desc "Convert a nested images/2007/11/12/123.png style image store to a images/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
- task :to_flat => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
- model_class.find(:all).each do |obj|
0
- creation = obj[:created_at] || obj[:created_on]
0
- old_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
- new_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
         
0
+ # Move the files
0
         if old_path != new_path && File.exists?(old_path)
0
           FileUtils.mkdir_p(File.dirname(new_path))
0
           FileUtils.move old_path, new_path
0
@@ -35,36 +35,55 @@ namespace :fleximage do
0
       end
0
     end
0
     
0
- desc "Convert master images stored as JPGs to PNGs"
0
- task :to_png => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ def convert_image_format(to_format)
0
       model_class.find(:all).each do |obj|
0
- old_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
- new_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
+
0
+ # Generate both types of file paths
0
+ png_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
+ jpg_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
+
0
+ # Output stub
0
+ output = (to_format == :jpg) ? 'PNG -> JPG' : 'JPG -> PNG'
0
+
0
+ # Assign old path and new path based on desired image format
0
+ if to_format == :jpg
0
+ old_path = png_path
0
+ new_path = jpg_path
0
+ else
0
+ old_path = jpg_path
0
+ new_path = png_path
0
+ end
0
+
0
+ # Perform conversion
0
         if File.exists?(old_path)
0
           image = Magick::Image.read(old_path).first
0
- image.format = 'PNG'
0
+ image.format = to_format.to_s.upcase
0
           image.write(new_path)
0
           File.delete(old_path)
0
- puts "JPG -> PNG : Image #{obj.id}"
0
+
0
+ puts "#{output} : Image #{obj.id}"
0
         end
0
       end
0
     end
0
     
0
+ desc "Convert a flat images/123.png style image store to a images/2007/11/12/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
+ task :to_nested => :environment do
0
+ convert_directory_format :nested
0
+ end
0
+
0
+ desc "Convert a nested images/2007/11/12/123.png style image store to a images/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
+ task :to_flat => :environment do
0
+ convert_directory_format :flat
0
+ end
0
+
0
+ desc "Convert master images stored as JPGs to PNGs"
0
+ task :to_png => :environment do
0
+ convert_image_format :png
0
+ end
0
+
0
     desc "Convert master images stored as PNGs to JPGs"
0
     task :to_jpg => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
- model_class.find(:all).each do |obj|
0
- old_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
- new_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
- if File.exists?(old_path)
0
- image = Magick::Image.read(old_path).first
0
- image.format = 'JPG'
0
- image.write(new_path)
0
- File.delete(old_path)
0
- puts "PNG -> JPG : Image #{obj.id}"
0
- end
0
- end
0
+ convert_image_format :jpg
0
     end
0
     
0
   end

Comments

    No one has commented yet.