Simple Plugin - Node Help - Get Image Size

Can you actually use async at the beginning of the code block? I didn’t know that.

try the examples without axios first

yes, even though that line is there just as a marker for the start of the code

1 Like

Does not works :slight_smile:

async function(properties, context) {
  const sizeOf = require("image-size");

  try {
    const { fileURL } = properties;

    // Fetch image data using fetch
    const response = await fetch(fileURL);
    if (!response.ok) {
      throw new Error(`Failed to fetch image: ${response.statusText}`);

    // Get image dimensions using fast-image-size
    const arrayBuffer = await response.arrayBuffer();
    const dimensions = sizeOf(arrayBuffer);

    return {
      width: dimensions.width,
      height: dimensions.height,
      error: "",
  } catch (error) {
    return {
      width: 0,
      height: 0,
      error: `Error while obtaining image dimensions: ${error.message}`,

I tried the sample in the docs and it works, exactly that sample (not with axios, not with fetch) adapted only to return a promise and instead of logging the result you resolve the promise. Give it a go.
I’m sure that you can make it work with axios or fetch, but you need to debug the problem by reading the error. Not sure how familiar are you with nodejs, but it’s easier to debug the code by running it locally first, so you get the error directly in the console. And since you are debugging, use an hard-coded url for a known image.

for reference, this works:

const http = require("http");

const sizeOf = require("image-size");

const imgUrl = "";
const options = new URL(imgUrl);

return new Promise((res) => {
	http.get(options, function (response) {
		const chunks = [];
			.on("data", function (chunk) {
			.on("end", function () {
				const buffer = Buffer.concat(chunks);
				res({ data: JSON.stringify(sizeOf(buffer) || {}, null, 2) });

you need to add error handling to it

I’ve got limited knowledge in Node…
But we’ve gotta face it !!!
Tried this based on your code… nothing seems to work…
I’m almost asking you to fix this… You seems to know exactly what works :slight_smile:

Finally make it happen!
Wanna Thank you @dorilama , your teached a way to deal with Node…
Thank you for your insights… !

Way to go :clap: :grinning:

One last question…
I’ve returned a Json object as Text by adding
dimensions: JSON.stringify(dimensions)
How to deal and return as Json itself? Is it as a “text” return?

It is returning this
dimensions = {“height”:1080,“width”:1080,“type”:“jpg”}
How to use in a simple manner? split by comma? There must be an easier way…

the best way is to set different return values in the plugin because then all the values are easily available in the frontend without workarounds.
In my example I returned a string just because it was a minimal example about the code, not the plugin setup.

1 Like

Tks @dorilama

This topic was automatically closed after 70 days. New replies are no longer allowed.