Module: ActionView::Helpers

Defined in:
lib/bcms_thumbnail/bcms_thumbnail.rb

Class Method Summary collapse

Class Method Details

.thumbnail(attachment_obj, geometry = '100x100', quality = 85) ⇒ Object



7
8
9
# File 'lib/bcms_thumbnail/bcms_thumbnail.rb', line 7

def thumbnail(attachment_obj,geometry = '100x100', quality = 85)
  thumbnail_engine(attachment_obj, geometry, false, quality)
end

.thumbnail_engine(attachment_obj, geometry = '100x100', square = false, quality = 85) ⇒ Object



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

.thumbnail_square(attachment_obj, geometry = '100', quality = 85) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/bcms_thumbnail/bcms_thumbnail.rb', line 11

def thumbnail_square(attachment_obj,geometry = '100', quality = 85)
  fixed_geometry = geometry
  if geometry.match(/\D/)
    fixed_geometry = geometry.split(/\D/)[0]
  end
  thumbnail_engine(attachment_obj, geometry, true, quality)
end