diff --git a/src/app.cpp b/src/app.cpp index abcfb06..5fdd52d 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -39,6 +39,7 @@ #include #include #include +#include App::App(const Wt::WEnvironment &env, Broadcast &server): Wt::WApplication(env), @@ -446,7 +447,7 @@ void App::sendImage() { auto image = layout->addNew(); image->setMaximumSize(Wt::WLength(100, Wt::LengthUnit::Pixel), Wt::WLength(100, Wt::LengthUnit::Pixel)); image->setHeight(Wt::WLength::Auto); - auto localImage = std::make_shared(); + auto localImage = std::make_shared(); auto buttonsContainer = layout->addNew(); auto okButton = buttonsContainer->addNew("Send image"); fileWidget->uploaded().connect([=, this]() mutable { @@ -466,29 +467,33 @@ void App::sendImage() { fileDialog->show(); } -void App::imageUploaded(Wt::WFileUpload *fileWidget, std::shared_ptr localImage, Wt::WImage *image, Wt::WPushButton *okButton) { +void App::imageUploaded(Wt::WFileUpload *fileWidget, std::shared_ptr localImage, Wt::WImage *image, Wt::WPushButton *okButton) { try { std::list originalImages; Magick::readImages(&originalImages, fileWidget->spoolFileName()); - std::list coalescedList; - Magick::coalesceImages(&coalescedList, originalImages.begin(), originalImages.end()); - Magick::Image coalescedImage; - Magick::Blob coalescedBlob; - Magick::writeImages(coalescedList.begin(), coalescedList.end(), &coalescedBlob); - coalescedImage.read(coalescedBlob); - *localImage = coalescedImage; - Magick::Blob previewBlob; - coalescedImage.write(&previewBlob); - Magick::writeImages(coalescedList.begin(), coalescedList.end(), &previewBlob); - auto previewBase64 = previewBlob.base64(); - auto previewImage = Wt::Utils::base64Decode(previewBase64); - std::string imageFormat = coalescedImage.magick(); + std::list previewList; + std::list localList; + std::transform(originalImages.begin(), originalImages.end(), std::back_inserter(previewList), [](const Magick::Image& img) { return img; }); + std::transform(originalImages.begin(), originalImages.end(), std::back_inserter(localList), [](const Magick::Image& img) { return img; }); + Magick::coalesceImages(&previewList, previewList.begin(), previewList.end()); + Magick::coalesceImages(&localList, localList.begin(), localList.end()); + auto resize = [](std::string size, std::list &imagesList) { + for (auto &frame : imagesList) { + frame.resize(size); + } + }; + resize("100x100^", previewList); + resize("500x500^", localList); + Magick::Blob resizedBlob; + Magick::writeImages(previewList.begin(), previewList.end(), &resizedBlob); + Magick::writeImages(localList.begin(), localList.end(), &(*localImage)); + auto resizedBase64 = resizedBlob.base64(); + auto resizedImageString = Wt::Utils::base64Decode(resizedBase64); + std::string imageFormat = previewList.front().magick(); auto imageResource = std::make_shared( "image/" + imageFormat, - std::vector(previewImage.begin(), previewImage.end())); + std::vector(resizedImageString.begin(), resizedImageString.end())); image->setImageLink(Wt::WLink(imageResource)); - image->resize(Wt::WLength(localImage->columns(), Wt::LengthUnit::Pixel), Wt::WLength(localImage->rows(), Wt::LengthUnit::Pixel)); - std::cout << __LINE__ << std::endl; okButton->setEnabled(true); triggerUpdate(); } catch (const std::exception& e) { @@ -496,6 +501,8 @@ void App::imageUploaded(Wt::WFileUpload *fileWidget, std::shared_ptraddNew(); + std::cout << __LINE__ << std::endl; auto outputText = Wt::WString("{1}: ").arg(writer); + std::cout << __LINE__ << std::endl; imageLineItem->addNew(outputText)->setStyleClass("output-line"); + std::cout << __LINE__ << std::endl; auto image = imageLineItem->addNew(); - auto imageBlob = Wt::Utils::base64Decode((std::string)imageDescription["imageblobbase64"]); + std::cout << __LINE__ << std::endl; + std::string base64Data = (std::string)imageDescription["imageblobbase64"]; + std::string decodedData = Wt::Utils::base64Decode(base64Data); // Decode Base64 data + Magick::Blob imageBlob(decodedData.data(), decodedData.size()); Magick::Image magickImage; magickImage.read(imageBlob); std::string imageFormat = magickImage.magick(); + std::cout << __LINE__ << std::endl; + std::cout << imageFormat << std::endl; + Magick::Blob blob; + magickImage.write(&blob); + auto resizedBase64 = blob.base64(); + auto resizedImageString = Wt::Utils::base64Decode(resizedBase64); auto imageResource = std::make_shared( "image/" + imageFormat, - std::vector(imageBlob.begin(), imageBlob.end())); + std::vector(resizedImageString.begin(), resizedImageString.end())); // Verwenden Sie den direkt decodierten Inhalt + std::cout << __LINE__ << std::endl; image->setImageLink(Wt::WLink(imageResource)); - image->resize(Wt::WLength((int)imageDescription["width"], Wt::LengthUnit::Pixel), Wt::WLength((int)imageDescription["height"], Wt::LengthUnit::Pixel)); imageLineItem->setAttributeValue("dummy", id); return imageLineItem; } @@ -767,7 +787,6 @@ void App::updateUserinfo(Wt::Json::Object data) { auto infoWidgetLayout = (Wt::WHBoxLayout*)infoWidget->layout(); auto infoTextWidget = dynamic_cast(infoWidgetLayout->itemAt(1)->widget()); auto blockButton = dynamic_cast(infoWidgetLayout->itemAt(2)->widget()); - std::cout << Wt::Json::serialize(userData) << std::endl; try { infoWidget->setStyleClass(Wt::WString("user-conversation-info userlist-gender-{1}").arg((std::string)userData["gender"])); if (infoTextWidget) { diff --git a/src/app.h b/src/app.h index d251953..a452312 100644 --- a/src/app.h +++ b/src/app.h @@ -207,7 +207,7 @@ private: void addTimeoutView(); void showPartnerSites(); void sendImage(); - void imageUploaded(Wt::WFileUpload *fileWidget, std::shared_ptr localImage, Wt::WImage *image, Wt::WPushButton *okButton); + void imageUploaded(Wt::WFileUpload *fileWidget, std::shared_ptr localImage, Wt::WImage *image, Wt::WPushButton *okButton); bool isAnimatedGIF(const Magick::Blob &blob); }; diff --git a/src/broadcast.cpp b/src/broadcast.cpp index ba80fd7..b16fb6a 100644 --- a/src/broadcast.cpp +++ b/src/broadcast.cpp @@ -255,7 +255,7 @@ Wt::Json::Object Broadcast::addMessage(std::string fromSessionId, std::string to }; } -Wt::Json::Object Broadcast::addImage(std::string fromSessionId, std::string toUserName, std::shared_ptr image) { +Wt::Json::Object Broadcast::addImage(std::string fromSessionId, std::string toUserName, std::shared_ptr image) { auto message = Message(fromSessionId, image); return addMessage(fromSessionId, toUserName, message); } @@ -658,7 +658,7 @@ Broadcast::Message::Message(std::string fromSessionId_, Wt::WString message_): sendTime = Wt::WDateTime::currentDateTime(); } -Broadcast::Message::Message(std::string fromSessionId_, std::shared_ptr image_): +Broadcast::Message::Message(std::string fromSessionId_, std::shared_ptr imageBlob): fromSessionId(fromSessionId_), sendType("image") { auto timestamp = std::time(nullptr); @@ -667,12 +667,12 @@ Broadcast::Message::Message(std::string fromSessionId_, std::shared_ptrwrite(&imageBlob); + Magick::Image image_; + image_.read(*imageBlob); image = { - {"width", Wt::Json::Value((int)image_->columns())}, - {"height", Wt::Json::Value((int)image_->rows())}, - {"imageblobbase64", Wt::Json::Value(imageBlob.base64())} + {"width", Wt::Json::Value((int)image_.columns())}, + {"height", Wt::Json::Value((int)image_.rows())}, + {"imageblobbase64", Wt::Json::Value(imageBlob->base64())} }; } diff --git a/src/broadcast.h b/src/broadcast.h index 3582bd7..c56876e 100644 --- a/src/broadcast.h +++ b/src/broadcast.h @@ -14,6 +14,7 @@ namespace Magick { class Image; + class Blob; } struct ConversationItem { @@ -50,7 +51,7 @@ public: struct Message { Message()= default; Message(std::string fromSessionId_, Wt::WString message_); - Message(std::string fromSessionId_, std::shared_ptr image_); + Message(std::string fromSessionId_, std::shared_ptr image_); Message(Wt::WString message_); bool systemMessage{false}; std::string fromSessionId; @@ -86,7 +87,7 @@ public: Wt::WString getCountryIsoCodeByCountry(Wt::WString country); std::list getBroadcastsForSession(std::string sessionId); Wt::Json::Object addMessage(std::string fromSessionId, std::string toUserName, Message message); - Wt::Json::Object addImage(std::string fromSessionId, std::string toUserName, std::shared_ptr image); + Wt::Json::Object addImage(std::string fromSessionId, std::string toUserName, std::shared_ptr image); void setConversationRead(std::string readingUserSessionId, std::string secondUserName); void sendConversation(std::string sessionId1, std::string userName2); void sendOpenConversations(std::string forSessionId);