3dron Posted March 26, 2022 Share Posted March 26, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/314628-in-a-function-how-do-i-keep-calling-imagecopy-to-stack-image-layers-on-the-newly-created-image-each-time/ Share on other sites More sharing options...
requinix Posted March 26, 2022 Share Posted March 26, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314628-in-a-function-how-do-i-keep-calling-imagecopy-to-stack-image-layers-on-the-newly-created-image-each-time/#findComment-1594595 Share on other sites More sharing options...
kicken Posted March 26, 2022 Share Posted March 26, 2022 (edited) $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 March 26, 2022 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/314628-in-a-function-how-do-i-keep-calling-imagecopy-to-stack-image-layers-on-the-newly-created-image-each-time/#findComment-1594596 Share on other sites More sharing options...
3dron Posted March 30, 2022 Author Share Posted March 30, 2022 Thanks all works now. Quote Link to comment https://forums.phpfreaks.com/topic/314628-in-a-function-how-do-i-keep-calling-imagecopy-to-stack-image-layers-on-the-newly-created-image-each-time/#findComment-1594731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.