javascript - 使用 Gulp 从数组中批量重命名图像
问题描述
我想使用 Gulp 批量调整和重命名图像。我有一项任务是根据文件的尺寸重命名文件,但我想在文件名中使用简写,比如-small
and-large
并且只使用数值来调整大小的过程。
我尝试将键:值对数组的一些迭代添加到该.forEach
行...
[small:100, large:1000].forEach(function (size)
[{small:100, large:1000}].forEach(function (size)
...然后尝试定位图像imageResize
行中的键/值,其中“k”将是键。这对我不起作用,到目前为止我还没有成功地跟踪这部分。
.pipe(rename(function (path) { path.basename = `${path.basename}-${size[k]}px`; }))
这是当前任务,它完成了工作。它基于 调整图像大小size
,然后size
作为字符串附加到文件名。
// Optimize Images
gulp.task("images", function (done) {
[100, 1000].forEach(function (size) {
return gulp.src(imagesSRC)
.pipe(newer(imagesDestSite)) // get newer images
.pipe(imageResize({ width: size })) // resize each according to array above
.pipe(rename(function (path) { path.basename = `${path.basename}-${size}px`; })) // put size in filename
.pipe(imagemin())
.pipe(gulp.dest(imagesDest))
.pipe(browserSync.reload({stream:true}))
.pipe(gulp.dest(imagesDestSite))
});
done();
});
该任务的输出当前如下所示:
image1-100px.jpg
image1-1000px.jpg
image2-100px.jpg
image2-1000px.jpg
哪个有效,但我希望输出是这样的:
image1-small.jpg
image1-large.jpg
image2-small.jpg
image2-large.jpg
任何指针将不胜感激,并在此先感谢!
解决方案
推荐阅读
- c++ - 自 C++20 以来是否允许对分配的存储进行指针运算?
- android - setGradientCenter (gradientDrawable.setGradientCenter) 在 GradientDrawable 中不起作用
- vue.js - 在循环中绑定类项
- java - Mockito.when thenReturn 基于输入参数
- reactjs - 对 FieldArray 中的 Formik 字段使用自定义输入时,输入失去焦点
- excel - 将单元格值拆分为多行而不从其他单元格复制数据
- python - 找到熊猫时间序列之间的相关性
- sql - 使用 D365 中另一个表中的字段过滤父数据源
- css - 为什么这个固定元素不水平居中?
- google-cloud-firestore - AngularFire - 如何设置日志记录级别?