While replying to a post in a support forum I noticed something odd about imagecopy(). The first snippet (should) create an image object, allocate a colour resource within that image, fill the background with the allocated colour and then copy another, cropped to fit, image onto it.
<?php
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );
imagefill( $temp, 0, 0, $white );
imagecopy($temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height);
?>
But this produces a black background. I noticed taking away the imagefill() call yields the same results. The solution was to call imagefill() after the imagecopy(). Thinking linearly I would have guessed this to cover the previously copied image in white but it doesn't. I guess GD uses a layer system? Is this correct?
<?php
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );
imagecopy( $temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height );
imagefill( $temp, 0, 0, $white );
?>
I am using php 5.1.4 with the bundled GD (2.0.28)