You could load with GraphicsMagick classes, e.g. GMimageMBS class.
Then you could try various things like scaling down in a loop. Maybe also trying various JPEG compression levels.
Doing it in a loop sounds sensible, but that implies saving to disc over and over again, which I would be wanting to avoid.
Im really looking for If I use THESE settings, how big will the file be ?
and check that in a loop , so that the save occurs one time only.
The stupid thing about Etsy is that they apparently insist on an image that is 2000 pixels or wider, and yet also impose a MB limit.
I’ve done a similar thing before by saving to the temp folder in the loop, then saving the correct sized file at the user’s request. Then you can show X by Y pixels = Z MB to allow for selection by the user.
You would do a binary search and maybe do a starting scale.
e.g. start with 5000x5000 limit (proportional scale down), compress to string (not file) and check length.
Then maybe go up/down.