Are there any best practices for efficiently combining images using PHP?

When combining images using PHP, it is important to use functions that are optimized for performance to ensure efficiency. One common approach is to use the GD library, which provides functions for image manipulation. Additionally, caching the processed images can help improve performance by reducing the need to repeatedly process the same images.

// Load the main image
$mainImage = imagecreatefromjpeg('main.jpg');

// Load the overlay image
$overlayImage = imagecreatefrompng('overlay.png');

// Get the dimensions of the overlay image
$overlayWidth = imagesx($overlayImage);
$overlayHeight = imagesy($overlayImage);

// Combine the overlay image onto the main image
imagecopy($mainImage, $overlayImage, 0, 0, 0, 0, $overlayWidth, $overlayHeight);

// Output the final image
header('Content-Type: image/jpeg');
imagejpeg($mainImage);

// Clean up
imagedestroy($mainImage);
imagedestroy($overlayImage);