This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from dariuszor/feature/compress_image
Feature/compress image
- Loading branch information
Showing
11 changed files
with
174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
debug = require('debug')('s3utils-compress') | ||
Promise = require 'bluebird' | ||
fs = Promise.promisifyAll require('fs') | ||
imagemin = require('imagemin') | ||
_ = require 'underscore' | ||
|
||
###* | ||
* Compress images with basic configuration from Imagemin librabry | ||
### | ||
class Compress | ||
|
||
@compressImage: (source, destination, extension) -> | ||
new Promise (resolve, reject) -> | ||
debug 'About to compress image from path: %s', source | ||
switch extension | ||
when "png" then plugin = imagemin.optipng({optimizationLevel: 3}) | ||
when "gif" then plugin = imagemin.gifsicle({interlaced: true}) | ||
when "svg" then plugin = imagemin.svgo() | ||
else plugin = imagemin.jpegtran({progressive: true}) | ||
|
||
fs.stat source, (err, stat) -> | ||
if err == null | ||
(new imagemin) | ||
.src(source) | ||
.dest(destination) | ||
.use(plugin) | ||
.run (err) -> | ||
if err | ||
reject 'Error while compressing file: ' + err | ||
else | ||
resolve true | ||
else | ||
debug 'Error while getting stats for source: %s, with error %s', source, err | ||
reject 'Error while getting stats for source: ' + source | ||
|
||
module.exports = Compress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
_ = require 'underscore' | ||
Promise = require 'bluebird' | ||
Compress = require '../lib/compress' | ||
fs = Promise.promisifyAll require('fs') | ||
|
||
examplesFolder = "" + __dirname + "/../examples/" | ||
compressedFolder = examplesFolder + "compressed/" | ||
describe 'Compress', -> | ||
|
||
beforeEach -> | ||
try | ||
fs.mkdirSync compressedFolder | ||
catch e | ||
if e.code != 'EEXIST' | ||
throw e | ||
|
||
afterEach -> | ||
fs.rmdirSync compressedFolder | ||
|
||
_.each [ | ||
"darthMeow.jpg" | ||
"saberCat.gif" | ||
"stormtroopocat.png" | ||
"git-cat.svg" | ||
"productImage.jpg" | ||
], (fileName) -> | ||
it "should compress file (#{fileName})", (done) -> | ||
extension = fileName.substr(fileName.length - 3) | ||
sourcePath = examplesFolder + fileName | ||
destinationPath = compressedFolder + fileName | ||
ext = Compress.compressImage sourcePath, compressedFolder, extension | ||
.then (result) -> | ||
expect(result).toBe(true) | ||
fileSizeInBytesBeforeCompressing = fs.statSync(sourcePath)["size"] | ||
fileSizeInBytesAfterCompressing = fs.statSync(destinationPath)["size"] | ||
expect(fileSizeInBytesBeforeCompressing).not.toBeLessThan(fileSizeInBytesAfterCompressing) | ||
fs.unlinkAsync destinationPath | ||
.then -> done() | ||
.catch (error) -> done(error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters