Jump to content

Need help optimizing an image generation script


Prismatic

Recommended Posts

Ok, So we made this script to generate avatar images on the fly, but the problem is, due to the number of imagecopy() function calls, the script gets lagged upto about 10 seconds (thats how long it can take to run)

 

 

What the script does is take some inputs from the URL and build a greyscale base image, along with that we build a color overlay so different parts of the avatar can be colored and we run that through a function of ours which merges the two into one colored image.

 

 

The problem is that to get to the point where we can merge the two images, the greyscale base image, and the colored overlays, we need to combine multiple smaller images (we combine 6 greyscale images into one, and then upto 8 overlays into one, so we can merge them). To do this, we need to use imagecopy, the problem is, imagecopy, when used a lot, is very CPU heavy. Is there an alternative to imagecopy() with has the same result, but doesn't tax the CPU the same?

 

This is the part of the script that combines the greyscale images into one

 

	/* Combine all the base components (Greyscale Images) */
imagecopy($ChestBase, $ArmBase, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($ArmBase); 
imagecopy($ChestBase, $LeftArmPadBase, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($LeftArmPadBase); 
imagecopy($ChestBase, $RightArmPadBase, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($RightArmPadBase); 
imagecopy($ChestBase, $LegBase, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($LegBase); 
imagecopy($ChestBase, $HeadBase, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($HeadBase); 
imagecopy($ChestBase, $Weapon, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($Weapon); 


if($SpecialOverlay == true){
	imagecopy($ChestBase, $SpecBase, 0, 0, 0, 0, $sx, $sy);
}

 

 

And now this is where we combine the overlays into one (this is directly under the above code)

 

	/* Now we combine all the colored overlays into one */
if($Layers == true){
	imagecopy($HeadMask, $HeadMaskArmor, 0, 0, 0, 0, $sx, $sy);
}
imagecopy($HeadMask, $ChestMask, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($ChestMask); 
imagecopy($HeadMask, $ArmOverlay, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($ArmOverlay); 
imagecopy($HeadMask, $LeftArmPadMask, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($LeftArmPadMask); 
imagecopy($HeadMask, $ArmOverlay_Inlet, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($ArmOverlay_Inlet); 
imagecopy($HeadMask, $RightArmPadMask, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($RightArmPadMask); 
imagecopy($HeadMask, $LegOverlay, 0, 0, 0, 0, $sx, $sy);
imagedestroy ($LegOverlay); 
if($SpecialOverlay == true){
	imagecopy($HeadMask, $SpecMask, 0, 0, 0, 0, $sx, $sy);
	imagedestroy ($SpecMask); 
}

 

This gives us two images, one is our avatar, all greyscale, and the other is the overlays, which, when pumped through our function, combine with the greyscale image and give us the colored result. The slowness comes from those imagecopy's, it's just too much, even for our dedicated server (P4 HT 3.0Ghz, 1GB ram).

 

Can anyone help me condense those?

 

 

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.