What best practices should be followed when creating a form in PHP to allow users to convert WebP images to JPG or PNG?

When creating a form in PHP to allow users to convert WebP images to JPG or PNG, it is important to validate the uploaded file to ensure it is a valid WebP image. This can be done by checking the MIME type of the file. Once validated, you can use PHP's image processing functions to convert the WebP image to JPG or PNG as per the user's selection.

<?php
if(isset($_POST['submit'])){
    $webpFile = $_FILES['webp_image']['tmp_name'];
    $outputFormat = $_POST['output_format'];
    
    if($outputFormat == 'jpg'){
        $outputFile = 'converted_image.jpg';
        $image = imagecreatefromwebp($webpFile);
        imagejpeg($image, $outputFile, 100);
    } elseif($outputFormat == 'png'){
        $outputFile = 'converted_image.png';
        $image = imagecreatefromwebp($webpFile);
        imagepng($image, $outputFile, 9);
    } else {
        echo 'Invalid output format selected';
    }
}
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="webp_image">
    <select name="output_format">
        <option value="jpg">JPG</option>
        <option value="png">PNG</option>
    </select>
    <input type="submit" name="submit" value="Convert">
</form>