[config] Improve dimensions validation and fix online_image resize aspect ratio (#14274)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Jonathan Swoboda
2026-02-25 10:45:50 -05:00
committed by GitHub
co-authored by Copilot
parent bb05cfb711
commit 228874a52b
2 changed files with 13 additions and 1 deletions
@@ -2,6 +2,7 @@
#include "image_decoder.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include <algorithm>
#include <cstring>
#ifdef USE_RUNTIME_IMAGE_BMP
@@ -43,6 +44,14 @@ int RuntimeImage::resize(int width, int height) {
int target_width = this->fixed_width_ ? this->fixed_width_ : width;
int target_height = this->fixed_height_ ? this->fixed_height_ : height;
// When both fixed dimensions are set, scale uniformly to preserve aspect ratio
if (this->fixed_width_ && this->fixed_height_ && width > 0 && height > 0) {
float scale =
std::min(static_cast<float>(this->fixed_width_) / width, static_cast<float>(this->fixed_height_) / height);
target_width = static_cast<int>(width * scale);
target_height = static_cast<int>(height * scale);
}
size_t result = this->resize_buffer_(target_width, target_height);
if (result > 0 && this->progressive_display_) {
// Update display dimensions for progressive display
+4 -1
View File
@@ -1640,7 +1640,10 @@ def dimensions(value):
if width <= 0 or height <= 0:
raise Invalid("Width and height must at least be 1")
return [width, height]
value = string(value)
if not isinstance(value, str):
raise Invalid(
"Dimensions must be a string (WIDTHxHEIGHT). Got a number instead, try quoting the value."
)
match = re.match(r"\s*([0-9]+)\s*[xX]\s*([0-9]+)\s*", value)
if not match:
raise Invalid(