In the context of PHP web development, what are some common methods for handling user-uploaded images and ensuring they meet predefined criteria for display?

When handling user-uploaded images in PHP web development, it is important to validate the images to ensure they meet predefined criteria for display, such as file type, size, and dimensions. One common method is to use the GD library in PHP to check and manipulate the images before displaying them on the website.

// Example code to handle user-uploaded images and ensure they meet predefined criteria

// Define the allowed file types
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];

// Define the maximum file size in bytes
$maxSize = 1048576; // 1MB

// Define the maximum dimensions for the image
$maxWidth = 800;
$maxHeight = 600;

// Get the uploaded file
$uploadedFile = $_FILES['image'];

// Check if the file type is allowed
if (!in_array($uploadedFile['type'], $allowedTypes)) {
    die('Invalid file type. Only JPEG, PNG, and GIF files are allowed.');
}

// Check if the file size is within the limit
if ($uploadedFile['size'] > $maxSize) {
    die('File size exceeds the limit. Maximum file size allowed is 1MB.');
}

// Check the dimensions of the image
list($width, $height) = getimagesize($uploadedFile['tmp_name']);
if ($width > $maxWidth || $height > $maxHeight) {
    die('Image dimensions exceed the limit. Maximum dimensions allowed are 800x600.');
}

// Process and display the image
// Your code to display the image goes here