photon
photon copied to clipboard
photon-node usage on backend
Hi, congratulations about the huge work, I can't wait to try it with Rust, but currently I'd need to use the lib in node. I don't exactly get how it is intended to be used on backend, it seems it requires anyway to work with a canvas, while with node the most common usage is probably reading directly from fs or to use a readStream from fs. I think an example would be really convenient.
Hi Patrick, Thanks for opening this issue. I've created a small example here which would read an image directly from fs, apply a grayscale filter, and then save the image as base64. I'm going to be uploading this example to the guide also, and will also be adding more Node examples, such as resizing images in Node, plus some benchmarks in the coming days.
Also, the image name in this script is input.png
, here's the code:
var fs = require('fs');
var photon = require("@silvia-odwyer/photon-node");
const fetch = require('node-fetch');
global.fetch = fetch;
function grayscaleImage() {
// read file, then convert to base64
var base64 = fs.readFileSync(`input.png`, { encoding: 'base64' });
let data = base64.replace(/^data:image\/(png|jpg);base64,/, "");
// convert base64 to PhotonImage
var phtn_img = photon.PhotonImage.new_from_base64(data);
photon.grayscale(phtn_img);
// get base64 from filtered image, and write
let output_base64 = phtn_img.get_base64();
let output_image_name = "output.png";
var output_data = output_base64.replace(/^data:image\/\w+;base64,/, '');
fs.writeFile(output_image_name, output_data, {encoding: 'base64'}, function(err) {
});
console.log(`Saved ${output_image_name}`);
}
grayscaleImage();
Hopefully that helps somewhat, and also don't hesitate to ask if you have any further questions. Thanks again for mentioning this, and stay tuned for further examples in the guide 😄
Great thank you!
@silvia-odwyer, is the code for @silvia-odwyer/photon-node
available somewhere, maybe here on github?
I'm interested in using photon with node.js, and I want to use your resize
function but it doesn't look like it is exported for webassembly? https://github.com/silvia-odwyer/photon/blob/6ece12b205d597bcd4454384e27ece3b7e88f369/crate/src/transform.rs#L240-L241 Is there any reason for not exporting it? I see that resizeimg_browser
is exported for webassembly but that one is using canvas and I guess it will not work for node (I saw the todo comment).
I gladly want to help if you point me in the right direction and thanks for a fantastic library!
@tjoskar Thanks for mentioning this; I'd like to export several node-only functions, since the browser functions make use of the canvas (and that doesn't work for node, as stated).
Currently no wasm-bindgen annotations exist for a "node-only" target, so I'd like to open an issue about this on wasm-bindgen to see if there are any possible solutions. If there are any other ways to work around this or you have any ideas, feel free to let me know here, thanks 😄
@silvia-odwyer I agree, it should be nice to create two targets, one for the web and one for non-web (node/phyton/go/etc.).
I might be missing something, but why can't we use resize
on the web (and non-web) with webassembly?
Should it not be possible to do something like in node/web (or any other webassembly runtime):
const myBase64Image = ...;
const photonImage = photon.PhotonImage.new_from_base64(myBase64Image);
photon.resize(photonImage, 50, 50, photon.SamplingFilter.Gaussian);
const myNewBase64Image = photonImage.get_base64();
resizeimg_browser
might be more efficient on the web, but the code above should work, right?
@tjoskar That's correct, the code sample would work, however in a web browser it would be less performant than the manner it's done currently.
What I can do for the moment is export the resize
method to WebAssembly so that image resizing is possible in Node also. But in the future, I'd definitely want to add a Node-only target for certain methods.
@silvia-odwyer
What I can do for the moment is export the resize method to WebAssembly
Sounds great!
I'd definitely want to add a Node-only target for certain methods
Sounds reasonable, but maybe turn it around to "browser-only" instead, so it still would be possible to use the functions with any other language that support WebAssembly, not just nodejs.
In the meantime I think it is fine to append _browser
the function name that only works in the browser and let it crash if one is using it in some other runtime (as it is today).