I have a question.
In the main code block, I am calling createRoundedCorners(fileName1, fileName2)
function that takes an existing fileName1 image and from it creates fileName2 image with rounded corners. Then, I am calling imageToBase64(filePath)
to read this image file and create base64 string from it. But, for some reason, on the first run, imageToBase64(filePath)
function throw an exception
ENOENT: no such file or directory, open 'D:\Projects\Bookmarklets\images\easyweb-32.png
I know the file is created before imageToBase64(filePath)
function is called.
So, on the second run, it doesn’t throw an error anymore.
Why is this happening and how to solve the issue? Thank you.
Main code block:
createRoundedCorners(fileName1, fileName2);
const filePath = path.join(__dirname, 'images', fileName2);
const base64string = imageToBase64(filePath);
imageToBase64(filePath) function:
// Function to convert an image file to a Base64 string
function imageToBase64(filePath) {
try {
// Read the image file synchronously
const imageBuffer = fs.readFileSync(filePath);
// Convert the file buffer to a Base64 string
const base64String = imageBuffer.toString('base64');
return base64String;
} catch (error) {
console.error('Error reading file:', error.message); // Throws an exception on the first run here
}
}