1
\$\begingroup\$

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

\$\endgroup\$
6
  • \$\begingroup\$ It seems odd to create a JPEG out of PNG inputs. The two image formats are suited to different kinds of images. \$\endgroup\$ Dec 6, 2014 at 8:43
  • \$\begingroup\$ Why on earth would you need 16 images of 1000 x 1000 pixels all at once? There's no monitor that can display that. \$\endgroup\$ Dec 6, 2014 at 12:40
  • \$\begingroup\$ no need to make images in differnt view or angle \$\endgroup\$
    – mydeve
    Dec 6, 2014 at 12:42
  • \$\begingroup\$ Well, I refer back to my answer: Clearly you're using these images in a very specific situation, but you're not telling us. We cannot help you if you don't give us the whole story. Moreso, telling us what you're trying to do might make your question far more interesting. \$\endgroup\$ Dec 6, 2014 at 12:51
  • \$\begingroup\$ Thanks for the example link. Those images are retrieved from the server only when needed, through AJAX calls, and nothing is merged, as far as I can see. \$\endgroup\$ Dec 6, 2014 at 13:05

1 Answer 1

2
\$\begingroup\$

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).
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.