Saving Resized Animated GIF Matching Canvas Output

I am working on an exercise that allows users to upload an image, resize it, and download the resized image. The functionality works well for static images (JPEG, PNG), but I am encountering issues with saving resized animated GIFs.

When a user uploads an animated GIF, the image is composited and displayed correctly, but attempting to save the canvas output just results in saving the original uploaded gif.

Code below:
https://jsfiddle.net/Aloe10211/uxzbwcaj/1/

let originalImage = null;
let originalImageType = '';

document.getElementById('upload').addEventListener('change', function(event) {
    const file = event.target.files[0];
    if (!file) return;

    originalImageType = file.type;

    if (file.type === 'image/gif') {
        const reader = new FileReader();
        reader.onload = function(e) {
            originalImage = e.target.result;
            document.getElementById('buttons').style.display = 'none';
            processGIF(originalImage);
        };
        reader.readAsDataURL(file);
    } else {
        const reader = new FileReader();
        reader.onload = function(e) {
            originalImage = e.target.result;
            document.getElementById('buttons').style.display = 'block';
        };
        reader.readAsDataURL(file);
    }
});

document.getElementById('smol50').addEventListener('click', function() {
    resizeImage(0.5);
});

document.getElementById('smol75').addEventListener('click', function() {
    resizeImage(0.25);
});

document.getElementById('smol90').addEventListener('click', function() {
    resizeImage(0.1);
});

function resizeImage(scale) {
    processImage(originalImage, scale);
}

function processImage(dataUrl, scale) {
    const img = new Image();
    img.onload = function() {
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        const width = img.width * scale;
        const height = img.height * scale;

        canvas.width = width;
        canvas.height = height;

        ctx.drawImage(img, 0, 0, width, height);

        const resizedImage = canvas.toDataURL('image/png');

        document.getElementById('output').src = resizedImage;
        document.getElementById('output-container').style.display = 'block';

        const downloadLink = document.getElementById('download');
        downloadLink.href = resizedImage;
        downloadLink.download = 'resized_image.png';
        downloadLink.style.display = 'block';
        downloadLink.innerText = 'Download Resized Image';
    }
    img.src = dataUrl;
}

function processGIF(dataUrl) {
    document.getElementById('output').src = dataUrl;
    document.getElementById('output').style.width = '100px';
    document.getElementById('output').style.height = 'auto';
    document.getElementById('output-container').style.display = 'block';

    const downloadLink = document.getElementById('download');
    downloadLink.href = dataUrl;
    downloadLink.download = 'resized_image.gif';
    downloadLink.style.display = 'block';
    downloadLink.innerText = 'Download Resized GIF';
}

  • The composited animated GIF uploads and displays correctly.
  • When resized, the GIF saves as the original gif file with no modification.

Expected:

  • The resized and composited GIF should be downloadable as an animated GIF matching the canvas output.

New contributor

Moe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật