tags:

views:

30

answers:

2

Hi, i met a very strange PHP behaviour, i don't understand why it behaves like this. I'm using the imagesetbrush function in this way:

class foo
{
   function setbrush($image)
   {
       //$this->_resource contains the main image resource
       imagesetbrush($this->_resource, $image);
   }
}
...
$res=imagecreatefrompng("image.png");
$class->setbrush($res);

in this way it works, but if i change the code like this:

class foo
{
   function setbrush($image)
   {
       $res=imagecreatefrompng($image);
       imagesetbrush($this->_resource, $res);
   }
}
...
$class->setbrush("image.png");

it doesn't work anymore. Do you see some error? It doesn't show me any message it simply doesn't execute the function.

+1  A: 

Could it be that the reference to the variable $res disappears after the function call? Have you tried declaring it as a class variable, just like the $_resource variable?

zaf
This works!!! I don't know why but this works!!! Thank you...
mck89
I've understood why it behaves like this so i write it. Imagesetbrush requires a reference to an image and in the second way the reference is unset when the function finishes so PHP can't get the brush resource. In the zaf's way the reference is mantained so it works.
mck89
A: 

Maybe somewhere in your foo class the working directory changes, so it can no longer find the image.png maybe when opening //$this->_resource contains the main image resource

Javier Parra