// ==========================================
// STEP 1: Preload LCP Image (Универсальный поиск)
// ==========================================
$lcp_img = '';
// 1. Пробуем стандартное фото Joomla
if (isset($this->item->images)) {
$images = json_decode($this->item->images);
if (!empty($images->image_intro)) {
$lcp_img = $images->image_intro;
} elseif (!empty($images->image_fulltext)) {
$lcp_img = $images->image_fulltext;
}
}
// 2. Если пусто, ищем первую картинку из поля $docs (fieldattach) или текста статьи
if (empty($lcp_img)) {
$html_to_search = '';
if (isset($docs) && is_string($docs)) { $html_to_search .= $docs; }
if (isset($this->item->fulltext)) { $html_to_search .= $this->item->fulltext; }
if (isset($this->item->introtext)) { $html_to_search .= $this->item->introtext; }
if (!empty($html_to_search) && preg_match('/<img[^>]+src=["\']([^"\']+)["\']/i', $html_to_search, $matches)) {
$lcp_img = $matches[1];
}
}
// 3. Выводим preload в HEAD
if (!empty($lcp_img)) {
// Формируем корректный URL
if (strpos($lcp_img, 'http') !== 0) {
$lcp_img = JURI::root(false) . '/' . ltrim($lcp_img, '/');
}
$doc->addCustomTag('<link rel="preload" as="image" href="' . htmlspecialchars($lcp_img) . '" fetchpriority="high">');
}