on_the_fly 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.
Files changed (7) hide show
  1. data/README +1 -0
  2. data/README.rdoc +64 -0
  3. data/Rakefile +15 -0
  4. data/VERSION +1 -0
  5. data/init.rb +1 -0
  6. data/lib/on_the_fly.rb +186 -0
  7. metadata +66 -0
data/README ADDED
@@ -0,0 +1 @@
1
+ See README.rdoc for more information.
data/README.rdoc ADDED
@@ -0,0 +1,64 @@
1
+ =Attachment on the Fly
2
+ =DO NOT USE THIS VERSION...IT IS NOT YET STABLE!!!! EXCEPT FOR ES3 AMS SYSTEM!
3
+ Attachment on the Fly is a module that extends Paperclip by allowing dynamic
4
+ image resizing without going through ActiveRecord first. This gem can only
5
+ resize images. If you try to resize a PDF or other attachment, nothing will
6
+ happen.
7
+
8
+ ==Requirements
9
+
10
+ * thoughtbot-paperclip gem
11
+ * ImageMagick
12
+
13
+ ==Quick Start
14
+
15
+ Attachment on the Fly is hosted on Gemcutter, so it is recommended that you
16
+ install the Gemcutter gem before installing Attachment on the Fly.
17
+
18
+ See http://gemcutter.org/ for installation instructions. Then:
19
+
20
+ sudo gem install attachment_on_the_fly
21
+
22
+ Setup your models the same way you normally do with Paperclip. See
23
+ http://github.com/thoughtbot/paperclip for detailed instructions.
24
+
25
+ Once your attachments are working with Paperclip, add this line to the top of
26
+ your models:
27
+
28
+ require "attachment_on_the_fly"
29
+
30
+ To use, reference the instance of your attachment like this:
31
+
32
+ <%= my_image_model.attachment.s_640_480 %>
33
+
34
+ This will output an image path based on your url interpolation and a resized
35
+ image named S_640_480_your_image_name.jpg (or gif or png).
36
+
37
+ ==Usage
38
+
39
+ Attachment on the Fly can accept three types of inputs: width by height, height
40
+ proportional, and width proportional.
41
+
42
+ my_image_model.attachment.s_640_480 will generate a 640x480 pixel image.
43
+
44
+ my_image_model.attachment.s_640_width will generate an image that is 640 pixels
45
+ wide and proportionally high based on the original image dimensions.
46
+
47
+ my_image_model.attachment.s_480_height will generate an image that is 480 pixels
48
+ high and proportionally wide based on the original image dimensions.
49
+
50
+ When an image's instance is first accessed through the s_ method, a new image
51
+ will be generated according to the image's path interpolation. Then, every time
52
+ the image is accessed with the same dimensions, the previously generated image
53
+ will be used. This allows the flexibility to resize images at will but still be
54
+ able to cache previously generated images.
55
+
56
+ Currently, convert's sharpen is set to 1. If you need to change the sharpness
57
+ or any other of convert's command line options, you will need to do so in the
58
+ final lines of lib/attachment_on_the_fly.rb. We are working on a feature to do
59
+ this dynamically, but it is not available yet.
60
+
61
+ ==Contributing
62
+
63
+ If you find this gem useful, please post your comments, bugs, patches and
64
+ suggestions on our Github site at http://github.com/drpentode/Attachment-on-the-Fly.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "on_the_fly"
5
+ gemspec.summary = "A Paperclip mix-in to allow auto-generation of resized images"
6
+ gemspec.description = "A Paperclip mix-in to allow auto-generation of resized images. Modded to delete images
7
+ and rename the thumb works on only the ES3 AMS system right now."
8
+ gemspec.email = "travis@pessetto.com"
9
+ gemspec.homepage = "http://pessetto.com"
10
+ gemspec.authors = ["Jeff Sutherland", "Travis Pessetto"]
11
+ gemspec.add_dependency "paperclip"
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'on_the_fly'
data/lib/on_the_fly.rb ADDED
@@ -0,0 +1,186 @@
1
+ #
2
+ # Methods to allow attachment modification on-the-fly
3
+ # the paperclip attachment attribute should be called "attachment" on the model
4
+ #
5
+ Paperclip::Attachment.class_eval do
6
+ require 'ftools' if RUBY_VERSION < "1.9"
7
+ require 'fileutils'
8
+ require 'tempfile'
9
+
10
+ # we respond to s_ and cls_
11
+ def respond_to?(method,*args, &block)
12
+ if method.to_s.match(/^s_[0-9]+_[0-9]+/) || method.to_s.match(/^s_[0-9]+_[a-z]+/) || method.to_s.match(/^s[0-9]+/) ||
13
+ method.to_s.match(/^cls_[0-9]+_[0-9]+/) || method.to_s.match(/^cls_[0-9]+_[a-z]+/) || method.to_s.match(/^cls[0-9]+/)
14
+ puts("IN METHOD TO S MATCH+++---:Method: #{method}")
15
+ return true
16
+ end
17
+ super
18
+ end
19
+
20
+ def method_missing(symbol , *args, &block )
21
+ #puts("IN METHOD MISSING+++---:symbol: #{symbol}")
22
+ @asset_id = 0
23
+ args.each do |val|
24
+ @asset_id = val
25
+ puts ("VAL #{val}")
26
+ end
27
+ # We are looking for methods with S_[number]_[number]_(height | width | proportion)
28
+ # Height and width
29
+ # Check to see if file exist if so return string
30
+ # if not generate image and return string to file Fiel is in format S_Height_x_Width_FILE_NAME
31
+ image_name = nil
32
+ if symbol.to_s.match(/^s_[0-9]+_[0-9]+/) || symbol.to_s.match(/^cls_[0-9]+_[0-9]+/)
33
+ values = symbol.to_s.split("_")
34
+ height = values[1]
35
+ width = values[2]
36
+ image_name = generate_image("both", height.to_i, width.to_i)
37
+ elsif symbol.to_s.match(/^s_[0-9]+_[a-z]+/) || symbol.to_s.match(/^cls_[0-9]+_[a-z]+/)
38
+ values = symbol.to_s.split("_")
39
+ size = values[1]
40
+ who = values[2]
41
+ image_name = generate_image(who, size.to_i)
42
+ elsif symbol.to_s.match(/^s[0-9]+/) || symbol.to_s.match(/^cls[0-9]+/)
43
+ values = symbol.to_s.split("s")
44
+ size = values[1]
45
+ who = "width"
46
+ image_name = generate_image(who, size.to_i)
47
+ else
48
+ # if our method string does not match, we kick things back up to super ... this keeps ActiveRecord chugging along happily
49
+ super
50
+ end
51
+ return image_name
52
+ end
53
+
54
+ def generate_image(kind, height = 0, width = 0)
55
+ convert_command_path = (Paperclip.options[:command_path] ? Paperclip.options[:command_path] + "/" : "")
56
+
57
+ prefix = ""
58
+
59
+ if kind == "height"
60
+ prefix = "S_" + height.to_s + "_HEIGHT_"
61
+ elsif kind == "width"
62
+ width = height
63
+ prefix = "S_" + height.to_s + "_WIDTH_"
64
+ elsif kind == "both"
65
+ prefix = "S_" + height.to_s + "_" + height.to_s + "_"
66
+ end
67
+
68
+ puts("!!!!!!!!SELF++: #{self}")
69
+ path = self.path
70
+ url = self.url
71
+
72
+ path_arr = path.split("/")
73
+ file_name = path_arr.pop
74
+ path = path_arr.join("/")
75
+
76
+ url_arr = url.split("/")
77
+ url_file_name = url_arr.pop
78
+ url_path = url_arr.join("/")
79
+ #just pop url_arry.pop
80
+
81
+ original = path + "/" + self.original_filename
82
+ newattchfilename = prefix + file_name
83
+ newfilename = path + "/" + prefix + file_name
84
+ new_path = url_path + "/" + prefix + file_name
85
+
86
+ orig_arry = original.split("/")
87
+ orig_arry.pop(2)
88
+ thumbfile = orig_arry.join("/") + "/thumb/" + file_name if !windows?
89
+ thumbfile = orig_arry.join("\\") + "\\thumb\\" + file_name if windows?
90
+
91
+
92
+ return new_path if File.exist?(newfilename)
93
+
94
+ if !File.exist?(original)
95
+ if Paperclip.options[:whiny]
96
+ raise OnTheFlyError.new("Original asset could not be read from disk at #{original}")
97
+ else
98
+ Paperclip.log("Original asset could not be read from disk at #{original}")
99
+ if Paperclip.options[:missing_image_path]
100
+ return Paperclip.options[:missing_image_path]
101
+ else
102
+ Paperclip.log("Please configure Paperclip.options[:missing_image_path] to prevent return of broken image path")
103
+ return new_path
104
+ end
105
+ end
106
+ end
107
+
108
+
109
+ #WINDOWS SUPPORT-Requires it to be in the PATH
110
+ if windows?
111
+ command = ""
112
+ if kind == "height"
113
+ # resize_image infilename, outfilename , 0, height
114
+ command = "convert -colorspace RGB -geometry x#{height} -quality 100 -sharpen 1 \"#{original}\" \"#{newfilename}\" >NUL"
115
+ elsif kind == "width"
116
+ # resize_image infilename, outfilename, width
117
+ command = "convert -colorspace RGB -geometry #{width} -quality 100 -sharpen 1 \"#{original}\" \"#{newfilename}\" >NUL"
118
+ elsif kind == "both"
119
+ # resize_image infilename, outfilename, height, width
120
+ command = "convert -colorspace RGB -geometry #{width}x#{height} -quality 100 -sharpen 1 \"#{original}\" \"#{newfilename}\" >NUL"
121
+ end
122
+
123
+ else #it must be *Nix based.
124
+ command = ""
125
+
126
+ if kind == "height"
127
+ # resize_image infilename, outfilename , 0, height
128
+ command = "#{convert_command_path}convert -colorspace RGB -geometry x#{height} -quality 100 -sharpen 1 #{original} #{newfilename} 2>&1 > /dev/null"
129
+ elsif kind == "width"
130
+ # resize_image infilename, outfilename, width
131
+ command = "#{convert_command_path}convert -colorspace RGB -geometry #{width} -quality 100 -sharpen 1 #{original} #{newfilename} 2>&1 > /dev/null"
132
+ elsif kind == "both"
133
+ # resize_image infilename, outfilename, height, width
134
+ command = "#{convert_command_path}convert -colorspace RGB -geometry #{width}x#{height} -quality 100 -sharpen 1 #{original} #{newfilename} 2>&1 > /dev/null"
135
+ end
136
+ end#ending Windows/Linux commands
137
+
138
+ `#{command}`
139
+
140
+ if $? != 0
141
+ raise OnTheFlyError.new("Execution of convert failed. Please set path in Paperclip.options[:command_path] or ensure that file permissions are correct.")
142
+ else
143
+ puts ("saving...")
144
+ puts ("-----++++----ABOUT SELF #{self}")
145
+ puts ("-----++++----ASSET ID #{@asset_id}")
146
+ img = Attachment.find_by_asset_id(@asset_id,:conditions=>{:main_image=>true})
147
+ puts("IMG: #{img}")
148
+ puts("+++-----NEW FILE NAME: #{newattchfilename}")
149
+ img.attachment_file_name=newattchfilename
150
+ img.save
151
+ #delete the old
152
+ puts("[ONTHEFLY]: DELETING ORIGINAL FILE...#{original}")
153
+ puts("[ONTHEFLY]: CONVERTING ORIGINAL...")
154
+ original = original.gsub(/[\/]/, '\\')
155
+ puts("[ONTHEFLY]: ORIGINAL NOW IS #{original}")
156
+ cmd = "del \"#{original}\"" if windows?
157
+ cmd = "rm #{original}" if !windows?
158
+ puts("[ONTHEFLY]: DELETED ORIGINAL FILE...")
159
+ `#{cmd}`
160
+ puts("[ONTHEFLY]: RENAMING THUMB...")
161
+ puts("[ONTHEFLY]: THUMB PATH IS #{thumbfile}")
162
+ if windows?
163
+ cmd = "REN \"#{thumbfile}\" #{prefix + file_name}"
164
+ else
165
+ cmd = "MV #{thumbfile} #{prefix+file_name}"
166
+ end
167
+ `#{cmd}`
168
+ puts ("[ONTHEFLY]: CHANGED THUMB NAME...")
169
+
170
+ #we need to save the object to the database to make it so it will delete!
171
+ #img=Attachment.new(:name=>"ontheflyimg",:created_at=>Time.now,:updated_at=>Time.now,
172
+ #:attachment_file_name=>newfilename,:asset_id=>76,:attachment_file_size=>0,
173
+ #:main_image=>0)
174
+ #img.save
175
+ end
176
+
177
+ return new_path
178
+ end
179
+
180
+ def windows?
181
+ !(RUBY_PLATFORM =~ /win32|mswin|mingw/).nil?
182
+ end
183
+
184
+ end
185
+
186
+ class OnTheFlyError < StandardError; end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: on_the_fly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Sutherland
9
+ - Travis Pessetto
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-08-10 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: paperclip
17
+ requirement: &22064496 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *22064496
26
+ description: ! "A Paperclip mix-in to allow auto-generation of resized images. Modded
27
+ to delete images\n\tand rename the thumb works on only the ES3 AMS system right
28
+ now."
29
+ email: travis@pessetto.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files:
33
+ - README
34
+ - README.rdoc
35
+ files:
36
+ - README
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION
40
+ - init.rb
41
+ - lib/on_the_fly.rb
42
+ homepage: http://pessetto.com
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.6
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A Paperclip mix-in to allow auto-generation of resized images
66
+ test_files: []