From b0954a164cf0538efc5cd5eed453e5421200b80d Mon Sep 17 00:00:00 2001 From: tao wang Date: Wed, 13 Sep 2023 19:32:59 +0800 Subject: [PATCH] ENH:update base64 conversion method jira:[for model mall] Change-Id: Ia609ae4a559e0e2711e37db32738c057e8a8a33a --- src/slic3r/GUI/Project.cpp | 45 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/slic3r/GUI/Project.cpp b/src/slic3r/GUI/Project.cpp index 78e120c9a..aefffccc0 100644 --- a/src/slic3r/GUI/Project.cpp +++ b/src/slic3r/GUI/Project.cpp @@ -328,9 +328,13 @@ std::map> ProjectPanel::Reload(wxString aux_path) boost::algorithm::to_lower(file_extension); //image - if (file_extension == ".jpg" || - file_extension == ".jpeg" || - file_extension == ".png" || + if (file_extension == ".jpg" || + file_extension == ".jpeg" || + file_extension == ".pjpeg" || + file_extension == ".png" || + file_extension == ".jfif" || + file_extension == ".pjp" || + file_extension == ".webp" || file_extension == ".bmp") { @@ -360,27 +364,30 @@ std::string ProjectPanel::formatBytes(unsigned long bytes) wxString ProjectPanel::to_base64(std::string file_path) { - std::map base64_format; - base64_format[".jpg"] = wxBITMAP_TYPE_JPEG; - base64_format[".jpeg"] = wxBITMAP_TYPE_JPEG; - base64_format[".png"] = wxBITMAP_TYPE_PNG; - base64_format[".bmp"] = wxBITMAP_TYPE_BMP; - - std::string extension = file_path.substr(file_path.rfind("."), file_path.length()); - boost::algorithm::to_lower(extension); + std::ifstream imageFile(encode_path(file_path.c_str()), std::ios::binary); + if (!imageFile) { + return wxEmptyString; + } - auto image = new wxImage(encode_path(file_path.c_str())); - wxMemoryOutputStream mem; - image->SaveFile(mem, base64_format[extension]); + std::ostringstream imageStream; + imageStream << imageFile.rdbuf(); + + std::string binaryImageData = imageStream.str(); + + std::string extension; + size_t last_dot = file_path.find_last_of("."); + + if (last_dot != std::string::npos) { + extension = file_path.substr(last_dot + 1); + } + + wxString bease64_head = wxString::Format("data:image/%s;base64,", extension); - wxString km = wxBase64Encode(mem.GetOutputStreamBuffer()->GetBufferStart(), - mem.GetSize()); std::wstringstream wss; - wss << L"data:image/jpg;base64,"; - //wss << wxBase64Encode(km.data(), km.size()); - wss << km; + wss << bease64_head; + wss << wxBase64Encode(binaryImageData.data(), binaryImageData.size()); wxString base64_str = wss.str(); return base64_str;