项目图片无损压缩脚本

一丶

有时候项目工程太大,可以利用tinify 提供的api,进行压缩

二丶 准备工作

$gem install tinify

2.申请Key

https://tinypng.com/dashboard/api

三丶代码

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require "tinify" # gem install tinify

DIR_PATH = "文件夹路径"
TINIFY_KEY = "申请的key"  #https://tinypng.com/dashboard/api
Tinify.key = TINIFY_KEY

class ImageManager
   
    def initialize(path = "") 
        @root_path = path
        @imageSize_total = 0
        @all_imagePaths = []
        traverse(@root_path ) 
        puts "\033[31m------------ begin ------------\033[0m\n"
        puts "准备压缩文件列表:"
        @all_imagePaths.each { |filePath|
            puts File.basename(filePath).to_s
        }
     
    end
    
    def calculate_image_total
        @imageSize_total = 0
        @all_imagePaths.each { |filePath|
            @imageSize_total = @imageSize_total + File.size(filePath)/1024.0            
        }
    end

    def handle_image(filePath)
        puts "\033[32m >>>>正在压缩图片 : #{File.basename(filePath) } \033[0m\n"
        old_size = (File.size(filePath)/1024.0).to_s
        source = Tinify.from_file(filePath)
        source.to_file(filePath)
        new_size = (File.size(filePath)/1024.0).to_s
        puts "压缩前: #{old_size} 压缩后: #{new_size}"        
    end

    def traverse(filepath)
        if File.directory?(filepath)
            Dir.foreach(filepath) do |filename|
                if filename != "." and filename != ".."
                  traverse(filepath + "/" + filename)
                end
            end
        else
            if File.extname(filepath) == ".png"
                @all_imagePaths << filepath
            end            
        end
    end
    #compress_pictures
    def run
        puts "---------------->"
           calculate_image_total        
        puts "压缩前图片总大小: " + format("%.2f", @imageSize_total) + " KB"
        
        @all_imagePaths.each { |filePath|
           handle_image(filePath)         
        }
        calculate_image_total        
        puts "压缩后图片总大小: " + format("%.2f", @imageSize_total) + " KB"
      
        puts "<----------------"
        self.end
    end
    def end
        puts "\033[31m------------ end ------------\033[0m\n"        
    end
    
end

imgM = ImageManager.new(DIR_PATH)
imgM.run

四 丶

压缩前:


项目图片无损压缩脚本_第1张图片
image.png

压缩后:


项目图片无损压缩脚本_第2张图片
,

五 丶

https://github.com/k373379320/ZBScript/blob/master/Ruby/project_handle_resource.rb

你可能感兴趣的:(项目图片无损压缩脚本)