/** * Generates an optimized HTML5 gallery from a server directory. * Utilizes pre-defined PATH_ROOT constant for environmental stability. * * @param string $relative_path Path from your project root (e.g., '/_/images/gallery/') * @return string HTML output of picture tags */ function render_gallery(string $relative_path): string { // 1. Leverage your existing global constant to find the absolute directory $clean_rel_path = '/' . trim($relative_path, '/'); $absolute_dir = PATH_ROOT . $clean_rel_path; if (!is_dir($absolute_dir)) { return ''; } // 2. Scan for JPEG images using glob() // Using DIRECTORY_SEPARATOR ensures Windows (/) vs Linux (\) stability $jpeg_files = glob($absolute_dir . DIRECTORY_SEPARATOR . '*.{jpg,jpeg}', GLOB_BRACE); // if (empty($jpeg_files)) { return ''; } $html_output = ''; foreach ($jpeg_files as $jpeg_path) { $filename = basename($jpeg_path); $filename_no_ext = pathinfo($filename, PATHINFO_FILENAME); // Formulate the paths for web rendering $clean_relative_base = rtrim($relative_path, '/') . '/'; $jpg_url = $clean_relative_base . $filename; $webp_url = $clean_relative_base . $filename_no_ext . '.webp'; $webp_absolute_path = $absolute_dir . DIRECTORY_SEPARATOR . $filename_no_ext . '.webp'; // // 3. Dynamically obtain Image Dimensions (Width/Height) $dimensions = getimagesize($jpeg_path); $width_attr = $dimensions ? $dimensions[0] : ''; $height_attr = $dimensions ? $dimensions[1] : ''; // 4. Extract Title Metadata for the Alt tag $alt_text = 'Gallery image'; // Fallback $exif_data = @exif_read_data($jpeg_path); if (!empty($exif_data['WINNODES']['Title'])) { // Windows 11 Title tag mapping (Converts UTF-16LE payload cleanly) $alt_text = iconv('UTF-16LE', 'UTF-8', $exif_data['WINNODES']['Title']); } elseif (!empty($exif_data['ImageDescription'])) { // Photoshop / Standard EXIF Description mapping $alt_text = $exif_data['ImageDescription']; } elseif (!empty($exif_data['Title'])) { $alt_text = $exif_data['Title']; } // Sanitize output for security $alt_text = htmlspecialchars(trim($alt_text), ENT_QUOTES, 'UTF-8'); // // 5. Build the semantic markup $html_output .= " \n"; // If a matching webp version exists, output it as the primary source choice if (file_exists($webp_absolute_path)) { $html_output .= " \n"; } // Final fallback img tag containing dimensions to prevent layout shifts $html_output .= " \"{$alt_text}\"\n"; $html_output .= " \n"; } return $html_output; }