By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Lambda Javascript canvas module decode as Hexa insteas of UTF8

0

Hi,

I have a development which is working fine on my local laptop. I'm trying to generate image from text (initials of somebody) as default profile image. To do that, I'm using canvas module, provide the right context. When I call the method canvas.fillText('IM',...), the string is Hexa decoded by the module. You can see the wrong image created by the lambda execution : Right image And the right one, created in local execution : Wrong image

I have tried to ask chatGPT which doesn't know the origin of the issue. I have tried to force the encoding which adding environment variable LANG = 'en_US.UTF-8' That doesn't work. I have verified all previous steps and next steps of the image creation, the only issue should provide from the decoding of fillText of the canvas module which is version "canvas": "^2.11.2"

So there is an deeper issue on the js execution of this module. Please, do you any idea why this is happening ? Thanks Pierre

asked 16 days ago25 views
2 Answers
0

The issue you're experiencing with the Lambda function generating incorrect images compared to your local environment is likely related to character encoding differences between the two environments.

In Lambda, it appears that the canvas module is interpreting the text input as hexadecimal instead of UTF-8, which is causing the unexpected output. This could be due to differences in how the Lambda environment handles character encoding compared to your local machine.

While setting the LANG environment variable to 'en_US.UTF-8' was a good attempt, it may not be sufficient to resolve the issue in this case. The problem seems to be more specific to how the canvas module is interpreting the input within the Lambda environment.

To address this, you could try the following approaches:

  1. Explicitly encode the input text as UTF-8 before passing it to the fillText method. You can use the TextEncoder API to ensure the text is properly encoded:

    const encoder = new TextEncoder();
    const encodedText = encoder.encode('IM');
    canvas.fillText(new TextDecoder().decode(encodedText), ...);
  2. If possible, update the canvas module to the latest version, as there might be bug fixes or improvements related to character encoding in newer versions.

  3. Consider using a different library or approach for image generation that might be more consistent across different environments. For example, you could explore using Sharp or Jimp, which are popular image processing libraries that work well in Lambda environments.

  4. If the issue persists, you might need to implement a workaround by manually converting the input text to the correct representation before passing it to fillText. This could involve creating a mapping of characters to their correct UTF-8 representations.

Remember to thoroughly test any changes in both your local and Lambda environments to ensure consistency across platforms.

Sources
SerdeContext | CodeGuruProfiler Client - AWS SDK for JavaScript v3
LazyJsonString | savingsplans Client - AWS SDK for JavaScript v3
LazyJsonString | CodeGuruProfiler Client - AWS SDK for JavaScript v3

profile picture
answered 16 days ago
0

Check lambda nodejs version and compare with local one

profile picture
answered 14 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions