Skip to content

how can i optimize the compress process? #4

Description

@edison0951

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions