Copying images with Gulp are corrupted/damaged
Asked Answered
H

2

11

There is a simple task of copying an image from a folder to another folder.

gulp.task('default', function () {
    return gulp.src('./img/*.*')
        .pipe(gulp.dest('./image'));
});

Everything worked fine for me before, but recently my images are copied to the output folder corrupted. When you try to open them, an error appears stating that the file is damaged and cannot be read.

gulp versions:

CLI version: 3.0.0

Local version: 5.0.0

I don’t understand what the problem could be and how to fix it, and it was even difficult to explain the problem. Maybe this is a problem with my PC, system or version of Gulp, etc., not clear. If anyone has encountered this problem please share the solution.

Thank you in advance!

Tried using other versions of Gulp, using different images, and tried to run the task on a different drive. All this was unsuccessful

Hum answered 26/4, 2024 at 14:47 Comment(0)
P
28

From Gulp 5 - Copied images using .src and .dest are corrupt:

Try to set { encoding: false } option to src. See "Stream encoding" at https://medium.com/gulpjs/announcing-gulp-v5-c67d077dbdb7

So this should work:

gulp.task('default', function () {
    return gulp.src('./img/*.*', {encoding: false})
        .pipe(gulp.dest('./image'));
});
Premeditation answered 8/5, 2024 at 16:56 Comment(2)
This fixed my issue. Thanks.Readership
This literally save my Sunday! OMG 3 hours trying IA BS.Geyserite
A
0

First of all, welcome to Javascript and Node. Sometimes it just breaks, which is exactly how it should be (sarcasm).

I encountered the same problem just a day ago. Somehow gulp corrupts images and as it seems there is no proper solution to this issue yet (at least i could not fine one).

So i came up with a temporary solution, until its fixed.

const fs = require("fs");


gulp.task('default', function (callback) {
    try {
        fs.copyFile("./img/background.jpg", "./image/background.jpg", callback);
    } catch (e) {
        // nth
    }
});

For your specific issue, you probably would walk over all files in the given directory and do the thing. But I figured i keep the "solution" small and simple.

Arrogance answered 8/5, 2024 at 15:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.