Well it is possible, but it would put much more stress on your server than having the current resizing, because the php script would have to resize the image, check it size, if the size is higher than 50KB change the quality image, recreate the thumbnail... until it get under 50KB.
PHP is not a language made to edit images, it can perform basic operations, but these operations should never be use in a loop, the stress on the server would be much higher X the number of users you have on your website...
I strongly do not recommand it.