Module: CarrierWave::OptimzeImage

Defined in:
lib/carrierwave-optimize-image.rb

Instance Method Summary collapse

Instance Method Details

#check_file_format?Boolean


16
17
18
19
20
21
22
# File 'lib/carrierwave-optimize-image.rb', line 16

def check_file_format?
	if file.content_type.include?("gif") || file.content_type.include?("video")
		false
	else 
		true
	end
end

#create_temp_imageObject


28
29
30
31
32
33
34
# File 'lib/carrierwave-optimize-image.rb', line 28

def create_temp_image
	tempfile = Tempfile.new(["#{file.filename}_#{Time.current.to_i}", File.extname(file.path)])
	tempfile.binmode
	tempfile.write(to_blob)
	ffmpeg(tempfile)
	tempfile.unlink
end

#ffmpeg(tempfile) ⇒ Object


40
41
42
# File 'lib/carrierwave-optimize-image.rb', line 40

def ffmpeg(tempfile)
	 system(" ffmpeg -y -i #{tempfile.path}  -q:v #{@level} #{current_path}")  
end

#lossless_ffmpeg_compressionObject


24
25
26
# File 'lib/carrierwave-optimize-image.rb', line 24

def lossless_ffmpeg_compression
	 create_temp_image
end

#optimize(options = {}) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/carrierwave-optimize-image.rb', line 3

def optimize(options = {})
	@level = options[:level].present? ? options[:level].to_i : 30
	begin
		
	if check_file_format?
		lossless_ffmpeg_compression
	end
		
	rescue Exception => e
		puts e
	end
end

#to_blobObject


36
37
38
# File 'lib/carrierwave-optimize-image.rb', line 36

def to_blob
	File.binread(file.path)
end