class ImageTinify::ImageTinify
Attributes
root_only[RW]
Public Class Methods
new(tiny_png_key = nil)
click to toggle source
# File lib/image_tinify.rb, line 23 def initialize(tiny_png_key = nil) @validate = false Tinify.key = tiny_png_key end
Public Instance Methods
_find_all_images(path, block)
click to toggle source
查找所有图片
# File lib/image_tinify.rb, line 41 def _find_all_images(path, block) if File.directory? path return if @root_only && path != @root_dir Dir.foreach(path) .reject { |x| x.start_with? '.' } .each { |name| _find_all_images File.join(path, name), block } else block.call path if TARGET_EXT.include? File.extname(path) end end
compress_image(path)
click to toggle source
压缩图片
# File lib/image_tinify.rb, line 78 def compress_image(path) validate! unless validate? Tinify.from_file(path).to_file(path) true end
find_all_images(path, block)
click to toggle source
# File lib/image_tinify.rb, line 52 def find_all_images(path, block) @root_dir = path _find_all_images(path, block) end
find_uncompressed_images(images) { |file| ... }
click to toggle source
查找所有未压缩的图片
# File lib/image_tinify.rb, line 58 def find_uncompressed_images(images) images.each { |file| begin stream = ChunkyPNG::Datastream.from_file(file) compressed = stream.other_chunks .select { |ck| ck.type.downcase == 'itxt' } .reduce(false) { |m, ck| m || (ck.keyword == MARKUP_KEY && ck.text == MARKUP_VALUE) } yield file if block_given? && !compressed rescue => e puts e.to_s end } end
mark_compressed(path)
click to toggle source
标记已压缩
# File lib/image_tinify.rb, line 85 def mark_compressed(path) stream = ChunkyPNG::Datastream.from_file(path) stream.other_chunks << ChunkyPNG::Chunk::InternationalText.new(MARKUP_KEY, MARKUP_VALUE) stream.save(path) true end
relative_path(path)
click to toggle source
# File lib/image_tinify.rb, line 36 def relative_path(path) path.gsub(@root_dir, '.') end
validate!()
click to toggle source
# File lib/image_tinify.rb, line 32 def validate! @validate = Tinify.validate! end
validate?()
click to toggle source
# File lib/image_tinify.rb, line 28 def validate? @validate end