Skip to content

Commit

Permalink
测试工程师,必备图片处理工具
Browse files Browse the repository at this point in the history
  • Loading branch information
李银池 committed May 24, 2020
1 parent cdef641 commit b6d07fd
Show file tree
Hide file tree
Showing 42 changed files with 1,619 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.history
/back-end
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
50 changes: 50 additions & 0 deletions ImageDownload/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const cheerio = require('cheerio');
var fs = require('fs');
var request = require('request');
var chalk = require('chalk');
var program = require('commander');
var CurrentFilePath=__dirname;
var FilePath;
program
.version('1.0.0')
.option('-p, --FilePath [type]', '下载后图片文件存放位置', "image")
.parse(process.argv);
// 判断是否有入参
console.log(chalk.green.bold('params:'));
if (program.FilePath === undefined) {
console.log(chalk.green.bold('no FilePath'));;
} else if (program.FilePath === true) {
console.log(chalk.green.bold('add FilePath'));;
} else {
console.log(chalk.green.bold(`add FilePath type ${chalk.blue.bold(program.FilePath)}`));
FilePath = program.FilePath;
}

fs.readFile(`${CurrentFilePath}/index.html`,"utf-8",function(err,data){
if(err) {
console.log("index.html loading is failed :"+err);
}
else{
var $ = cheerio.load(data);
var tr_list = $('table tr');
tr_list.each((index, item) => {
var imgName = $(item).find('td').eq(0).text();
var imgUrl = $(item).find('td').eq(1).text();
var writeStream = fs.createWriteStream(`${CurrentFilePath}/${FilePath}/${imgName}${imgUrl.substring(imgUrl.lastIndexOf('.'))}`);
var readStream = request(imgUrl);
readStream.pipe(writeStream);
readStream.on('end', function() {
console.log(`${chalk.blue.bold(imgName)} ${chalk.blue.bold("Download Finish")} ${chalk.green.bold("Finish")}`);
});
readStream.on('error', function() {
console.log(chalk.red.bold("Error:" + err))
});
writeStream.on("finish", function() {
console.log(`${chalk.blue.bold(imgName)} ${chalk.blue.bold("Save")} ${chalk.green.bold("Success")}`);
writeStream.end();
});
});
}

});

22 changes: 22 additions & 0 deletions ImageDownload/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<meta charset="utf-8">
</head>

<body>
<table border="1">
<tr>
<td>图片1</td>
<td>https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png</td>
</tr>
<tr>
<td>图片2</td>
<td>https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin/1.jpg</td>
</tr>
<tr>
<td>图片3</td>
<td>https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin/2.jpg</td>
</tr>
</table>
</body>
</html>
Binary file added Made/image/format.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions Made/made.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var sharp = require("sharp");
var chalk = require("chalk");
var program = require('commander');
var imageFormat;
var width;
var height;
var inputImage;
var outputImage;
program
.version('1.0.0')
.option('-f, --imageFormat [type]', '生成图片的格式,例如:jpg、jpeg、png、bmp、gif、webp、tiff、svg等', "jpg")
.option('-w, --width [type]', '生成图片的宽(单位:像素)', 200)
.option('-e, --height [type]', '生成图片的 图片存放位置', 200)
.option('-i, --inputImage [type]', '原始图片存放位置', "Made/image/format.jpg")
.option('-o, --outputImage [type]', '生成图片后,图片所存放的置', "Made/output/format")
.parse(process.argv);

console.log(chalk.green.bold('params:'));
if (program.imageFormat === undefined) {
console.log(chalk.green.bold('no imageFormat'));;
} else if (program.imageFormat === true) {
console.log(chalk.green.bold('add imageFormat'));;
} else {
console.log(chalk.green.bold(`add imageFormat type ${chalk.blue.bold(program.imageFormat)}`));
imageFormat = program.imageFormat;
}
if (program.width === undefined) {
console.log(chalk.green.bold('no width'));;
} else if (program.width === true) {
console.log(chalk.green.bold('add width'));;
} else {
console.log(chalk.green.bold(`add width type ${chalk.blue.bold(program.width)}`));
width = program.width;
}
if (program.height === undefined) {
console.log(chalk.green.bold('no height'));;
} else if (program.height === true) {
console.log(chalk.green.bold('add height'));;
} else {
console.log(chalk.green.bold(`add height type ${chalk.blue.bold(program.height)}`));
height = program.height;
}
if (program.inputImage === undefined) {
console.log(chalk.green.bold('no inputImage'));;
} else if (program.inputImage === true) {
console.log(chalk.green.bold('add inputImage'));;
} else {
console.log(chalk.green.bold(`add inputImage type ${chalk.blue.bold(program.inputImage)}`));
inputImage = program.inputImage;
}
if (program.outputImage === undefined) {
console.log(chalk.green.bold('no outputImage'));;
} else if (program.outputImage === true) {
console.log(chalk.green.bold('add outputImage'));;
} else {
console.log(chalk.green.bold(`add outputImage type ${chalk.blue.bold(program.outputImage)}`));
outputImage = program.outputImage;
}


