Skip to main content
May 28, 2024 3:19 PM UTC
Question

Unable to load some images

Hi Figma community,

I am building a plugin which takes url of the images and display them in a frame. I am able to make it work but for some images I am getting the below error.
Error loading image: Image is too large

I cross checked the image size, the resolution is same as others which are working.

Can someone help me out with this?

Warm regards

This topic has been closed for replies.

8 replies

Gayani_S
Figmate
Community Support
May 30, 2024 7:59 AM UTC

Hey @Kaverappa_K_U, thank you for reaching out! I am unsure here, but I’ve reached out internally to the teams for help. I’ll come back to you once I receive more information on this.

Appreciate your patience!

jefflee
Figmate
Figmate
May 30, 2024 4:25 PM UTC

Thanks for the question. A couple things that will help us investigate:

  • Can you share the code that is producing the error?
  • Is this consistently happening for the same image file? If so, are you wiling to share the image and/or URL it is hosted from?
May 30, 2024 6:12 PM UTC

Hi @jefflee,

Thanks for the response.
I have attached the function I am using to display the picture and also the image urls which are throwing errors.

async function loadImage(frame: FrameNode, imageUrl: string) {
  try {
    // Create the image from the URL
    console.log(imageUrl)
    const image = await figma.createImageAsync(imageUrl);
    console.log(imageUrl)
    // Get the dimensions of the image
    const { width: imageWidth, height: imageHeight } = await image.getSizeAsync();
    // Check if the image exceeds Figma's maximum allowable dimensions
    const MAX_DIMENSION = 4096;
    console.log(imageWidth, imageHeight)
    if (imageWidth > MAX_DIMENSION || imageHeight > MAX_DIMENSION) {
      throw new Error('Image is too large to be processed by Figma.');
    }

    // Get the dimensions of the frame
    const frameWidth = frame.width;
    const frameHeight = frame.height;

    // Calculate the scaling factors to fit the image within the frame
    const widthScale = frameWidth / imageWidth;
    const heightScale = frameHeight / imageHeight;
    const scale = Math.min(widthScale, heightScale);

    // Calculate the scaled dimensions of the image
    const scaledWidth = imageWidth * scale;
    const scaledHeight = imageHeight * scale;
    console.log(scaledWidth, scaledHeight)
    // Create a rectangle node and resize it to match the scaled image dimensions
    const imageNode = figma.createRectangle();
    imageNode.resize(scaledWidth, scaledHeight);

    // Set the image as the fill of the rectangle node
    imageNode.fills = [{ type: 'IMAGE', imageHash: image.hash, scaleMode: 'FILL' }];

    // Position the image node in the center of the frame
    imageNode.x = (frameWidth - scaledWidth) / 2;
    imageNode.y = (frameHeight - scaledHeight) / 2;

    // Remove existing rectangle nodes from the frame
    frame.children.forEach(child => {
      if (child.type === 'RECTANGLE') {
        child.remove();
      }
    });

    // Append the rectangle node to the frame
    frame.appendChild(imageNode);
  } catch (error) {
    console.error('Error loading image:', error);
  }
}

Image urls:

Please let me know what’s the issue?

Thanks and Regards

May 30, 2024 7:55 PM UTC

Hey @jefflee

Thanks for the response.
I already sent you the code snippet and the urls.
But somehow it is hidden by the spam filter.
😦
BR

jefflee
Figmate
Figmate
May 30, 2024 8:28 PM UTC

Got it, sorry about the content filters. What about posting as a Github gist?

May 30, 2024 8:42 PM UTC

I got what is the error though.
I am getting error in the Figma inbuilt function createImageAsync where if the image height or width is higher than 4096 pixels it throws this error.
How can i handle this as Figma’s createImageAsync only supports 4096

jefflee
Figmate
Figmate
May 30, 2024 10:37 PM UTC

createImageAsync can only process images up to the documented limit. I’m not sure if there’s a solid workaround for that limit other than trying to preprocess the image outside of Figma.

If you’re just trying to detect when an image exceeds this limit, you can try the following:

let image
try {
  image = await.createImageAsync(url)
} catch (err) {
  // handle the error cases listed here:
  // https://www.figma.com/plugin-docs/api/properties/figma-createimageasync/#possible-error-cases
}
May 30, 2024 10:40 PM UTC

Yeah thank you for the reply.
🙂