2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
2020-07-20 22:10:04 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2020-08-28 02:34:12 +00:00
|
|
|
#include <list>
|
2020-07-20 22:10:04 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Magick;
|
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Jpeg(const Napi::CallbackInfo &info) {
|
2020-07-20 22:10:04 +00:00
|
|
|
Napi::Env env = info.Env();
|
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].As<Napi::Object>();
|
2021-05-11 19:25:02 +00:00
|
|
|
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
2020-07-20 22:10:04 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
Blob blob;
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
Image image;
|
2021-05-11 19:25:02 +00:00
|
|
|
image.read(Blob(data.Data(), data.Length()));
|
2021-04-26 14:47:03 +00:00
|
|
|
image.quality(1);
|
|
|
|
image.magick("JPEG");
|
|
|
|
image.write(&blob);
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
|
|
|
result.Set("type", "jpg");
|
|
|
|
return result;
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (Napi::Error const &err) {
|
|
|
|
throw err;
|
2021-04-26 14:47:03 +00:00
|
|
|
} catch (std::exception const &err) {
|
|
|
|
throw Napi::Error::New(env, err.what());
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
throw Napi::Error::New(env, "Unknown error");
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2020-07-20 22:10:04 +00:00
|
|
|
}
|