diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-31 15:10:41 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-31 15:10:41 +0000 |
commit | 2368893df711f330cd210005e616fc3b6003ff31 (patch) | |
tree | d48874fb1279741f8e97a7f397e46d14ac2df05f /tooling | |
parent | 729eabcb410add9dbcfa46677308003dc95a64d0 (diff) | |
download | gitlab-ce-2368893df711f330cd210005e616fc3b6003ff31.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'tooling')
-rw-r--r-- | tooling/lib/tooling/images.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tooling/lib/tooling/images.rb b/tooling/lib/tooling/images.rb new file mode 100644 index 00000000000..d0c464b983c --- /dev/null +++ b/tooling/lib/tooling/images.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +module Tooling + module Image + # Determine the tolerance till when we run pngquant in a loop + TOLERANCE = 10000 + + def self.check_executables + unless system('pngquant --version', out: File::NULL) + warn( + 'Error: pngquant executable was not detected in the system.', + 'Download pngquant at https://pngquant.org/ and place the executable in /usr/local/bin' + ) + abort + end + + unless system('gm version', out: File::NULL) + warn( + 'Error: gm executable was not detected in the system.', + 'Please install imagemagick: brew install imagemagick or sudo apt install imagemagick' + ) + abort + end + end + + def self.compress_image(file, keep_original = false) + check_executables + + compressed_file = "#{file}.compressed" + FileUtils.copy(file, compressed_file) + + pngquant_file = PngQuantizator::Image.new(compressed_file) + + # Run the image repeatedly through pngquant until + # the change in file size is within TOLERANCE + # or the loop count is above 1000 + 1000.times do + before = File.size(compressed_file) + pngquant_file.quantize! + after = File.size(compressed_file) + break if before - after <= TOLERANCE + end + + savings = File.size(file) - File.size(compressed_file) + is_uncompressed = savings > TOLERANCE + + if is_uncompressed && !keep_original + FileUtils.copy(compressed_file, file) + end + + FileUtils.remove(compressed_file) + + [is_uncompressed, savings] + end + end +end |