From 6071c4720db307d688edc7c4b84b6bb30fd3b963 Mon Sep 17 00:00:00 2001 From: blankie Date: Tue, 20 Feb 2024 23:05:44 +1100 Subject: [PATCH] Fix finding thumbnail size for /img-master/ images https://pixwhile.zangetsu.kaizoku.cyou/artworks/115861083 https://pixwhile.zangetsu.kaizoku.cyou/artworks/116018582 https://pixwhile.zangetsu.kaizoku.cyou/artworks/84869687 --- pixivmodels.cpp | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/pixivmodels.cpp b/pixivmodels.cpp index c253160..9d45dfe 100644 --- a/pixivmodels.cpp +++ b/pixivmodels.cpp @@ -308,21 +308,45 @@ static std::optional> get_thumbnail_size(blankie:: return std::nullopt; } - std::smatch sm; - if (!std::regex_match(thumbnail_url.path, sm, illust_size_regex)) { - return std::nullopt; - } + uint64_t thumbnail_width, thumbnail_height; - uint64_t thumbnail_width = to_ull(sm.str(1)); - // uint64_t thumbnail_height = to_ull(sm.str(2)); + if (thumbnail_url.path.starts_with("/img-master/")) { + if (original_size->first <= 1200 && original_size->second <= 1200) { + return original_size; + } + + // one side is implicitly 1200 + if (original_size->first >= original_size->second) { + // original width >= original height + + thumbnail_width = 1200; + // derived from original_size->second / (original_size->first / thumbnail_width) + // to make it more accurate without using floats + thumbnail_height = original_size->second * thumbnail_width / original_size->first; + } else { + // original width < original height + + thumbnail_height = 1200; + // derived from original_size->first / (original_size->second / thumbnail_height) + // to make it more accurate without using floats + thumbnail_width = original_size->first * thumbnail_height / original_size->second; + } + } else { + std::smatch sm; + if (!std::regex_match(thumbnail_url.path, sm, illust_size_regex)) { + return std::nullopt; + } + + thumbnail_width = to_ull(sm.str(1)); + // thumbnail_height = to_ull(sm.str(2)); - std::pair real_thumbnail_size = { - thumbnail_width, // derived from original_size->second / (original_size->first / thumbnail_width) // to make it more accurate without using floats - original_size->second * thumbnail_width / original_size->first - }; - return real_thumbnail_size; + thumbnail_height = original_size->second * thumbnail_width / original_size->first; + } + + std::pair thumbnail_size = {thumbnail_width, thumbnail_height}; + return thumbnail_size; } static Images get_illust_images(const nlohmann::json& image, std::optional image_metadata) {