You are correct, this way you will be creating thumbnails all the time... One possibility would be to adjust your implementation in such a way, so that it will only create thumbnails on the first load and then store their paths/names in the database. Then you can check against the existence of thumbnails and their consistency with the original file and only recreate them if you have updated the original.
It's also possible to address this during content creation with a content plugin.
https://docs.joomla.org/Plugin/Events/Content#onContentBeforeSave