Sha256: d9b607c20ccdfc93430159b9614245d36df31813cb4e9dbe4b159220e3d1f00f

Contents?: true

Size: 967 Bytes

Versions: 1

Compression:

Stored size: 967 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`

          File.open out_file_path, "rb" do |out_file|
            compressed_png_data = out_file.read
          end
          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

1 entries across 1 versions & 1 rubygems

Version Path
sprockets-image_compressor-0.2.3 lib/sprockets/image_compressor/png_compressor.rb