How can the filename and file type be correctly specified to ensure proper image loading and manipulation in PHP?

When loading and manipulating images in PHP, it is important to correctly specify the filename and file type to ensure proper handling. This can be done by using functions like `imagecreatefromjpeg()`, `imagecreatefrompng()`, or `imagecreatefromgif()` depending on the file type. Additionally, you should validate the file type before processing it to prevent security vulnerabilities.

// Example of loading and manipulating a JPEG image
$filename = 'image.jpg';

// Check if the file is a valid JPEG image
if (exif_imagetype($filename) == IMAGETYPE_JPEG) {
    $image = imagecreatefromjpeg($filename);

    // Perform image manipulation here

    // Output or save the manipulated image
    imagejpeg($image, 'manipulated_image.jpg');

    // Free up memory
    imagedestroy($image);
} else {
    echo 'Invalid file type. Only JPEG images are allowed.';
}