How can PHP be used to handle image uploads and resizing efficiently in a web application?

To handle image uploads and resizing efficiently in a web application using PHP, you can use the GD library which provides functions for image manipulation. You can upload the image using a form, validate it, resize it to a desired dimension, and save it to a specified location on the server.

// Check if the file was uploaded
if(isset($_FILES['image'])){
    $file = $_FILES['image'];
    
    // Validate file type
    $allowedTypes = ['image/jpeg', 'image/png'];
    if(!in_array($file['type'], $allowedTypes)){
        die('Invalid file type. Only JPEG and PNG files are allowed.');
    }
    
    // Resize image
    $image = imagecreatefromstring(file_get_contents($file['tmp_name']));
    $resizedImage = imagescale($image, 200, 200);
    
    // Save resized image
    imagejpeg($resizedImage, 'uploads/resized_image.jpg');
    
    // Free up memory
    imagedestroy($image);
    imagedestroy($resizedImage);
}