ericnograles / browser-image-resizer

A tiny browser-based library to downscale and/or resize images using canvas
MIT License
99 stars 43 forks source link

Nextjs ReferenceError: self is not defined #87

Closed opeodedeyi closed 6 months ago

opeodedeyi commented 11 months ago

Everything works well till i deploy the app and the code is building on Vercel or Netlify and then i get the ReferenceError: self is not defined

below is my import code and what is compressed

'use client';

import "./forminput.css";
import { useState, useRef } from 'react';
import { readAndCompressImage } from 'browser-image-resizer';

const SingleImageUploadInput = ({ children }) => {
    const readImage = async (file) => {
        const config = {
            quality: 0.7,
            maxWidth: 1920,
            maxHeight: 1080,
            mimeType: 'image/webp',
            debug: true,
        };

        try {
            const resizedImageFile = await readAndCompressImage(file, config);
            const reader = new FileReader();

            return new Promise((resolve, reject) => {
                reader.onloadend = () => {
                    resolve(reader.result);
                };
                reader.onerror = reject;
                reader.readAsDataURL(resizedImageFile);
                let resizedImgSize = resizedImageFile.size;
                setImageSize(convertFileSize(resizedImgSize));
            });
        } catch (error) {
            console.log('Failed to resize the image:', error);
        }
    };
TomerH0525 commented 9 months ago

const { readAndCompressImage } = (await import('browser-image-resizer')); try implementing this fix should help,

ericnograles commented 6 months ago

Thanks for the reply @TomerH0525 -- this is related to #77. I will add some guidance on the docs for static site usage per that issue.