How can file_exists function be implemented in Smarty to check if an image exists in PHP?

To check if an image exists in PHP using the file_exists function in Smarty, you can create a custom function in Smarty that utilizes the file_exists function in PHP. This custom function can take the image path as a parameter and return a boolean value indicating whether the image exists or not.

// Custom function to check if an image exists
function smarty_function_image_exists($params, $smarty) {
    $imagePath = $params['image_path'];
    
    if (file_exists($imagePath)) {
        return true;
    } else {
        return false;
    }
}

// Register the custom function in Smarty
$smarty->registerPlugin('function', 'image_exists', 'smarty_function_image_exists');

// Usage in Smarty template
{if image_exists image_path="path/to/image.jpg"}
    <img src="path/to/image.jpg" alt="Image">
{else}
    <p>Image not found.</p>
{/if}