Jump to content

In a function how do I keep calling imagecopy to stack image layers on the newly created image each time?


3dron
 Share

Recommended Posts

I have code like this in a function: 

 function stacklayer($folder, $layer) {

       $newlayer = imagecreatefrompng($folder . '/' . $layer . '.png');
        imagecopy($baseimage,$newlayer,0,0,0,0,$w,$h);
        imagedestroy($newlayer); 
}

stacklayer('Layer1', 'Image1')

But $baseimage does not have the new layers added on top of $baseimage created outside the function. 

In this manner:

$baseimage = imagecreatefrompng("Base.png");
$w = imagesx($baseimage); $h = imagesy($baseimage);
imagealphablending($baseimage,true);

 

 

If I run this outside the function it adds the layers as intended.

$folder = 'Layer1', $layer = 'Image1';
$newlayer = imagecreatefrompng($folder . '/' . $layer . '.png');
imagecopy($baseimage,$newlayer,0,0,0,0,$w,$h);
imagedestroy($newlayer); 

 

Any help?

Thanks

3dron

Link to comment
Share on other sites

Variables defined outside of functions are not available inside of functions.

If you didn't see PHP give you a warning about that then you probably do not have your environment set up properly for PHP development. Find your php.ini and set

display_errors = on
error_reporting = -1

Then restart PHP and/or your web server and try your script again.

Link to comment
Share on other sites

$baseimage, $w, and $h do not exist inside your function.  You need to pass them in as additional parameters.

function stacklayer($baseimage, $w, $h, $folder, $layer) {
    $newlayer = imagecreatefrompng($folder . '/' . $layer . '.png');
    imagecopy($baseimage,$newlayer,0,0,0,0,$w,$h);
    imagedestroy($newlayer); 
} 

Then include them when you call your function.

$baseimage = imagecreatefrompng("Base.png");
$w = imagesx($baseimage); $h = imagesy($baseimage);
stackLayer($baseimage, $w, $h, 'Layer1', 'Image1');

 

Edited by kicken
Link to comment
Share on other sites

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.

 Share

×
×
  • 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.