sharp(inputImage).resize(Number(width), Number(height)).toFile(`${outputImage}.${imageFormat}`, function (err) {
if (err) {
console.error(chalk.red.bold(err));
}
console.log(chalk.green.bold("Success")+" 执行完毕");
console.log(chalk.green.bold("Please view file dir")+" Made/output");
})
Binary file added Rename/image/图片1.jpg
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 Rename/image/图片2.jpg
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 Rename/image/图片3.jpg
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 Rename/image/图片4.jpg
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 Rename/image/图片5.jpg
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 Rename/image/图片6.jpg
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 Rename/image/图片7.jpg
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 Rename/image/图片8.jpg
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 Rename/image/图片9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions Rename/rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

var fs = require("fs");
var chalk = require("chalk");
var program = require('commander');
var newFileName;
var newFileFormat;
var newFilePath;
program
.version('1.0.0')
.option('-n, --newFileName [type]', '图片名称', "Image")
.option('-f, --newFileFormat [type]', '图片格式', "bmp")
.option('-p, --newFilePath [type]', '图片存放位置', "image")
.parse(process.argv);
console.log(chalk.green.bold('params:'));
if (program.newFileName === undefined) {
console.log(chalk.green.bold('no newFileName'));;
} else if (program.newFileName === true) {
console.log(chalk.green.bold('add newFileName'));;
} else {
console.log(chalk.green.bold(`add newFileName type ${chalk.blue.bold(program.newFileName)}`));
newFileName = program.newFileName;
}
if (program.newFileFormat === undefined) {
console.log(chalk.green.bold('no newFileFormat'));;
} else if (program.newFileFormat === true) {
console.log(chalk.green.bold('add newFileFormat'));;
} else {
console.log(chalk.green.bold(`add newFileFormat type ${chalk.blue.bold(program.newFileFormat)}`));
newFileFormat = program.newFileFormat;
}
if (program.newFilePath === undefined) {
console.log(chalk.green.bold('no newFilePath'));;
} else if (program.newFileName === true) {
console.log(chalk.green.bold('add newFilePath'));;
} else {
console.log(chalk.green.bold(`add newFilePath type ${chalk.blue.bold(program.newFilePath)}`));
newFilePath = program.newFilePath;
}
var path = `${__dirname}/${newFilePath}`;
fs.readdir(path, function (err, files) {
if (err) {
return console.error(chalk.red(err));
}
var n = 0;
files.forEach(function (file) {
n++;
if (file.replace('^.*\.(jpg|gif|png|bmp)$i')) {
var oldfilePath = path + `/${file}`;
var newFilePath = `${path}/${newFileName}${n}.${newFileFormat}`;
console.log(`${chalk.green('Batch Rename Success ')}${chalk.green.blue(newFileName)}${chalk.yellow.bold(n)}.${chalk.blue.bold(newFileFormat)}`);
fs.rename(oldfilePath, newFilePath, function (error) {
if (error) {
console.error(`${chalk.red(err)}`);
}

})
}
});
})
Binary file added Resize/image/resize.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions Resize/resize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var sharp = require("sharp");
var chalk = require("chalk");
var program = require('commander');
var inputImage;
var width;
var height;
var outputImage;program
.version('1.0.0')
.option('-i, --inputImage [type]', '被修改图片存放位置', "Resize/image/resize.jpg")
.option('-w, --width [type]', '宽(单位:像素)', 200)
.option('-e, --height [type]', '图片存放位置', 200)
.option('-o, --outputImage [type]', '被修改后的图片存放位置', "Resize/output/resize.jpg")
.parse(process.argv);
console.log(chalk.green.bold('params:'));
if (program.inputImage === undefined) {
console.log(chalk.green.bold('no inputImage'));;
} else if (program.inputImage === true) {
console.log(chalk.green.bold('add inputImage'));;
} else {
console.log(chalk.green.bold(`add inputImage type ${chalk.blue.bold(program.inputImage)}`));
inputImage = program.inputImage;
}
if (program.width === undefined) {
console.log(chalk.green.bold('no width'));;
} else if (program.width === true) {
console.log(chalk.green.bold('add width'));;
} else {
console.log(chalk.green.bold(`add width type ${chalk.blue.bold(program.width)}`));
width = program.width;
}
if (program.height === undefined) {
console.log(chalk.green.bold('no height'));;
} else if (program.newFileName === true) {
console.log(chalk.green.bold('add height'));;
} else {
console.log(chalk.green.bold(`add height type ${chalk.blue.bold(program.height)}`));
height = program.height;
}
if (program.outputImage === undefined) {
console.log(chalk.green.bold('no outputImage'));;
} else if (program.newFileName === true) {
console.log(chalk.green.bold('add outputImage'));;
} else {
console.log(chalk.green.bold(`add outputImage type ${chalk.blue.bold(program.outputImage)}`));
outputImage = program.outputImage;
}sharp(inputImage).resize(Number(width), Number(height)).toFile(outputImage, function (err) {
if (err) {
console.error(chalk.red.bold(err));
}
console.log(chalk.green.bold("Success")+" 执行完毕");
})



Binary file added WaterMake/image/watermake.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b6d07fd

Please sign in to comment.