Is it possible to check if imagecreatefromjpeg function worked and execute imagecreatefrompng if it didn't?

When using the imagecreatefromjpeg function in PHP to create an image resource from a JPEG file, it may fail if the file is not a valid JPEG image. To handle this situation, you can check if the imagecreatefromjpeg function returned false, indicating an error, and then execute the imagecreatefrompng function to create an image resource from a PNG file instead.

$jpeg_file = 'image.jpg';
$png_file = 'image.png';

$image = imagecreatefromjpeg($jpeg_file);

if ($image === false) {
    $image = imagecreatefrompng($png_file);
}

// Use the $image resource for further processing