如何用毛模板将图像重命名?

发布于 2025-02-09 21:59:33 字数 1746 浏览 1 评论 0原文

我的图像具有不同的标题:

  1. !test.png
  2. 1101.png
  3. 12345.jpg
  4. image3.jpg
  5. image4.jpg
  6. image5.jpg
  7. image5.jpg image6.jpg
  8. image7.jpg
  9. test_test.png test.png
  10. test.jpg test.jpg
  11. test2.png

我需要带有下一个标题的图像:

  1. 101。 png
  2. 102.png
  3. 103.jpg
  4. 104.png
  5. 105.png
  6. 106.jpg
  7. 107.jpg
  8. 108.jpg
  9. 109.jpg 109.jpg
  10. 110.jpg
  11. 111.png

示例:101.png =“ strong> 01.png “或“ 01.jpg ”,其中:

  • 1 ”是可变的,当我输入Gulp命令时,我会在控制台中输入( Gulp 1
  • 01..11.png ”(或“ 01..11.jpg ”)只是图像的名称按顺序

我有此代码,gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
    return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
    .pipe(rename({
      // code
    }))
    .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

package.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "del": "^6.1.0"
  }
}

我的结构:

https://i.sstatic.net/ulck6.png“ alt =“我的结构”>

I have images with different titles:

  1. !test.png
  2. 1101.png
  3. 12345.jpg
  4. image3.jpg
  5. image4.jpg
  6. image5.jpg
  7. image6.jpg
  8. image7.jpg
  9. test_test.png
  10. test.jpg
  11. test2.png

I need images with next titles:

  1. 101.png
  2. 102.png
  3. 103.jpg
  4. 104.png
  5. 105.png
  6. 106.jpg
  7. 107.jpg
  8. 108.jpg
  9. 109.jpg
  10. 110.jpg
  11. 111.png

Example: 101.png = "1" + "01.png" or "01.jpg", where:

  • 1” is variable I enter in the console when I type the gulp command (gulp 1)
  • 01..11.png” (or “01..11.jpg”) are just the names of the images in ascending order

I have this code, gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
    return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
    .pipe(rename({
      // code
    }))
    .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

package.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "del": "^6.1.0"
  }
}

My structure:

My structure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎夕旧梦 2025-02-16 21:59:33

我得到了此代码,Gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers'

const argv = yargs(hideBin(process.argv)).argv

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
  var arg = argv.arg
  var index = 1
  return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
  .pipe(rename(function (path) {
    if (index <= 9) {
      path.basename = arg + '0' + (index++)
    } else {
      path.basename = arg + '' + (index++)
    }
    return path;
 }))
  .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

packgage.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "del": "^6.1.0",
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "yargs": "^17.5.1"
  }
}

在我编写的控制台中:

gulp --arg 1

有一个更漂亮的解决方案吗?

I got this code, gulpfile.js:

import gulp from 'gulp';
import rename from 'gulp-rename';
import del from 'del';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers'

const argv = yargs(hideBin(process.argv)).argv

const deleteImages = () => {
  return del(['build',], {force:true});
}

const renameImages = () => {
  var arg = argv.arg
  var index = 1
  return gulp.src('src/images/*.{jpg,jpeg,png,gif,wepb}')
  .pipe(rename(function (path) {
    if (index <= 9) {
      path.basename = arg + '0' + (index++)
    } else {
      path.basename = arg + '' + (index++)
    }
    return path;
 }))
  .pipe(gulp.dest('build'))
}

gulp.task('default', gulp.series(deleteImages, renameImages));

packgage.json:

{
  "name": "gulp-rename-images",
  "description": "Task for gulp",
  "version": "0.0.1",
  "license": "MIT",
  "main": "gulpfile.js",
  "type": "module",
  "devDependencies": {
    "del": "^6.1.0",
    "gulp": "^4.0.2",
    "gulp-rename": "^2.0.0",
    "yargs": "^17.5.1"
  }
}

In the console I write:

gulp --arg 1

Is there a prettier solution?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文