|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
1 |
module Fleximage |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
2 |
|
| |
3 |
# Container for Fleximage model method inclusion modules |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
4 |
module Model |
| |
5 |
|
|
18112e3e
»
|
Squeegy |
2008-03-30 |
Added a more helpful except... |
6 |
class MasterImageNotFound < RuntimeError #:nodoc: |
| |
7 |
end |
| |
8 |
|
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
9 |
# Include acts_as_fleximage class method |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
10 |
def self.included(base) #:nodoc: |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
11 |
base.extend(ClassMethods) |
| |
12 |
end |
| |
13 |
|
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
14 |
# Provides class methods for Fleximage for use in model classes. The only class method is |
| |
15 |
# acts_as_fleximage which integrates Fleximage functionality into a model class. |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
16 |
# |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
17 |
# The following class level accessors also get inserted. |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
18 |
# |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
19 |
# * +image_directory+: (String, no default) Where the master images are stored, directory path relative to your |
| |
20 |
# app root. |
| |
21 |
# * +use_creation_date_based_directories+: (Boolean, default +true+) If true, master images will be stored in |
| |
22 |
# directories based on creation date. For example: <tt>"#{image_directory}/2007/11/24/123.png"</tt> for an |
|
b0efcd08
»
|
Squeegy |
2008-04-02 |
provided file object will e... |
23 |
# image with an id of 123 and a creation date of November 24, 2007. Turing this off would cause the path |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
24 |
# to be "#{image_directory}/123.png" instead. This helps keep the OS from having directories that are too |
| |
25 |
# full. |
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
26 |
# * +image_storage_format+: (:png or :jpg, default :png) The format of your master images. Using :png will give |
| |
27 |
# you the best quality, since the master images as stored as lossless version of the original upload. :jpg |
| |
28 |
# will apply lossy compression, but the master image file sizes will be much smaller. If storage space is a |
| |
29 |
# concern, us :jpg. |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
30 |
# * +require_image+: (Boolean, default +true+) The model will raise a validation error if no image is uploaded |
| |
31 |
# with the record. Setting to false allows record to be saved with no images. |
|
9b8dae30
»
|
Squeegy |
2008-04-02 |
doc update for class method... |
32 |
# * +missing_image_message+: (String, default "is required") Validation message to display when no image was uploaded for |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
33 |
# a record. |
|
9b8dae30
»
|
Squeegy |
2008-04-02 |
doc update for class method... |
34 |
# * +invalid_image_message+: (String default "was not a readable image") Validation message when an image is uploaded, but is not an |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
35 |
# image format that can be read by RMagick. |
|
b0efcd08
»
|
Squeegy |
2008-04-02 |
provided file object will e... |
36 |
# * +output_image_jpg_quality+: (Integer, default 85) When rendering JPGs, this represents the amount of |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
37 |
# compression. Valid values are 0-100, where 0 is very small and very ugly, and 100 is near lossless but |
| |
38 |
# very large in filesize. |
|
aff901ae
»
|
Squeegy |
2008-04-12 |
rdoc update |
39 |
# * +default_image_path+: (String, nil default) If no image is present for this record, the image at this path will be |
| |
40 |
# used instead. Useful for a placeholder graphic for new content that may not have an image just yet. |
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
41 |
# * +default_image+: A hash which defines an empty starting image. This hash look like: <tt>:size => '123x456', |
| |
42 |
# :color => :transparent</tt>, where <tt>:size</tt> defines the dimensions of the default image, and <tt>:color</tt> |
| |
43 |
# defines the fill. <tt>:color</tt> can be a named color as a string ('red'), :transparent, or a Magick::Pixel object. |
|
9b8dae30
»
|
Squeegy |
2008-04-02 |
doc update for class method... |
44 |
# * +preprocess_image+: (Block, no default) Call this class method just like you would call +operate+ in a view. |
| |
45 |
# The image transoformation in the provided block will be run on every uploaded image before its saved as the |
| |
46 |
# master image. |
| |
47 |
# |
| |
48 |
# Example: |
| |
49 |
# |
| |
50 |
# class Photo < ActiveRecord::Base |
|
39050b06
»
|
Squeegy |
2008-04-05 |
Accept model configuration ... |
51 |
# acts_as_fleximage do |
| |
52 |
# image_directory 'public/images/uploaded' |
| |
53 |
# use_creation_date_based_directories true |
| |
54 |
# image_storage_format :png |
| |
55 |
# require_image true |
| |
56 |
# missing_image_message 'is required' |
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
57 |
# invalid_image_message 'was not a readable image'\ |
|
aff901ae
»
|
Squeegy |
2008-04-12 |
rdoc update |
58 |
# default_image_path 'public/images/no_photo_yet.png' |
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
59 |
# default_image nil |
|
39050b06
»
|
Squeegy |
2008-04-05 |
Accept model configuration ... |
60 |
# output_image_jpg_quality 85 |
| |
61 |
# |
| |
62 |
# preprocess_image do |image| |
| |
63 |
# image.resize '1024x768' |
| |
64 |
# end |
|
9b8dae30
»
|
Squeegy |
2008-04-02 |
doc update for class method... |
65 |
# end |
| |
66 |
# |
| |
67 |
# # normal model methods... |
| |
68 |
# end |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
69 |
module ClassMethods |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
70 |
|
| |
71 |
# Use this method to include Fleximage functionality in your model. It takes an |
| |
72 |
# options hash with a single required key, :+image_directory+. This key should |
|
39050b06
»
|
Squeegy |
2008-04-05 |
Accept model configuration ... |
73 |
# point to the directory you want your images stored on your server. Or |
| |
74 |
# configure with a nice looking block. |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
75 |
def acts_as_fleximage(options = {}) |
| |
76 |
|
|
81e51e74
»
|
Squeegy |
2008-05-21 |
Simplification of some unne... |
77 |
# Include the necesary instance methods |
| |
78 |
include Fleximage::Model::InstanceMethods |
| |
79 |
|
| |
80 |
# Call this class method just like you would call +operate+ in a view. |
| |
81 |
# The image transoformation in the provided block will be run on every uploaded image before its saved as the |
| |
82 |
# master image. |
| |
83 |
def self.preprocess_image(&block) |
| |
84 |
preprocess_image_operation(block) |
| |
85 |
end |
| |
86 |
|
| |
87 |
# Internal method to ask this class if it stores image in the DB. |
| |
88 |
def self.db_store? |
| |
89 |
columns.find do |col| |
| |
90 |
col.name == 'image_file_data' |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
91 |
end |
|
cd20f273
»
|
Squeegy |
2008-04-01 |
Added image preprocessing v... |
92 |
end |
| |
93 |
|
|
a6c1bde3
»
|
Squeegy |
2008-04-30 |
Fixed that overriding the v... |
94 |
# validation callback |
| |
95 |
validate :validate_image |
| |
96 |
|
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
97 |
# The filename of the temp image. Used for storing of good images when validation fails |
| |
98 |
# and the form needs to be redisplayed. |
| |
99 |
attr_reader :image_file_temp |
| |
100 |
|
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
101 |
# Where images get stored |
| |
102 |
dsl_accessor :image_directory |
| |
103 |
|
| |
104 |
# Put uploads from different days into different subdirectories |
| |
105 |
dsl_accessor :use_creation_date_based_directories, :default => true |
| |
106 |
|
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
107 |
# The format are master images are stored in |
| |
108 |
dsl_accessor :image_storage_format, :default => Proc.new { :png } |
| |
109 |
|
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
110 |
# Require a valid image. Defaults to true. Set to false if its ok to have no image for |
| |
111 |
dsl_accessor :require_image, :default => true |
| |
112 |
|
| |
113 |
# Missing image message |
| |
114 |
dsl_accessor :missing_image_message, :default => 'is required' |
| |
115 |
|
| |
116 |
# Invalid image message |
| |
117 |
dsl_accessor :invalid_image_message, :default => 'was not a readable image' |
| |
118 |
|
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
119 |
# Sets the quality of rendered JPGs |
| |
120 |
dsl_accessor :output_image_jpg_quality, :default => 85 |
| |
121 |
|
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
122 |
# Set a default image to use when no image has been assigned to this record |
| |
123 |
dsl_accessor :default_image_path |
| |
124 |
|
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
125 |
# Set a default image based on a a size and fill |
| |
126 |
dsl_accessor :default_image |
| |
127 |
|
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
128 |
# A block that processes an image before it gets saved as the master image of a record. |
| |
129 |
# Can be helpful to resize potentially huge images to something more manageable. Set via |
| |
130 |
# the "preprocess_image { |image| ... }" class method. |
| |
131 |
dsl_accessor :preprocess_image_operation |
| |
132 |
|
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
133 |
# Image related save and destroy callbacks |
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
134 |
after_destroy :delete_image_file |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
135 |
before_save :pre_save |
| |
136 |
after_save :post_save |
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
137 |
|
|
39050b06
»
|
Squeegy |
2008-04-05 |
Accept model configuration ... |
138 |
# execute configuration block |
| |
139 |
yield if block_given? |
| |
140 |
|
| |
141 |
# set the image directory from passed options |
| |
142 |
image_directory options[:image_directory] if options[:image_directory] |
| |
143 |
|
| |
144 |
# Require the declaration of a master image storage directory |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
145 |
if !image_directory && !db_store? |
| |
146 |
raise "No place to put images! Declare this via the :image_directory => 'path/to/directory' option\n"+ |
| |
147 |
"Or add a database column named image_file_data for DB storage" |
|
39050b06
»
|
Squeegy |
2008-04-05 |
Accept model configuration ... |
148 |
end |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
149 |
end |
| |
150 |
end |
| |
151 |
|
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
152 |
# Provides methods that every model instance that acts_as_fleximage needs. |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
153 |
module InstanceMethods |
| |
154 |
|
| |
155 |
# Returns the path to the master image file for this record. |
| |
156 |
# |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
157 |
# @some_image.directory_path #=> /var/www/myapp/uploaded_images |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
158 |
# |
| |
159 |
# If this model has a created_at field, it will use a directory |
| |
160 |
# structure based on the creation date, to prevent hitting the OS imposed |
| |
161 |
# limit on the number files in a directory. |
| |
162 |
# |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
163 |
# @some_image.directory_path #=> /var/www/myapp/uploaded_images/2008/3/30 |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
164 |
def directory_path |
|
7250e32e
»
|
Squeegy |
2008-04-12 |
Added conversion tasks to t... |
165 |
raise 'No image directory was defined, cannot generate path' unless self.class.image_directory |
| |
166 |
|
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
167 |
# base directory |
| |
168 |
directory = "#{RAILS_ROOT}/#{self.class.image_directory}" |
| |
169 |
|
| |
170 |
# specific creation date based directory suffix. |
|
407eb0ae
»
|
Squeegy |
2008-03-30 |
Added use_creation_date_bas... |
171 |
creation = self[:created_at] || self[:created_on] |
| |
172 |
if self.class.use_creation_date_based_directories && creation |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
173 |
"#{directory}/#{creation.year}/#{creation.month}/#{creation.day}" |
| |
174 |
else |
| |
175 |
directory |
| |
176 |
end |
| |
177 |
end |
| |
178 |
|
| |
179 |
# Returns the path to the master image file for this record. |
| |
180 |
# |
| |
181 |
# @some_image.file_path #=> /var/www/myapp/uploaded_images/123.png |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
182 |
def file_path |
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
183 |
"#{directory_path}/#{id}.#{self.class.image_storage_format}" |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
184 |
end |
| |
185 |
|
|
019dd1fe
»
|
Squeegy |
2008-03-30 |
documentation |
186 |
# Sets the image file for this record to an uploaded file. This can |
| |
187 |
# be called directly, or passively like from an ActiveRecord mass |
| |
188 |
# assignment. |
| |
189 |
# |
| |
190 |
# Rails will automatically call this method for you, in most of the |
| |
191 |
# situations you would expect it to. |
| |
192 |
# |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
193 |
# # via mass assignment, the most common form you'll probably use |
| |
194 |
# Photo.new(params[:photo]) |
| |
195 |
# Photo.create(params[:photo]) |
| |
196 |
# |
|
019dd1fe
»
|
Squeegy |
2008-03-30 |
documentation |
197 |
# # via explicit assignment hash |
| |
198 |
# Photo.new(:image_file => params[:photo][:image_file]) |
| |
199 |
# Photo.create(:image_file => params[:photo][:image_file]) |
| |
200 |
# |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
201 |
# # Direct Assignment, usually not needed |
| |
202 |
# photo = Photo.new |
| |
203 |
# photo.image_file = params[:photo][:image_file] |
|
019dd1fe
»
|
Squeegy |
2008-03-30 |
documentation |
204 |
# |
| |
205 |
# # via an association proxy |
| |
206 |
# p = Product.find(1) |
| |
207 |
# p.images.create(params[:photo]) |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
208 |
def image_file=(file) |
|
b0efcd08
»
|
Squeegy |
2008-04-02 |
provided file object will e... |
209 |
# Get the size of the file. file.size works for form-uploaded images, file.stat.size works |
| |
210 |
# for file object created by File.open('foo.jpg', 'rb') |
| |
211 |
file_size = file.respond_to?(:size) ? file.size : file.stat.size |
| |
212 |
|
| |
213 |
if file.respond_to?(:read) && file_size > 0 |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
214 |
|
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
215 |
# Create RMagick Image object from uploaded file |
| |
216 |
if file.path |
| |
217 |
@uploaded_image = Magick::Image.read(file.path).first |
| |
218 |
else |
| |
219 |
@uploaded_image = Magick::Image.from_blob(file.read).first |
| |
220 |
end |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
221 |
|
| |
222 |
set_magic_attributes(file) |
| |
223 |
|
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
224 |
# Success, make sure everything is valid |
| |
225 |
@invalid_image = false |
|
8edb2e60
»
|
Squeegy |
2008-04-17 |
Fixed that temp image would... |
226 |
save_temp_image(file) unless @dont_save_temp |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
227 |
end |
| |
228 |
rescue Magick::ImageMagickError => e |
|
fd5812db
»
|
Squeegy |
2008-04-18 |
Fixed a minor validation me... |
229 |
error_strings = [ |
| |
230 |
'Improper image header', |
| |
231 |
'no decode delegate for this image format', |
|
b05be276
»
|
Squeegy |
2008-04-18 |
Additional tests and test s... |
232 |
'UnableToOpenBlob' |
|
fd5812db
»
|
Squeegy |
2008-04-18 |
Fixed a minor validation me... |
233 |
] |
| |
234 |
if e.to_s =~ /#{error_strings.join('|')}/ |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
235 |
@invalid_image = true |
| |
236 |
else |
| |
237 |
raise e |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
238 |
end |
| |
239 |
end |
| |
240 |
|
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
241 |
# Assign the image via a URL, which will make the plugin go |
| |
242 |
# and fetch the image at the provided URL. The image will be stored |
| |
243 |
# locally as a master image for that record from then on. This is |
| |
244 |
# intended to be used along side the image upload to allow people the |
| |
245 |
# choice to upload from their local machine, or pull from the internet. |
| |
246 |
# |
| |
247 |
# @photo.image_file_url = 'http://foo.com/bar.jpg' |
| |
248 |
def image_file_url=(file_url) |
| |
249 |
@image_file_url = file_url |
| |
250 |
if file_url =~ %r{^https?://} |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
251 |
file = open(file_url) |
| |
252 |
|
| |
253 |
# Force a URL based file to have an original_filename |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
254 |
eval <<-CODE |
|
81e51e74
»
|
Squeegy |
2008-05-21 |
Simplification of some unne... |
255 |
def file.original_filename |
| |
256 |
"#{file_url}" |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
257 |
end |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
258 |
CODE |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
259 |
|
| |
260 |
self.image_file = file |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
261 |
|
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
262 |
elsif file_url.empty? |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
263 |
# Nothing to process, move along |
| |
264 |
|
|
3cf9e6f8
»
|
Squeegy |
2008-03-31 |
Added ability to accept a U... |
265 |
else |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
266 |
# invalid URL, raise invalid image validation error |
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
267 |
@invalid_image = true |
|
3cf9e6f8
»
|
Squeegy |
2008-03-31 |
Added ability to accept a U... |
268 |
end |
| |
269 |
end |
| |
270 |
|
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
271 |
# Sets the uploaded image to the name of a file in RAILS_ROOT/tmp that was just |
| |
272 |
# uploaded. Use as a hidden field in your forms to keep an uploaded image when |
| |
273 |
# validation fails and the form needs to be redisplayed |
| |
274 |
def image_file_temp=(file_name) |
| |
275 |
if !@uploaded_image && file_name && file_name.any? |
|
926f6aff
»
|
Squeegy |
2008-04-15 |
Fixed embarrasing bug where... |
276 |
@image_file_temp = file_name |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
277 |
file_path = "#{RAILS_ROOT}/tmp/fleximage/#{file_name}" |
|
8edb2e60
»
|
Squeegy |
2008-04-17 |
Fixed that temp image would... |
278 |
|
| |
279 |
@dont_save_temp = true |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
280 |
if File.exists?(file_path) |
| |
281 |
File.open(file_path, 'rb') do |f| |
| |
282 |
self.image_file = f |
| |
283 |
end |
| |
284 |
end |
|
8edb2e60
»
|
Squeegy |
2008-04-17 |
Fixed that temp image would... |
285 |
@dont_save_temp = false |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
286 |
end |
| |
287 |
end |
| |
288 |
|
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
289 |
# Return the @image_file_url that was previously assigned. This is not saved |
| |
290 |
# in the database, and only exists to make forms happy. |
| |
291 |
def image_file_url |
| |
292 |
@image_file_url |
| |
293 |
end |
| |
294 |
|
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
295 |
# Return true if this record has an image. |
| |
296 |
def has_image? |
|
78e4f66f
»
|
Squeegy |
2008-04-15 |
refactored has_image? |
297 |
@uploaded_image || @output_image || has_saved_image? |
| |
298 |
end |
| |
299 |
|
| |
300 |
def has_saved_image? |
| |
301 |
self.class.db_store? ? !!image_file_data : File.exists?(file_path) |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
302 |
end |
| |
303 |
|
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
304 |
# Call from a .flexi view template. This enables the rendering of operators |
|
cd20f273
»
|
Squeegy |
2008-04-01 |
Added image preprocessing v... |
305 |
# so that you can transform your image. This is the method that is the foundation |
| |
306 |
# of .flexi views. Every view should consist of image manipulation code inside a |
| |
307 |
# block passed to this method. |
|
d373c0dd
»
|
Squeegy |
2008-03-31 |
massive doc update, now inc... |
308 |
# |
| |
309 |
# # app/views/photos/thumb.jpg.flexi |
| |
310 |
# @photo.operate do |image| |
| |
311 |
# image.resize '320x240' |
| |
312 |
# end |
|
bd7251c4
»
|
Squeegy |
2008-03-31 |
Added operators and a new s... |
313 |
def operate(&block) |
| |
314 |
returning self do |
|
53dc9e5b
»
|
Squeegy |
2008-04-05 |
@model is now available in ... |
315 |
proxy = ImageProxy.new(load_image, self) |
|
14fc591f
»
|
Squeegy |
2008-04-03 |
Added ImageProxy class that... |
316 |
block.call(proxy) |
| |
317 |
@output_image = proxy.image |
|
bd7251c4
»
|
Squeegy |
2008-03-31 |
Added operators and a new s... |
318 |
end |
| |
319 |
end |
| |
320 |
|
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
321 |
# Load the image from disk/DB, or return the cached and potentially |
| |
322 |
# processed output image. |
|
019dd1fe
»
|
Squeegy |
2008-03-30 |
documentation |
323 |
def load_image #:nodoc: |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
324 |
@output_image ||= @uploaded_image |
|
58fd79dc
»
|
Squeegy |
2008-05-21 |
ImageProxy#method_missing n... |
325 |
|
| |
326 |
# Return the current image if we have loaded it already |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
327 |
return @output_image if @output_image |
| |
328 |
|
|
58fd79dc
»
|
Squeegy |
2008-05-21 |
ImageProxy#method_missing n... |
329 |
# Load the image from disk |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
330 |
if self.class.db_store? |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
331 |
if image_file_data && image_file_data.any? |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
332 |
@output_image = Magick::Image.from_blob(image_file_data).first |
| |
333 |
else |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
334 |
master_image_not_found |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
335 |
end |
| |
336 |
else |
| |
337 |
@output_image = Magick::Image.read(file_path).first |
| |
338 |
end |
|
cd20f273
»
|
Squeegy |
2008-04-01 |
Added image preprocessing v... |
339 |
|
|
18112e3e
»
|
Squeegy |
2008-03-30 |
Added a more helpful except... |
340 |
rescue Magick::ImageMagickError => e |
| |
341 |
if e.to_s =~ /unable to open file/ |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
342 |
master_image_not_found |
|
18112e3e
»
|
Squeegy |
2008-03-30 |
Added a more helpful except... |
343 |
else |
| |
344 |
raise e |
| |
345 |
end |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
346 |
end |
| |
347 |
|
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
348 |
# Convert the current output image to a jpg, and return it in binary form. options support a |
| |
349 |
# :format key that can be :jpg, :gif or :png |
| |
350 |
def output_image(options = {}) #:nodoc: |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
351 |
format = (options[:format] || :jpg).to_s.upcase |
| |
352 |
@output_image.format = format |
|
3cae34cf
»
|
Squeegy |
2008-04-02 |
Image will now render out b... |
353 |
@output_image.strip! |
|
9208c4c6
»
|
Squeegy |
2008-04-02 |
Added output_image_jpg_qual... |
354 |
if format = 'JPG' |
| |
355 |
quality = self.class.output_image_jpg_quality |
| |
356 |
@output_image.to_blob { self.quality = quality } |
| |
357 |
else |
| |
358 |
@output_image.to_blob |
| |
359 |
end |
|
bd7251c4
»
|
Squeegy |
2008-03-31 |
Added operators and a new s... |
360 |
ensure |
| |
361 |
GC.start |
| |
362 |
end |
| |
363 |
|
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
364 |
# Delete the image file for this record. This is automatically ran after this record gets |
| |
365 |
# destroyed, but you can call it manually if you want to remove the image from the record. |
| |
366 |
def delete_image_file |
|
888e17dd
»
|
Squeegy |
2008-05-29 |
Fixed failing test. Thanks... |
367 |
File.delete(file_path) if !self.class.db_store? && File.exists?(file_path) |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
368 |
end |
| |
369 |
|
| |
370 |
# Execute image presence and validity validations. |
|
a6c1bde3
»
|
Squeegy |
2008-04-30 |
Fixed that overriding the v... |
371 |
def validate_image #:nodoc: |
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
372 |
field_name = (@image_file_url && @image_file_url.any?) ? :image_file_url : :image_file |
|
fd5812db
»
|
Squeegy |
2008-04-18 |
Fixed a minor validation me... |
373 |
|
| |
374 |
if @invalid_image |
|
b927dfa4
»
|
Squeegy |
2008-03-31 |
Add cleaner solution for UR... |
375 |
errors.add field_name, self.class.invalid_image_message |
|
fd5812db
»
|
Squeegy |
2008-04-18 |
Fixed a minor validation me... |
376 |
elsif self.class.require_image && !has_image? |
| |
377 |
errors.add field_name, self.class.missing_image_message |
|
0bd7bdeb
»
|
Squeegy |
2008-03-31 |
Validation is working. It ... |
378 |
end |
| |
379 |
end |
| |
380 |
|
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
381 |
private |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
382 |
# Perform pre save tasks. Preprocess the image, and write it to DB. |
| |
383 |
def pre_save |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
384 |
if @uploaded_image |
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
385 |
# perform preprocessing |
|
172165e6
»
|
Squeegy |
2008-04-02 |
only preprocess if there is... |
386 |
perform_preprocess_operation |
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
387 |
|
| |
388 |
# Convert to storage format |
| |
389 |
@uploaded_image.format = self.class.image_storage_format.to_s.upcase |
| |
390 |
|
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
391 |
# Write image data to the DB field |
| |
392 |
if self.class.db_store? |
| |
393 |
self.image_file_data = @uploaded_image.to_blob |
| |
394 |
end |
| |
395 |
end |
| |
396 |
end |
| |
397 |
|
| |
398 |
# Write image to file system and cleanup garbage. |
|
dbc4b868
»
|
Squeegy |
2008-04-06 |
renamed bad method name |
399 |
def post_save |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
400 |
if @uploaded_image && !self.class.db_store? |
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
401 |
# Make sure target directory exists |
|
91ab808c
»
|
Squeegy |
2008-03-30 |
creation date based file st... |
402 |
FileUtils.mkdir_p(directory_path) |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
403 |
|
|
76c65f3d
»
|
Squeegy |
2008-04-02 |
Allow master image storage ... |
404 |
# Write master image file |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
405 |
@uploaded_image.write(file_path) |
| |
406 |
end |
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
407 |
|
| |
408 |
# Cleanup temp files |
| |
409 |
delete_temp_image |
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
410 |
|
| |
411 |
# Start GC to close up memory leaks |
| |
412 |
GC.start if @uploaded_image |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
413 |
end |
|
cd20f273
»
|
Squeegy |
2008-04-01 |
Added image preprocessing v... |
414 |
|
| |
415 |
# Preprocess this image before saving |
| |
416 |
def perform_preprocess_operation |
| |
417 |
if self.class.preprocess_image_operation |
| |
418 |
operate(&self.class.preprocess_image_operation) |
|
9c53eb49
»
|
Squeegy |
2008-04-19 |
Fixed a bug that magic widt... |
419 |
set_magic_attributes #update width and height magic columns |
|
cd20f273
»
|
Squeegy |
2008-04-01 |
Added image preprocessing v... |
420 |
@uploaded_image = @output_image |
| |
421 |
end |
| |
422 |
end |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
423 |
|
|
3f1f6866
»
|
Squeegy |
2008-04-05 |
Added support for DB image ... |
424 |
# If any magic column names exists fill them with image meta data. |
|
9c53eb49
»
|
Squeegy |
2008-04-19 |
Fixed a bug that magic widt... |
425 |
def set_magic_attributes(file = nil) |
| |
426 |
if file && self.respond_to?(:image_filename=) |
|
305defb7
»
|
Squeegy |
2008-04-12 |
Fixed a bug that would caus... |
427 |
filename = file.original_filename if file.respond_to?(:original_filename) |
| |
428 |
filename = file.basename if file.respond_to?(:basename) |
| |
429 |
self.image_filename = filename |
| |
430 |
end |
|
52ef8a3d
»
|
Squeegy |
2008-04-05 |
Support image_filename imag... |
431 |
self.image_width = @uploaded_image.columns if self.respond_to?(:image_width=) |
| |
432 |
self.image_height = @uploaded_image.rows if self.respond_to?(:image_height=) |
| |
433 |
end |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
434 |
|
|
b532e66a
»
|
Squeegy |
2008-04-15 |
Support getting and setting... |
435 |
# Save the image in the rails tmp directory |
| |
436 |
def save_temp_image(file) |
| |
437 |
file_name = file.respond_to?(:original_filename) ? file.original_filename : file.path |
| |
438 |
@image_file_temp = file_name.split('/').last |
| |
439 |
path = "#{RAILS_ROOT}/tmp/fleximage" |
| |
440 |
FileUtils.mkdir_p(path) |
| |
441 |
File.open("#{path}/#{@image_file_temp}", 'w') do |f| |
| |
442 |
file.rewind |
| |
443 |
f.write file.read |
| |
444 |
end |
| |
445 |
end |
| |
446 |
|
| |
447 |
# Delete the temp image after its no longer needed |
| |
448 |
def delete_temp_image |
| |
449 |
FileUtils.rm_rf "#{RAILS_ROOT}/tmp/fleximage/#{@image_file_temp}" |
| |
450 |
end |
| |
451 |
|
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
452 |
# Load the default image, or raise an expection |
| |
453 |
def master_image_not_found |
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
454 |
# Load the default image from a path |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
455 |
if self.class.default_image_path |
|
1e45f1e7
»
|
Squeegy |
2008-04-19 |
default_image_path is now r... |
456 |
@output_image = Magick::Image.read("#{RAILS_ROOT}/#{self.class.default_image_path}").first |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
457 |
|
|
fb328f2b
»
|
Squeegy |
2008-05-31 |
Added default_image option ... |
458 |
# Or create a default image |
| |
459 |
elsif self.class.default_image |
| |
460 |
x, y = Fleximage::Operator::Base.size_to_xy(self.class.default_image[:size]) |
| |
461 |
color = self.class.default_image[:color] |
| |
462 |
|
| |
463 |
@output_image = Magick::Image.new(x, y) do |
| |
464 |
self.background_color = color if color && color != :transparent |
| |
465 |
end |
| |
466 |
|
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
467 |
# No default, not master image, so raise exception |
| |
468 |
else |
|
58fd79dc
»
|
Squeegy |
2008-05-21 |
ImageProxy#method_missing n... |
469 |
message = "Master image was not found for this record" |
|
94510c8e
»
|
Squeegy |
2008-04-12 |
Added default_image_path ac... |
470 |
|
| |
471 |
if !self.class.db_store? |
| |
472 |
message << "\nExpected image to be at:" |
| |
473 |
message << "\n #{file_path}" |
| |
474 |
end |
| |
475 |
|
| |
476 |
raise MasterImageNotFound, message |
| |
477 |
end |
| |
478 |
end |
|
0c565ce1
»
|
Squeegy |
2008-03-30 |
You can now apply the acts_... |
479 |
end |
| |
480 |
|
| |
481 |
end |
|
8d801232
»
|
jlsync |
2008-05-24 |
Don't need to delete file a... |
482 |
end |