PHP Joomla! 6 Joomla! 5 Joomla! 3 (Legacy) Template Override Intermediate

Extract the First Image from Article Content

Finds the first <img> tag inside introtext or fulltext as a safe fallback when no structured intro image is set.

Updated on 24 November 2026 45 copies 185 views
templates/seu_template/html/com_content/category/blog_item.php
<?php

declare(strict_types=1);

use Joomla\CMS\HTML\HTMLHelper;

$images = json_decode((string) ($item->images ?? '{}'));
$image  = '';

if (!empty($images->image_intro)) {
    $image = HTMLHelper::cleanImageURL($images->image_intro)->url;
} elseif (preg_match('/<img[^>]+src=["\']([^"\']+)["\']/i', (string) $item->introtext, $matches)) {
    $image = HTMLHelper::cleanImageURL($matches[1])->url;
}
Checks structured image_intro with HTMLHelper::cleanImageURL() before falling back to HTML parsing.
templates/seu_template/html/com_content/category/blog_item.php
<?php
preg_match('/(?<!_)src=([\'"])?(.*?)\1/', $item->introtext, $matches);
$image = $matches[2] ?? '';
Original Joomla 3 regex extraction from $item->introtext.

Technical explanation and best practices

Handy for blog cards, featured modules and Open Graph meta tags when older articles embed images inline inside HTML instead of filling the structured image_intro field.

The modern version checks image_intro first and normalizes Joomla 5/6 #joomlaImage:// hashes with HTMLHelper::cleanImageURL().

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.