Skip to content

Instantly share code, notes, and snippets.

@southpolesteve
Created August 9, 2013 16:28
Show Gist options
  • Save southpolesteve/6195037 to your computer and use it in GitHub Desktop.
Save southpolesteve/6195037 to your computer and use it in GitHub Desktop.
A RSpec test that checks all your images for proper compression
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