Page 1 of 1

Huge problem with drawing images in PHP

Posted: Thu Jun 26, 2008 4:54 pm
by hora
I'm having the weirdest problem ever here.. I don't understand what's going on. I'm trying to draw an empty rectangle, so just a border with nothing inside.

First, I drew a filled one just to see what it looks like, here's my code:

Code: Select all

<?php
 
function show_black($level){
    $width = 200;
    $height = 20;
    $image = imagecreate($width, $height);
    
    $black = imagecolorallocate($image, 0,0,0);
 
    //imagefilledrectangle($image, 0, 0, 99, 20, $black);
    
    // flush image
    //header("Content-type: image/png");
    ImagePng($image, "picture.png") or die("wtf");
    imagedestroy($image);
    echo '<img src="picture.png" alt="bar" />';
}
 
?>
 
That worked fine, gave me a filled rectangle. Then I tried using just imagerectangle, here's the code:

Code: Select all

<?php
 
function show_black($level){
    $width = 200;
    $height = 20;
    $image = imagecreate($width, $height);
    
    $black = imagecolorallocate($image, 0,0,0);
 
    //imagerectangle($image, 0, 0, 200, 20, $black);
    
    // flush image
    //header("Content-type: image/png");
    ImagePng($image, "picture.png") or die("wtf");
    imagedestroy($image);
    echo '<img src="picture.png" alt="bar" />';
}
 
?>
 
It showed up EXACTLY the same as the filled rectangle... I thought maybe it was because it was displaying the old png file, so I deleted it off the server, and I tried it again. It still showed a filled rectangle.. Then I changed the name of the file to "graph.png", but it still created a filled rectangle.. I also tried drawing lines, using imageline, but it ALWAYS drew the original filled rectangle, no matter if I deleted the png off the server or if I changed the name of png.. What's going on???

Re: Huge problem with drawing images in PHP

Posted: Thu Jun 26, 2008 5:10 pm
by onion2k
When you create an image it isn't transparent by default. It'll be either black, or the background will be the same as the first color you allocate.

I wrote a function to create transparent images ages ago... http://www.phpgd.com/scripts.php?script=27

Alternatively... download the library in my signature and use that. You'd want to do something like:

Code: Select all

<?php
    
    include_once "image/image.inc.php";
 
    //Create a new image object
    $image = new image();
 
    //Create a transparent image
    $image->createImageTrueColorTransparent(200,20);
 
    //Draw a rectangle (could also use the image_draw_primitive plugin
    $rectangle = new image_draw_primitive();
    $rectangle->addRectangle(0,0, 199,19, "000000");
    $image->attach($rectangle);
 
    //Could do this instead if you prefered...
    //$black = $image->imagecolorallocate("000000");
    //imagerectangle($image->image, 0,0, 199,19, $black);
 
    //Save a copy
    $image->imagepng("picture.png");
 
    //Output it to a browser.
    $image->imagepng();
 
 
It's not really finished yet, not even close to be honest, but it's getting there.

Re: Huge problem with drawing images in PHP

Posted: Thu Jun 26, 2008 5:34 pm
by hora
Any idea why even if I tried drawing a line using imageline, it drew a rectangle? It seems to me like the ONLY thing I can draw is a rectangle..

After experimenting a little more, I noticed it draws my lines on TOP of the black rectangle... Is that because the first colour I allocate, will be the fill colour of my image??

Re: Huge problem with drawing images in PHP

Posted: Fri Jun 27, 2008 4:10 am
by onion2k
hora wrote:Is that because the first colour I allocate, will be the fill colour of my image??
Yes. Only because you're using imagecreate() though. If you used imagecreatetruecolor() it'd always be black to start with.