Sha256: cfc69027c5151a8e7dea4006ced4904513f4e4ede84f9d830ac3576d81fe5b71

Contents?: true

Size: 908 Bytes

Versions: 2

Compression:

Stored size: 908 Bytes

Contents

require "sprockets/image_compressor/base"

module Sprockets
  module ImageCompressor
    class PngCompressor < Base
      def initialize
        @name = "pngcrush"
      end

      def compress(content)
        compressed_png_data = ""
        Tempfile.open ["in_file", ".png"] do |in_file|
          in_file.binmode
          out_file_path = in_file.path + ".optimized.png"
          in_file.write content
          in_file.close

          out = `#{binary_path} #{in_file.path} #{out_file_path} 2>&1`
          compressed_png_data = IO.binread(out_file_path)
          File.unlink out_file_path
        end
        compressed_png_data

      rescue Errno::ENOENT => e
        # error during compression so out_file not found
        # return original content as a fallback
        warn "sprockets-image_compressor: PNG compression failed... returning original."
        content
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sprockets-image_compressor-0.3.0 lib/sprockets/image_compressor/png_compressor.rb
sprockets-image_compressor-0.2.4 lib/sprockets/image_compressor/png_compressor.rb