Before you modify the width/height, store them:
$actual_width = $desired_width;
$actual_height = $desired_height;
$desired_height = floor($height*($desired_width/$width));
$desired_width = floor($width*($desired_height/$height));
When you are doing the canvas:
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($actual_width, $actual_height);
Virtual image at this point is black, fill it with white:
$white = imagecolorallocate($virtual_image, 255, 255, 255);
imagefill($virtual_image, 0, 0, $white );