Displaying image, prefering local to distant

Hi,
I have a bunch of image files in some directory, and would like to display one if present locally or use a distant one if not.

So I’ve tried this : `

    <?php if ($page->daoloc()->isNotEmpty()): ?>
        <div class="fondintroimg">
            <?php
            // Assuming $page->daoloc() returns the image filename
            $localImagePath = '/archives/vignettes_fonds/' . $page->daoloc();
            $webImagePath = 'https://some.website.elsewhere/medias/vignettes_fonds/' . $page->daoloc();

            if (file_exists($_SERVER['DOCUMENT_ROOT'] . $localImagePath)) {
                // Use the local image path if it exists
                $imagePath = $localImagePath;
            } else {
                // Use the web URL if the local image doesn't exist
                $imagePath = $webImagePath;
            }
            ?>
            <img class="width-full max-h-60" src="<?= $imagePath ?>" alt="image for <?= $page->title() ?>">
        </div>
    <?php endif ?>`

And the local file never gets picked up although it is present in the /archives/vignettes_fonds/ (or /1_archives/9_vignettes_fonds/ in real).

Yet when I try to access it directly, it does work and is displayed, as http://www.test/archives/vignettes_fonds/ARB2.jpg, which is translated to http://www.test/media/pages/archives/vignettes_fonds/9d871b2d4c-1647549709/ARB2.jpg

It seemed fairly straighforward as php goes, but the Kirby part not quite.

Thanks for any insights !

j.

For the local image, you need to convert whatever is stored in the daoloc field into a file object, for example, if the filename is stored, then

<?php
$file = page('archives/vignettes_fonds')->image($page->daoloc()->value());
$url = $file ? $file->url() : $webImagePath;
?>
            <img class="width-full max-h-60" src="<?= $url ?>" alt="image for <?= $page->title() ?>">

Perfect ! Thank you Sonja