Jump to content

[SOLVED] merging multiple images with imagecopy.. not quite working


Dragen

Recommended Posts

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:

im2.gif

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:

im.gif

 

can anyone see where I'm going wrong here?

Thanks

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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