In the spirit of https://commons.wikimedia.org/wiki/User:Fae/Imagehash, use https://github.com/KilianB/JImageHash to generate perceptual hashes for all pictures.
Compare these hashes to find duplicated images before upload, always upload the best one.