19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/bcms_thumbnail/bcms_thumbnail.rb', line 19
def thumbnail_engine(attachment_obj,geometry = '100x100',square = false, quality = 85)
if ! attachment_obj.blank? && attachment_obj.respond_to?('attachment') && ['jpg','png','gif','bmp'].include?(attachment_obj.attachment.file_extension.downcase)
thumbnail_location = "/bcms_thumbnail_cache/#{geometry}/#{attachment_obj.attachment.file_location.gsub(/[\\\/]/,'-')}#{(square) ? '-square' : ''}#{quality}.jpg"
if ! File.exists?("#{RAILS_ROOT}/public#{thumbnail_location}")
if ! File.exists?("#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}")
FileUtils.mkdir_p("#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}")
FileUtils.chmod 0755, "#{RAILS_ROOT}/public/bcms_thumbnail_cache/"
FileUtils.chmod 0755, "#{RAILS_ROOT}/public/bcms_thumbnail_cache/#{geometry}"
end
image = MiniMagick::Image.from_file("#{RAILS_ROOT}/tmp/uploads/#{attachment_obj.attachment.file_location}")
image.quality quality
if square
if image[:width] < image[:height]
remove = ((image[:height] - image[:width])/2).round
image.shave "0x#{remove}"
elsif image[:width] > image[:height]
remove = ((image[:width] - image[:height])/2).round
image.shave "#{remove}x0"
end
image.resize "#{geometry}x#{geometry}"
else
image.resize geometry
end
image.write("#{RAILS_ROOT}/public#{thumbnail_location}")
FileUtils.chmod 0644, "#{RAILS_ROOT}/public#{thumbnail_location}"
URI::escape(thumbnail_location)
else
URI::escape(thumbnail_location)
end
else
logger.warn("bcms_thumbnail: Either the attachment object doesn't accept attachments, you passed us a blank object, or the attachment type can't be thumbnailed.")
'/image-not-found'
end
end
|