Open lu911 opened 4 years ago
Hi @YukSeungChan Thanks for submitting this issue! I've had a look at your code, and it appears that the issue is related to the manner in which you're fetching your image, since the result is encoded as a string rather than as a buffer.
I've coded a small example to help out. This issue has also cropped up for someone else, so I'll update the docs to include an example also.
await fetch(image_url)
.then(data => data.buffer())
.then(buffer => {
let b64 = buffer.toString('base64');
let photon_image = photon.PhotonImage.new_from_base64(b64);
// photon.grayscale(photon_image);
});
Hope this helps 😄 If you have any other questions, let me know!
Thanks for the answer.
I can't use node-fetch. How can I encode the ArrayBuffer of Web API to base64 in the browser?
The code I wrote above doesn't work.
I am not able to use this library in web workers 😥
Can you please show us a full example on how to achieve this. ( I am fairly new to wasm )
So far my index.html contains :
<html>
<body>
<h1>Demonstrates loading a local file using Photon on a WebWorker thread 2</h1>
<p><input type="file" onchange="newFiles(this);" /></p>
<script>
async function newFiles(element)
{
for (var i=0; i<element.files.length; i++)
{
await readFileAndProcess(element.files[i]);
}
}
async function readFileAndProcess(file)
{
let arrayBuffer = await file.arrayBuffer();
const worker = new Worker('photon-worker.js', {
type: 'module'
});
worker.onmessage = function(e)
{
var img = document.createElement("img");
img.setAttribute("src", e.data);
document.body.appendChild(img);
};
worker.postMessage(arrayBuffer);
}
</script>
</body>
</html>
My photon-worker.js file contains :
// TODO: what to import here ?
self.addEventListener('message', e =>
{
console.debug('event passed to web worker : ', e);
console.debug('data passed to web worker : ', e.data);
let arrayBuffer = e.data;
// TODO: how to get photon object here , so that i can call the function photon.from_base64() here
});
Thanks 👍
For converting buffer to dataUrl (b64), you can check: https://github.com/cloudflare/workerd/issues/54#issuecomment-1262659997
I tried creating a minimal project expressing the problem:
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers/tree/main/photon-library
Great Works! Can you publish as an npm package?
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers/tree/main/photon-library
Thanks, I deploy success
Support png: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=resize!820,400,1&format=png
Support jpg: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=resize!820,400,1&format=jpg
Support webp: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=resize!820,400,1&format=webp
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers/tree/main/photon-library
Thanks, I deploy success
Glad to hear 😃
Support Watermark: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=watermark!https%3A%2F%2Fmt.ci%2Flogo.png,10,10 Support Pipe: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=resize!800,400,1|watermark!https%3A%2F%2Fmt.ci%2Flogo.png,10,10
I
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers/tree/main/photon-library
Great Works! Can you publish as an npm package?
I published a npm package for cloudflare workers: https://www.npmjs.com/package/@cf-wasm/photon
Support Watermark: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=watermark&options=https%3A%2F%2Fmt.ci%2Flogo.png,10,10 Support Pipe: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&pipe=resize!800,400,1|watermark!https%3A%2F%2Fmt.ci%2Flogo.png,10,10
Can you share the source code if you don't mind?
Support Watermark: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=watermark&options=https%3A%2F%2Fmt.ci%2Flogo.png,10,10 Support Pipe: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&pipe=resize!800,400,1|watermark!https%3A%2F%2Fmt.ci%2Flogo.png,10,10
Can you share the source code if you don't mind?
Let me sort it out this weekend
Support Watermark: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&action=watermark&options=https%3A%2F%2Fmt.ci%2Flogo.png,10,10 Support Pipe: https://image.miantiao.me/?url=https%3A%2F%2Fstatic.miantiao.me%2Fbanner-big.jpeg&pipe=resize!800,400,1|watermark!https%3A%2F%2Fmt.ci%2Flogo.png,10,10
Can you share the source code if you don't mind?
@suragch @techwithdeo Look Here https://github.com/ccbikai/cloudflare-worker-image
Thank you :)
I tried creating a minimal project expressing the problem:
Here is an example of using Photon Image Library on Cloudflare Workers: https://github.com/techwithdeo/cloudflare-workers/tree/main/photon-library
Not working here :( Failed to compile. ▲ ./app/img/[collection]/[id]/photon/photon_rs_bg.wasm ▲ Module not found: Can't resolve 'wbg' ▲ ▲ https://nextjs.org/docs/messages/module-not-found ▲ ▲ Import trace for requested module: ▲ ./app/img/[collection]/[id]/photon/index.js ▲ ./app/img/[collection]/[id]/route.js ▲ ./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=private-next-app-dir%2Fimg%2F%5Bcollection%5D%2F%5Bid%5D%2Froute.js&page=%2Fimg%2F%5Bcollection%5D%2F%5Bid%5D%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGaW1nJTJGJTVCY29sbGVjdGlvbiU1RCUyRiU1QmlkJTVEJTJGcm91dGUmcGFnZT0lMkZpbWclMkYlNUJjb2xsZWN0aW9uJTVEJTJGJTVCaWQlNUQlMkZyb3V0ZSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmltZyUyRiU1QmNvbGxlY3Rpb24lNUQlMkYlNUJpZCU1RCUyRnJvdXRlLmpzJmFwcERpcj0lMkZob21lJTJGY29vcGVyJTJGZGV2JTJGbWV0YS11Y2xvY2slMkZhcHAmYXBwUGF0aHM9JTJGaW1nJTJGJTVCY29sbGVjdGlvbiU1RCUyRiU1QmlkJTVEJTJGcm91dGUmcGFnZUV4dGVuc2lvbnM9dHN4JnBhZ2VFeHRlbnNpb25zPXRzJnBhZ2VFeHRlbnNpb25zPWpzeCZwYWdlRXh0ZW5zaW9ucz1qcyZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9JnByZWZlcnJlZFJlZ2lvbj0mbWlkZGxld2FyZUNvbmZpZz1lMzAlM0Qh&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! ▲ ▲ ./app/img/[collection]/[id]/pngs/pngs_bg.wasm ▲ Module not found: Can't resolve 'wbg' ▲ ▲ https://nextjs.org/docs/messages/module-not-found ▲ ▲ Import trace for requested module: ▲ ./app/img/[collection]/[id]/pngs/index.js ▲ ./app/img/[collection]/[id]/route.js ▲ ./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=private-next-app-dir%2Fimg%2F%5Bcollection%5D%2F%5Bid%5D%2Froute.js&page=%2Fimg%2F%5Bcollection%5D%2F%5Bid%5D%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGaW1nJTJGJTVCY29sbGVjdGlvbiU1RCUyRiU1QmlkJTVEJTJGcm91dGUmcGFnZT0lMkZpbWclMkYlNUJjb2xsZWN0aW9uJTVEJTJGJTVCaWQlNUQlMkZyb3V0ZSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmltZyUyRiU1QmNvbGxlY3Rpb24lNUQlMkYlNUJpZCU1RCUyRnJvdXRlLmpzJmFwcERpcj0lMkZob21lJTJGY29vcGVyJTJGZGV2JTJGbWV0YS11Y2xvY2slMkZhcHAmYXBwUGF0aHM9JTJGaW1nJTJGJTVCY29sbGVjdGlvbiU1RCUyRiU1QmlkJTVEJTJGcm91dGUmcGFnZUV4dGVuc2lvbnM9dHN4JnBhZ2VFeHRlbnNpb25zPXRzJnBhZ2VFeHRlbnNpb25zPWpzeCZwYWdlRXh0ZW5zaW9ucz1qcyZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9JnByZWZlcnJlZFJlZ2lvbj0mbWlkZGxld2FyZUNvbmZpZz1lMzAlM0Qh&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! ▲ ▲ > Build failed because of webpack errors ▲ error Command failed with exit code 1.
I'm using Cloudflare Pages.
@matbrgz
I recently updated @cf-wasm/photon, and now it supports Cloudflare Workers, Cloudflare Pages, Next.js and node environment.
Use @cf-wasm/photon for Next.js (Webpack) as shown:
import * as photon from "@cf-wasm/photon/next";
Oh you should definetly add this as a feature to the README, do open a PR!
hi @techwithdeo @ccbikai , Thanks very much for posting your solutions. both work great!!! However, webp images become grayscale, any idea how to preserve the RGB colors for webp images? That would be very much appreciated. Thanks in advance.
Web Workers (using Webpack bundler): https://github.com/fineshopdesign/cf-wasm/tree/main/packages/photon#web-worker or you can use esm.sh
Cloudflare Workers: https://github.com/fineshopdesign/cf-wasm/tree/main/packages/photon#cloudflare-workers
Next.js (App router + Edge Runtime): https://github.com/fineshopdesign/cf-wasm/tree/main/packages/photon#nextjs-app-router
Next.js (Pages router + Edge Runtime): https://github.com/fineshopdesign/cf-wasm/tree/main/packages/photon#nextjs-pages-router
Hello, I want to use this library on Cloudflare workers. Cloudflare workers run on WebWorker environment.
I imported photon-rs on rust project and build with wasm-pack.
I get "RuntimeError: unreachable" error on some images converted into base64.
In docs, I saw an example utilizing canvas but are there other ways to store it on memory?
[Code]
[Error]