Hello,
The problem is that you're not setting the correct scope when you instanciate the WordFragment class to add the image. For example:
$headerImage = new WordFragment($docx, 'defaultHeader');
In your code $header has the scope, but for example $headerLeft (among others) doesn't have it.
Please check the included example LayoutAndGeneral/addHeader/sample_2.php. This example example adds an image, a text and a pager to the header and it works perfectly.
Regards.