Module: DataMapper::FastenTheSeatBelt::Compression

Defined in:
lib/dm-fasten-the-seat-belt/fasten-the-seat-belt/compression.rb

Constant Summary collapse

COMPRESSABLE_MIME_TYPES =
["image/jpeg", "image/jpg", "image/pjpeg"]

Instance Method Summary collapse

Instance Method Details

#compress_jpeg(filename, quality) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/dm-fasten-the-seat-belt/fasten-the-seat-belt/compression.rb', line 14

def compress_jpeg(filename, quality)
  if compressable?
    system("jpegoptim -m#{quality} -q --strip-all \"#{filename}\" &> /dev/null")
  else
    puts "FastenTheSeatBelt says: Quality setting not supported for #{self.content_type} files"
  end
end

#compress_now!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/dm-fasten-the-seat-belt/fasten-the-seat-belt/compression.rb', line 22

def compress_now!
  return false if self.images_are_compressed
  self.class.fasten_the_seat_belt_options[:thumbnails].each_pair do |key, value|
    thumb_filename = absolute_path(key)

    compress_jpeg(thumb_filename, value[:quality].to_i) if value[:quality] and value[:quality].to_i < 100
  end

  self.update_attributes(:images_are_compressed => true)
end

#compressable?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/dm-fasten-the-seat-belt/fasten-the-seat-belt/compression.rb', line 6

def compressable?
  COMPRESSABLE_MIME_TYPES.include?(self.content_type)
end

#dont_compress_now!Object



10
11
12
# File 'lib/dm-fasten-the-seat-belt/fasten-the-seat-belt/compression.rb', line 10

def dont_compress_now!
  self.images_are_compressed = false
end