What are the potential security risks of using GD library to add watermarks to images in PHP?

One potential security risk of using the GD library to add watermarks to images in PHP is the possibility of allowing arbitrary file uploads, which can lead to remote code execution. To mitigate this risk, it is important to validate and sanitize user input before processing it with the GD library.

// Validate and sanitize the user input for the image file
if(isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
    $image = $_FILES['image']['tmp_name'];
    
    // Validate and sanitize other input variables as needed
    
    // Process the image with the GD library
    // Add watermark to the image
}