Created
August 9, 2013 16:28
-
-
Save southpolesteve/6195037 to your computer and use it in GitHub Desktop.
A RSpec test that checks all your images for proper compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
require 'fastimage' | |
describe "Image Compression" do | |
Dir[Rails.root.join("app/assets/images/*.{jpg,png,gif}")].each do |file| | |
context "#{file}" do | |
it "should be properly dimensioned" do | |
FastImage.size(file).each do |dim| | |
dim.should < 2000 # pixels | |
end | |
end | |
it "should be properly sized" do | |
File.size(file).should < 358400 #350kb | |
end | |
it "should be properly compressed" do | |
pixels = FastImage.size(file).inject(:*) | |
if pixels > 1000 | |
size = File.size(file) | |
(size.to_f/pixels.to_f).should < 2.5 #bytes/pixels ratio | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment