Microsoft and Google hash your unencrypted photos on their own servers. Apple could easily do the same… I wonder why they didn’t…
That has to be done on the phone because Messages is end-to-end encrypted. If they are going to have to have hash matching on the phone anyway for that, it makes sense to also use that for checking images that are to be sent to the cloud.