Sortby date created image

I’m trying to sort by dateCreated of the image. There is though not explicit field that would store this information, rather it’s in the binary field it-self:

<?php foreach ($site->find('projekte')->children()->images()->filterBy('category', $page->uri(), ',')->sortBy('dateCreated', 'desc') as $img): ?>

      <div class="col-lg-6 p-3 p-xl-5">
       <div class="portfolio-image">
            <img class="img-fluid w-100 " src="<?= $img->resize(600)->url() ?>" alt="<?= $img->alt() ?>">
       </div>
      </div>

    <?php endforeach ?>

Unfortunately this doesn’t have any effect. Do I have to explicitely store that information in a field?

dateCreated doesn’t seem to be a default file method, there is modified, but that does something slightly different.

So yes, you’d have to create a field for that, or you could create a fileMethod plugin that retrieves that information from somewhere (like the exif data).

Something like:

/site/plugins/file-methods/index.php

<?php 

use Kirby\Cms\App as Kirby;

Kirby::plugin('my/plugin', [
    'fileMethods' => [
        'dateCreated' => function () {
           $root = $this->root();

           // search exif data
           $ifd0 = @exif_read_data($root, 'IFD0');
           if($date = $ifd0['DateTime'] ?? false) return strtotime($date);

           // try ctime (might work on windows)
           if($date = filectime($root)) return $date;

           // fallback to mtime
           return $this->modified();
        }
    ]
]);


thanks. I went with modified. That’s good enough.