What are some best practices for optimizing GIF animations created with the GD Lib in PHP?

When optimizing GIF animations created with the GD Lib in PHP, it is important to reduce the number of frames, limit the color palette, and decrease the image size to improve loading times and reduce file size.

// Example code for optimizing GIF animations with GD Lib in PHP

// Reduce the number of frames
$framesToRemove = 3; // Set the number of frames to remove
for ($i = 0; $i < $framesToRemove; $i++) {
    imagegif($image, 'optimized.gif', 100); // Save the optimized GIF
}

// Limit the color palette
$colors = 64; // Set the number of colors in the palette
imagetruecolortopalette($image, true, $colors);

// Decrease the image size
$newWidth = imagesx($image) / 2; // Set the new width
$newHeight = imagesy($image) / 2; // Set the new height
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image));
imagegif($newImage, 'optimized.gif', 100); // Save the optimized GIF