Better still, it’s good practice to check the image physically exists first, since its possible for the file itself to be deleted but the reference to it can remain in your field. Errors will be thrown if you try to work with an image that does not exist…
On a side note, I would suggest not using $1 to add classes to the project, it’s not safe. If you change the sorting for example, the class number will potentially end up on another project, which will apply the CSS rule to the wrong project.
You could use the UID instead which is safer but can change if you update the page title:
class="<?= $project->uid() ?>"
If you really want to add a fixed unique class to the project that will never change, I would suggest the AutoID plugin. You need to save all the pages after installing it to generate the unique ID numbers.
well i simplified the loop in my code above. in reality its a little complicated then that. there’s some other dom elements in between each projects haha. and using nth for those situations is not ideal.