I had a bit of a fiddle.
<?php
$image = imagecreatefromjpeg('input.jpg');
$x = imagesx($image);
$y = imagesy($image);
$mask = imagecreatetruecolor($x,$y);
$white = imagecolorallocate($mask,255,255,255);
$blank = imagecolorallocate($mask,0,0,0);
imagefilledrectangle($mask, 0, 0, $x, $y, $white); // Or draw other background.
imagecolortransparent($mask, $blank);
$centrex = (int)($x/2);
$centrey = (int)($y/2);
$diameter = (int)min($x, $y);
imagefilledellipse($mask, $centrex, $centrey, $diameter, $diameter, $blank);
imagecopymerge($image, $mask, 0, 0, 0, 0, $x, $y, 100);
imagejpeg($image, 'output.jpg');