lokesh / color-thief

Grab the color palette from an image using just Javascript. Works in the browser and in Node.
https://lokeshdhakar.com/projects/color-thief/
MIT License
12.67k stars 1.31k forks source link

Pass mimetype when input is Buffer #174

Open elrumordelaluz opened 5 years ago

elrumordelaluz commented 5 years ago

First, thanks for this great Library!

Working with this cool Library (congrats and thanks!) in node.js, in particular making responsimage I found that the image (input) value could be not only a path string to the image but also a Buffer, since the url argument:

It can be a relative path, an http url, a data url, or an in-memory Buffer.

And this is cool, and sound coherent when working in node. But in this scenario I found that the second argument type is needed:

type is an optional mime type for the image (required when using a Buffer)

So made this PR to fix that part, adding the relative test. ~Keep in mind that I didn't include the /dist with bundled files.~ I added this folder to let use my repo as the packages source in the meanwhile (otherwise, some projects are broken)

Let me know in any case and thanks again for this great Library!

AdamSantanielloVP commented 5 years ago

Any update on when this pull request can be merged and released? This update would be immensely helpful.

ghost commented 4 years ago

same issue (I think), I get Invalid file type (https://github.com/scijs/get-pixels/blob/b389ffc3408b1be50f63fe8bd0837f17a48f1f27/node-pixels.js#L137)

I made it working for me with the follow patch (dist only) and pass it as [buffer, type] to getColor

diff --git a/node_modules/colorthief/dist/color-thief.js b/node_modules/colorthief/dist/color-thief.js
index 2833cec..1fe5714 100644
--- a/node_modules/colorthief/dist/color-thief.js
+++ b/node_modules/colorthief/dist/color-thief.js
@@ -48,13 +48,20 @@ function validateOptions(options) {

 function loadImg(img) {
     return new Promise((resolve, reject) => {
-        getPixels(img, function(err, data) {
-            if(err) {
+        let params;
+        if (Array.isArray(img)) {
+            params = img;
+        } else {
+            params = [img];
+        }
+        params.push(function (err, data) {
+            if (err) {
                 reject(err)
             } else {
                 resolve(data);
             }
-        })
+        });
+        getPixels(...img)
     });
 }
bjarkih commented 4 years ago

A vote from me. I'm having to fork the library because this is missing from the current code base.

ghost commented 4 years ago

You could use https://www.npmjs.com/package/patch-package if you don't want to fork, like I did for my patch above

WandersonAlves commented 4 years ago

Bump

m-soli commented 3 years ago

any news when this is going to get merged ?

ronanyeah commented 3 years ago

Bump

Enchiridion commented 2 years ago

Bump. I already have the image in a Buffer after doing processing through sharp. I'd rather not write it to a temp file just so it can be read back.

arbazsiddiqui commented 1 year ago

Bump

Dituon commented 1 year ago

Bump

hroland commented 1 year ago

Bump

ppjjzz commented 1 year ago

any update?

EvanZhouDev commented 1 year ago

Please merge, we need this!

YahiaBakour commented 6 months ago

Can someone merge this damn PR