PHP version of the image manipulation part of Reddit's thumbnail creator