How can masking be used in PHP to achieve specific image cropping effects?

To achieve specific image cropping effects in PHP, masking can be used by applying a transparent overlay image on top of the original image. The overlay image, also known as the mask, determines which parts of the original image will be visible and which will be hidden, creating the desired cropping effect.

// Load the original image
$originalImage = imagecreatefromjpeg('original.jpg');

// Load the mask image
$mask = imagecreatefrompng('mask.png');

// Apply the mask to the original image
imagecopy($originalImage, $mask, 0, 0, 0, 0, imagesx($originalImage), imagesy($originalImage));

// Output the final cropped image
header('Content-Type: image/jpeg');
imagejpeg($originalImage);

// Free up memory
imagedestroy($originalImage);
imagedestroy($mask);