This repository has been archived by the owner on Apr 19, 2020. 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.
- Loading branch information
1 parent
4fb07e3
commit 94e7121
Showing
4 changed files
with
28 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,13 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const filesize = require('filesize') | ||
const imageSize = require('image-size') | ||
const resizeImage = require('resize-img') | ||
|
||
function getOutputFileName(input) { | ||
const inputPath = path.parse(input) | ||
return path.resolve(inputPath.dir, inputPath.name + '-lowly' + inputPath.ext) | ||
} | ||
|
||
function lowly(input) { | ||
const fileInput = fs.readFileSync(input) | ||
const fileOutput = getOutputFileName(input) | ||
|
||
const dimensions = imageSize(fileInput) | ||
function lowly(image) { | ||
const dimensions = imageSize(image) | ||
const finalWidth = 30 | ||
const finalHeight = Math.round(finalWidth / dimensions.width * dimensions.height) | ||
resizeImage(fileInput, {width: finalWidth, height: finalHeight}) | ||
.then(buff => { | ||
fs.writeFileSync(fileOutput, buff) | ||
console.log('Final size: ' + filesize(buff.length)) | ||
}) | ||
const finalHeight = Math.round( | ||
finalWidth / dimensions.width * dimensions.height | ||
) | ||
return resizeImage(image, {width: finalWidth, height: finalHeight}) | ||
} | ||
|
||
module.exports = lowly |
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
Binary file not shown.
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const test = require('tape') | ||
const lowly = require('../') | ||
import fs from 'fs' | ||
import path from 'path' | ||
import test from 'ava' | ||
import pify from 'pify' | ||
import lowly from '../' | ||
|
||
test('lowly', t => { | ||
const initialSize = fs.readFileSync( | ||
path.resolve(__dirname, 'fixture.jpeg') | ||
).length | ||
lowly(path.resolve(__dirname, 'fixture.jpeg')) | ||
const finalSize = fs.readFileSync( | ||
path.resolve(__dirname, 'fixture-lowly.jpeg') | ||
).length | ||
const readFile = pify(fs.readFile) | ||
|
||
t.ok(initialSize > finalSize, 'the final size should be much lower') | ||
t.end() | ||
test('lowly', async t => { | ||
const image = await readFile(path.resolve(__dirname, 'fixture.jpeg')) | ||
const lowlyImage = await lowly(image) | ||
|
||
const initialSize = image.length | ||
const finalSize = lowlyImage.length | ||
|
||
t.true(initialSize > finalSize, 'the final size should be much lower') | ||
}) |