Skip to content

Commit

Permalink
bump version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
4Bos committed Nov 13, 2019
1 parent 4ee31a9 commit 6a7fe9b
Show file tree
Hide file tree
Showing 13 changed files with 4,541 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/node_modules
/result
13 changes: 13 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const gulp = require('gulp');
const gulpStubImage = require('./index');
const del = require('del');

function clean() {
return del('result');
}

function stub() {
return gulp.src('images/**/*').pipe(gulpStubImage()).pipe(gulp.dest('result'));
}

exports.default = gulp.series(clean, stub);
Binary file added images/i1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i28x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i32x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i512x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/i64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const jimp = require('jimp');
const File = require('vinyl');
const PluginError = require('plugin-error');
const path = require('path');
const Transform = require('stream').Transform;
const PLUGIN_NAME = 'StubImage';
const loadFont = jimp.loadFont(jimp.FONT_SANS_128_BLACK);


function getMimeType(extension) {
extension = extension.substr(1).toLowerCase();

switch (extension) {
case 'jpeg':
case 'jpg':
return jimp.MIME_JPEG;
case 'png':
return jimp.MIME_PNG;
}

return null;
}


function fillImage(image, color) {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, offset) {
this.bitmap.data.writeUInt32BE(color, offset);
});
}


/**
* @param font
* @param {string} text
*/
function measureText(font, text) {
const w = jimp.measureText(font, text);
const h = jimp.measureTextHeight(font, text, w + 10);

return [w, h];
}


class StubImageTransform extends Transform {
/**
* @param {File} file
* @param {string=} encoding
* @param {function(Error, object)} callback
* @private
*/
_transform(file, encoding, callback) {
if (file.isNull()) {
callback(null, file);
} else if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!'));
} else if (file.isBuffer()) {
const extension = path.extname(path.basename(file.path));
const mimeType = getMimeType(extension);

if (!mimeType) {
this.emit('error', new PluginError(PLUGIN_NAME, 'File type not supported'));
return;
}

jimp.read(file.contents).then(async image => {
const w = image.bitmap.width;
const h = image.bitmap.height;

fillImage(image, 0xE5E5E5FF);

if (w >= 32 && h >= 16) {
const text = `${w}x${h}`;
const font = await loadFont;
const [textW, textH] = measureText(font, text);

const textImage = new jimp(textW, textH);

textImage.print(font, 0, 0, text);
textImage.color([
{apply: 'red', params: [128]},
{apply: 'green', params: [128]},
{apply: 'blue', params: [128]}
]);

const scaleFactor = Math.min(1, (w * .8) / textW, (h * .8) / textH);
const textNewW = Math.round(textW * scaleFactor);
const textNewH = Math.round(textH * scaleFactor);

textImage.resize(textNewW, textNewH);

image.composite(
textImage,
Math.round(w / 2 - textNewW / 2),
Math.round(h / 2 - textNewH / 2)
);
}

image.getBuffer(mimeType, (error, buffer) => {
file.contents = buffer;

callback(error, file);
});
}).catch(error => {
callback(error, file);
});
}
}
}


module.exports = function() {
return new StubImageTransform({objectMode: true});
};
Loading

0 comments on commit 6a7fe9b

Please sign in to comment.