Hey JBW,
thanks so much for your help. Unfortunately I have only basic understanding of coding. After using the code that I have posted here I was facing problems with uploading new images for a product.
I am actually testing where the problem could be.
So the code above is working only on pictures that were already uploaded but not on pictures that will be newly uploaded.
I will test your version too and let you know how it worked.