How can users be allowed to save images with custom filenames in PHP when using the GD Image Library?

When using the GD Image Library in PHP to save images, by default, the library generates a filename for the saved image. To allow users to save images with custom filenames, you can modify the code to accept a custom filename input from the user and use that filename when saving the image.

// Get the custom filename input from the user
$customFilename = $_POST['custom_filename'];

// Save the image with the custom filename
imagepng($image, 'path/to/save/directory/' . $customFilename . '.png');

// Free up memory
imagedestroy($image);