Get the image position as an ID

Hi,
I’m develloping a website to learn kirby.
I’d like to give my image a specific id using their position in the grid from my panel.
For exemple, if my image is the third in the panel, it will have “3” as an ID.
Actually i’m using $image->hash() in order to give a unique id to each image, but it’s not as clean as I expected…
Am I missing something?

Thanks.

Are the files sorted manually?

Hi texnixe,
They are sorted using the ->SortBy(‘sort’) method.
So when they are reorganized in the panel, they are moved in the frontend.

If all images have a sorting number (i.e. you manually sort them in the Panel), you can get the sorting number with $image->sort().

Alternatively, you can print the index of the file in the collection inside the loop, example:

<?php
$images = page->images()->sortBy('sort');
foreach ($images as $image) {
  echo $images->indexOf($image) + 1; // + 1 because the index starts at zero.
}

Seems to be working just fine.
Somehow I wasn’t able to find that on the documentation…
Thanks a lot!