imagecopyresampled() or imagecopyresized() should do exactly what you want.
(Despite its name, imagecopyresampled also resizes.)
They take some or all of the original image, resize it and copy it to the target image. If the original image is not square, you can crop or distort it when copying it.
source share