diff --git a/inc/single-helpers.php b/inc/single-helpers.php index 33e6dba..821d0b4 100644 --- a/inc/single-helpers.php +++ b/inc/single-helpers.php @@ -12,6 +12,25 @@ function thalim_format_date($raw, $lang = 'fr', $format = 'j F Y') { return date_i18n($format, $ts); } +/** + * Tell whether an image is displayed in portrait, taking EXIF orientation + * into account. WordPress stores the raw (un-rotated) width/height, but the + * browser auto-rotates JPEGs that carry an EXIF orientation flag of 5–8 + * (90°/270° rotation), which swaps the displayed dimensions. Without this, + * a phone photo shot in portrait can be mis-classified as landscape. + */ +function thalim_image_is_portrait($doc_id, $w, $h) { + $path = get_attached_file($doc_id); + if ($path && function_exists('exif_read_data') && preg_match('/\.jpe?g$/i', $path)) { + $exif = @exif_read_data($path); + $orientation = isset($exif['Orientation']) ? (int) $exif['Orientation'] : 0; + if (in_array($orientation, [5, 6, 7, 8], true)) { + [$w, $h] = [$h, $w]; + } + } + return $h > $w; +} + /** * Resolve all Pods custom fields for a single post into a display-ready array. */ @@ -158,7 +177,7 @@ function thalim_get_single_data($post_id) { 'alt' => get_post_meta($doc_id, '_wp_attachment_image_alt', true) ?: '', 'caption' => thalim_bilingual(wp_get_attachment_caption($doc_id) ?: '', $lang), 'title' => thalim_bilingual(get_the_title($doc_id) ?: '', $lang), - 'portrait' => ($h > $w), + 'portrait' => thalim_image_is_portrait($doc_id, $w, $h), ]; } } else {