Optimizing execution time of merging images I am using the below code to merge two images, but it take avg of 6-7 sec for creating 16 images. Is there any way I can speed up my process?

$image1=imagecreatefrompng('img/'.$value);
$image2=imagecreatefrompng('img/'.$value1);
$w=imagesx($image1);
$h=imagesy($image1);
imagecopy($image1,$image2,0,0,0,0,$w,$h);
imagejpeg($image1,'final/'.$key.$key1.'.jpg');
imagedestroy($image1);


I also get a black backgroud. I do not want to add an extra layer as it will increase execution time.

Below is basic detail

1: images of size: 1000 * 1000
2: png + png = jpg image
3: different image will combine i had already coded in way to avoid dupliacy


below i what i am trying to do like:

http://www.gemvara.com/jewelry/emerald-isle-ring/emerald-cut-diamond-14k-white-gold-ring-with-diamond/by53

here you can see new product is combination of stone and ring base.
and has 4 different view i am trying to do like that
The best way to gain speed is to keep the resulting images, and use them again if the same images are needed. This will eliminate the merging on the second run altogther.

Since you don't give any clue as to what end you're doing this, I cannot start to guess how useful my suggestion is...

Your question would be better if we knew:


What your images look like, how big are they? (You've added this in, thanks)
Where do they come from?
What do you use the images for? 


If you describe the situation in which you use your code, instead of just an isolated bit of code, then people can be much more creative in their answers. It is obvious there's not much we can improve in the plain and simple PHP code you show, so any solution would probably be found in the way you use the code.

Now that you've added some extra information to your question, it's easier to help you.

I assume, from what you say, that you're using the code only once, just after you've uploaded a new picture in your CMS, and you need to merge something into it. 

The answer can be pretty short: Computers don't have unlimited processing power, so complex operations, like this, will take a long time. 

There are some tips I can give:


Don't merge 16 times when you receive the image, but merge (once) when one on the 16 images is requested. This reduces the merger time to less than half a second per image.
Reduce the size of the images before merging. Images of 500 x 500 pixels will merge four times as fast as images of 1000 x 1000 pixels.
Use a background process to do the merging: https://florian.ec/articles/running-background-processes-in-php (use with care and only if there's really no other solution, which almost always does exists).