Can i convert an image from a url to base64 using pipedream?

This topic was automatically generated from Slack. You can find the original thread here.

Mauro Mequelussi : can i convert an image from a url to base64 using pipedream?

For example if I want to encode base64 this image below. How should I do?

Dylan Sather (Pipedream) : I would try something like:

const axios = require("axios")
const resp = await axios({
  url: <your URL>,
  method: "GET",
  responseType: "arraybuffer",
})
return Buffer.from(resp.data, 'binary').toString("base64")

Mauro Mequelussi : Is there any integration in pipedream so I can get metatags from a URL (page) and generate the preview link?

  • Title
  • Description
  • Image

I need to generate the preview link first and after the image url generate the base64, as shown above.

Dylan Sather (Pipedream) : I’d recommend using a library like Cheerio to download the page and parse the head element for the metadata you’re looking for

Any help please?

Wanted to post the image from url to base64

Hi @athul.sreenivasan,

First off, welcome to Pipedream! Happy to have you.

To download an image and convert it to base64 using Pipedream, you can use the following code snippet in a Pipedream workflow:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    imageUrl: {
      type: "string",
      label: "Image URL",
    },
  },
  async run({ $ }) {
    const response = await axios($, {
      method: "GET",
      url: this.imageUrl,
      responseType: "arraybuffer",
    });

    const base64Image = Buffer.from(response, "binary").toString("base64");
    return base64Image;
  },
});

This code snippet defines a Pipedream component with a single prop imageUrl for the image URL. It uses the axios function from the @pipedream/platform package to download the image as an ArrayBuffer. Then, it converts the ArrayBuffer to a base64 string using the Buffer class and returns the base64-encoded image.

Thanks so much! It works amazing!! :heart_eyes:

I would like to use an npm package along with this:

How can i use this?