Skip to content

The ImageMagick example doesn't work #2034

@Flucadetena

Description

@Flucadetena

Version info

I'm using node 12, And have tried to downgrade to 10 and the same error occurred.

node: v12.16.3
"firebase-admin": "^9.3.0",
"firebase-functions": "^3.11.0",
"gm": "^1.23.1"
Firebase-tools: 8.14.1

Test case

import * as functions from "firebase-functions";
import { createWriteStream } from "fs";
import axios from "axios";
import gm from "gm";
gm.subClass({ imageMagick: true });

export const testTreatImage = functions.https.onRequest(async (req, res) => {
	try {
		const url = "https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/best-skincare-products-glowing-skin-1561735403.png";
		const originalPath = "/tmp/original.jpg";
		await downloadImage(url, originalPath);
		await treatImg(originalPath);

		res.status(200).send({ message: "OK" });
	} catch (err) {
		console.error(`Error saving the image: `, err.toString());
		res.status(400).send({ message: "ERROR" });
	}
});

async function downloadImage(url: string, originalPath: string) {
	try {
		const decodeUrl = decodeURIComponent(url);
		const res = await axios.get(decodeUrl, { responseType: "stream" });

		res.data.pipe(createWriteStream(originalPath));

		return new Promise<string>((resolve, reject) => {
			res.data.on("end", () => {
				resolve(originalPath);
			});

			res.data.on("error", () => {
				console.log("Err");
				reject();
			});
		});
	} catch (err) {
		console.error("Error downloading the img: ", err.toString());
		throw new Error();
	}
}

async function treatImg(originalPath: string) {
	try {
		const saveImg = (path: string, event: gm.State) => new Promise((resolve) => event.write(path, resolve));

		const oneMWebp = "/tmp/1000.webp";
		const oneWebpError: any = await saveImg(
			oneMWebp,
			gm(originalPath).fill("rgb(247,247,247)").fuzz(5, true).opaque("rgb(255,255,255)").quality(50).resize(1000, undefined, "^").strip(),
		);
		if (oneWebpError !== undefined) throw new Error(oneWebpError.toString());

		return null;
	} catch (err) {
		console.error("Error treating the img and resizing: ", err.toString());
		throw new Error();
	}
}

Steps to reproduce

Call the http function.

Expected behavior

Work and treat the image, as it does in local dev.

With spawn and convert works, but using gm doesn't.

Actual behavior

Error treating the img and resizing: Error: Error: Could not execute GraphicsMagick/ImageMagick: gm "convert" "-quality" "50" "/tmp/original.jpg" "-fill" "rgb(247,247,247)" "-fuzz" "5%" "-opaque" "rgb(255,255,255)" "-resize" "1000x^" "+profile" ""*"" "+comment" "/tmp/1000.webp" this most likely means the gm/convert binaries can't be found

Were you able to successfully deploy your functions?

Yes no problem at all.

Thanks a lot!

Metadata

Metadata

Assignees

No one assigned

    Labels

    samplesIssues that are directly related to samples.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions