Dragen Posted September 10, 2007 Share Posted September 10, 2007 Hi, I've been working on some code for a bit and just can't figure out my problem. My code runs through a series of variables and creates a seperate image for each, then merges them together. Here's my code: <?php for($i = 0; $i < count($this->value); $i++){ if(function_exists('imagecreatetruecolor')){ $this->image = imagecreatetruecolor($this->width,$this->height); }elseif(function_exists('imagecreate')){ $this->image = imagecreate($this->width,$this->height); }else{ die('Error: Failed to create image'); } // Fill it $this->grad_fill($this->image,$this->direction,$this->startcolor,$this->endcolor); //this just creates the background colours $this->border('#000000', '#000000', '#000000', '#000000'); //this does a border $this->text_enter($this->value[$i]); // enters the text on the image $this->merge($this->fimage, $this->image, $i); //here I merge the small image onto a larger image with a width 6 times larger than the small $this->image imagedestroy($this->image); } ?> Here's the code for $this->merge: <?php function merge($d_img, $s_img, $k){ imagecopy($d_img, $s_img, 0, 0, $this->dest['x'][$k], $this->dest['y'], $this->fwidth, $this->height); } ?> It should create an image with six sections to it (one section for each run through the 'for' statement), which the position of the smaller image on the larger image changing each time to create something like: The problem is, when I output the image it is only displaying the last section. The rest are blank, instead of containing the correct image, like this: can anyone see where I'm going wrong here? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/ Share on other sites More sharing options...
Dragen Posted September 10, 2007 Author Share Posted September 10, 2007 does anyone have any ideas? If my explanation isn't clear enough please say! Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-345429 Share on other sites More sharing options...
Dragen Posted September 11, 2007 Author Share Posted September 11, 2007 can anyone help? I'm just trying to copy several images together on another image (all created in php and not stored on the server). I thought if I create the first image (the one I want to place the others on), then create the other images and add them on one by one with a 'for' statement it should work.. Instead I'm only getting the last image added onto the first. Can anyone see a problem with what I'm doing? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346256 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 It's probably putting them on top of each other, instead of spacing them out. You need to tell it where to place each small image on the large one. Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346261 Share on other sites More sharing options...
Dragen Posted September 11, 2007 Author Share Posted September 11, 2007 I'm doing that. I've got the x and y positions set up in an array. It then skips through the array. on my image copy I've got this: imagecopy($d_img, $s_img, 0, 0, $this->dest['x'][$k], $this->dest['y'], $this->fwidth, $this->height); $this->dest['x'][$k] $k is incremented by one each time, so it access the next value from the array. My array looks like this: $this->dest = array('x' => array('0', '-16', '-31', '-46', '-61', '-76'), 'y' => '0'); And all of the x positions are accurate, because I've tested them seperatly... The images should all be put into their own places, but it seems that each time I copy a new image to the original it ignores the last one. Do I need to 'save' the image after I've used imagecopy, before I can place another image on it? Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346267 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 Why aren't the x and y values positive? is $d_img the large image? You need to put each of the little ones onto the same large destination image. Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346274 Share on other sites More sharing options...
Dragen Posted September 11, 2007 Author Share Posted September 11, 2007 To be honest I've got no idea why they're negative. For some reason if I have them as positive it goes the wrong way.. the way I'd expected it to have gone if it was negative. Bit weird, but they do place the image in the right positions... yes, $d_img is the large image. The smaller ones are all being placed onto it when I use imagecopy. imagecopy($d_img, $s_img, 0, 0, $this->dest['x'][$k], $this->dest['y'], $this->fwidth, $this->height); $d_img is always set as $this->fimage, when I call my merge function here: $this->merge($this->fimage, $this->image, $i); I'm quite sure that I'm doing that part right... It's the first time I've properly tried using imagecopy. Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346289 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 You're putting them all at 0, 0 on the destination. http://php.net/imagecopy Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346290 Share on other sites More sharing options...
Dragen Posted September 11, 2007 Author Share Posted September 11, 2007 haha! Thanks... I feel silly now also explains why my x positioning was all in negative. Thanks ever so much!! Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346294 Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 no problem. Quote Link to comment https://forums.phpfreaks.com/topic/68684-solved-merging-multiple-images-with-imagecopy-not-quite-working/#findComment-346296 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.