What are the best practices for handling images in PHP, especially in relation to arrays?

When handling images in PHP, especially in relation to arrays, it is important to properly validate and sanitize user input to prevent security vulnerabilities such as injection attacks. Additionally, it is recommended to store images in a secure directory outside of the web root to prevent direct access. Lastly, using functions like `imagecreatefromjpeg()` and `imagejpeg()` can help manipulate and output images effectively.

// Example of validating and sanitizing image upload
if(isset($_FILES['image'])){
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
        echo "Image uploaded successfully.";
    } else {
        echo "File is not an image.";
    }
}