Voting

: min(eight, two)?
(Example: nine)

The Note You're Voting On

C. Jansen
18 years ago
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
// create a new image resource
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );

//fill the background with white
imagefill( $temp, 0, 0, $white );

//copy the image into new a resource
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
// create a new image resource
$temp = imagecreatetruecolor( $width, $height );
$white = imagecolorallocate( $temp, 255, 255, 255 );

// copy image into new resource
imagecopy( $temp, $this->Image, 0, 0, $crop_top, $crop_left, $width, $height );

//fill the background with white (not sure why it has to be in this order)
imagefill( $temp, 0, 0, $white );
?>

I am using php 5.1.4 with the bundled GD (2.0.28)

<< Back to user notes page

To Top