2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
2020-07-22 18:12:38 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2020-07-22 18:12:38 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Magick;
|
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Sonic(const Napi::CallbackInfo &info) {
|
2020-07-22 18:12:38 +00:00
|
|
|
Napi::Env env = info.Env();
|
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].As<Napi::Object>();
|
|
|
|
string text = obj.Get("text").As<Napi::String>().Utf8Value();
|
2020-07-22 18:12:38 +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;
|
|
|
|
Image text_image;
|
|
|
|
text_image.backgroundColor("none");
|
|
|
|
text_image.fontPointsize(72);
|
|
|
|
text_image.textGravity(Magick::CenterGravity);
|
|
|
|
text_image.font("Bitstream Vera Sans");
|
|
|
|
text_image.read("pango:<span foreground='white'>" + text + "</span>");
|
|
|
|
text_image.resize(Geometry(474, 332));
|
|
|
|
text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity);
|
|
|
|
image.read("./assets/images/sonic.jpg");
|
|
|
|
image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp);
|
|
|
|
image.magick("PNG");
|
|
|
|
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", "png");
|
|
|
|
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-22 18:12:38 +00:00
|
|
|
}
|