tinify-ruby icon indicating copy to clipboard operation
tinify-ruby copied to clipboard

how can i optimize the compress process?

Open edison0951 opened this issue 7 years ago • 0 comments

Now I use the tinify-ruby script to compress our images. And I process 160 images that need almost nine minutes. This is my code:

@@index = 0

@@api_keys = []

def self.tinify(file_path)

    if @@api_keys.length == 0

      UI.message("no api keys found")

      return

    end

    Tinify.key = @@api_keys[@@index]

    UI.message("current api key is: #{Tinify.key}")

    file_name = File.basename file_path

    UI.message("#{file_name} start to ready compress image")

    begin

      source = Tinify.from_file file_path

      source.to_file file_path

      UI.message("#{file_name} compress success!")

      @@index += 1

      if @@index == @@api_keys.length

        @@index = 0

      end

    rescue Tinify::AccountError => e

      @@index += 1

      if @@index == @@api_keys.length

        @@index = 0

      end

      tinify file_path

    rescue => e

      UI.message("#{file_name} compress failure!")

      @@index += 1

      if @@index == @@api_keys.length

        @@index = 0

      end

    end

 end

how can I optimize the process time?

edison0951 avatar Dec 10 '18 08:12 edison0951