Classer les images en portrait selon l'orientation EXIF

This commit is contained in:
2026-06-04 13:36:10 +02:00
parent 369b882693
commit d900e7aa65

View File

@@ -12,6 +12,25 @@ function thalim_format_date($raw, $lang = 'fr', $format = 'j F Y') {
return date_i18n($format, $ts); 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 58
* (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. * 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) ?: '', 'alt' => get_post_meta($doc_id, '_wp_attachment_image_alt', true) ?: '',
'caption' => thalim_bilingual(wp_get_attachment_caption($doc_id) ?: '', $lang), 'caption' => thalim_bilingual(wp_get_attachment_caption($doc_id) ?: '', $lang),
'title' => thalim_bilingual(get_the_title($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 { } else {