Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The ImageMagick example doesn't work #2034

Closed
Flucadetena opened this issue Oct 29, 2020 · 0 comments
Closed

The ImageMagick example doesn't work #2034

Flucadetena opened this issue Oct 29, 2020 · 0 comments
Labels
samples Issues that are directly related to samples.

Comments

@Flucadetena
Copy link

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!

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Oct 29, 2020
This was referenced Jan 11, 2022
This was referenced Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

No branches or pull requests

1 participant