John
John

Reputation: 11

Why php imagefill always black?

This is driving me mad. I would really appreciate if you told me any idea about why I see this square in red color just in my local xampp installation. If I run the code in the remote server (http://www.arreglaordenador.com/numberimage2.php) I see the square in black color instead of red. Do you have any ideas?

<?php

$im = imagecreatetruecolor(100, 100);

// sets background to red
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?> 

Thanks

Upvotes: 1

Views: 3182

Answers (4)

AGMG
AGMG

Reputation: 3948

It's because you have used imagecreatetruecolor() function to create image and your GD version has bugs. imagecreatetruecolor() won't override by some color function in some GD versions(I don't remember which). You have two solutions.

  1. Ask your hosting service to upgrade GD to latest stable version.

or

  1. Use imagecreate() function to create image identifier instead imagecreatetruecolor().

Upvotes: 3

Valentin Flachsel
Valentin Flachsel

Reputation: 10825

This is definitely a GD issue on your server because your code works just fine on both my local WAMP and on my hosting account.

Upvotes: 0

Jordan Ryan Moore
Jordan Ryan Moore

Reputation: 6887

Based on the imagecreatetruecolor() documentation, your server probably doesn't have the correct version of the GD image library installed.

Upvotes: 1

Pekka
Pekka

Reputation: 449385

Can you try allocating a different colour first?

$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);

I don't exactly see why, but this looks like a transparency issue to me (i.e. red being selected as a transparent colour for some reason).

Most probably, this is due to different GD versions. Can you compare which one you have locally and which one remotely?

Upvotes: 1

Related Questions