Skip to content

Instantly share code, notes, and snippets.

@sr3d
Created August 21, 2010 15:43
Show Gist options
  • Save sr3d/542469 to your computer and use it in GitHub Desktop.
Save sr3d/542469 to your computer and use it in GitHub Desktop.
require 'ftools'
require 'fileutils'
require 'rubygems'
require 'RMagick'
include Magick
require 'open3'
def merge( files = [] )
merged_file = ""
files.each do |file|
File.open( file, "r") { |f|
merged_file += f.read + "\n"
}
end
merged_file
end
def minify( string = '', type = 'js' )
cmd = "java -jar bin/yuicompressor-2.4.2.jar --type #{type}"
stdin, stdout, stderr = Open3.popen3( cmd )
stdin.write string
stdin.close
stdout.read
end
def minify_to_file( string, type, output )
cmd = "java -jar bin/yuicompressor-2.4.2.jar -v --type #{type} -o #{output} --charset utf-8"
# puts cmd
stdin, stdout, stderr = Open3.popen3( cmd )
stdin.write string
stdin.close
puts stderr.read
end
def string2png( string, output )
image = Magick::Image.new 1,string.length
for i in 0 .. string.length
color = '#%02X0000' % string[i]
pixel = Magick::Pixel.from_color color
image.pixel_color 0, i, pixel
end
image.compression = ZipCompression
image.write("png8:"+ output)
image = nil
end
def png2string(image)
string = ''
Image.read(File.open( image, "r" )).first.each_pixel do |px,x,y|
string += (px.red % 256).chr
end
string
end
def string2pngs( string, output )
puts "original string length #{string.length}"
limit = 8192 #8192 # IE9 max for getImageData
images_count = (string.length * 1.0 / limit ).ceil
files = []
for i in 0 ... images_count
filename = output.split('.')
filename[ filename.length - 2 ] += i.to_s
filename = filename.join('.')
files << filename
from = i * limit
to = from + [ limit, string.length - from ].min
puts "output from #{from} to #{to}"
string2png string[from,to], filename
end
# verify
# s = ''
# files.each{ |file| s += png2string(file) }
# raise ('invalid string, expecting ' + s + "\n\n---but got---\n\n " + string) unless s == string
# total_size = 0
# files.each{ |file| puts "#{"%-20.20s" % file} #{"%30s" % File.stat( file ).size}"; total_size += File.stat( file ).size }
# puts total_size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment