Jump to content

GD imagecopy problems with black background


Jak

Recommended Posts

This function is part of a class, it basically creates a new image in the dimensions specified, and then copies in the image held in $this->image. The image that is copied in will never be bigger than the new image (that is taken care of in another function) but one or both of the dimensions could be less than the new image (if the ratio isn’t the same).

It all works perfectly, it creates a new true colour image, fills it in white, but then when it runs the imagecopy function it seems to forget the fact that I set the background to white, and the image that results has a black border.

For example if I wanted a 100x100 image, and I was copying a 100x50 image in, I would end up with a 100x100 image with the old image centred vertically and a 25px black stripe above and below, I want those stripes to be white.

Any ideas?

[code=php:0]function crop($width = null, $height = null)
{
// get the current widths and heights
$current_width = imagesx($this->image);
$current_height = imagesy($this->image);

$crop_top = floor(($current_width - $width) / 2);
$crop_left = floor(($current_height - $height) / 2);

// create a new image resource
$temp = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($temp, 255, 255, 255);
imagefill($temp, 0, 0, $white);

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

// destroy original image
imagedestroy($this->image);

// copy new image
$this->image = $temp;
}
[/code]

Thanks in advance,
Jack

P.S. This function probably shouldnt actually be called crop, as thats not what it does, incase that confused anyone.
Link to comment
Share on other sites

For anyone thats interested, i managed to fix it. You have to put the imagefill after the image copy, i dont understand why this dosent cover the entire with white, but for some reason it dosent.

[code=php:0]// create a new image resource
$temp = imagecreatetruecolor($width, $height);

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

// fill the image background with white
$white = imagecolorallocate($temp, 255, 255, 255);
imagefill($temp, 0, 0, $white);[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.