What are the limitations of using the imagepng function in PHP for displaying images on a website?

The imagepng function in PHP is limited in that it only supports PNG image format. To display images of other formats such as JPEG or GIF, you would need to use additional functions like imagejpeg or imagegif. To overcome this limitation, you can check the file extension of the image and use the appropriate function based on the file type.

<?php
$image = imagecreatefromjpeg('image.jpg');
$extension = pathinfo('image.jpg', PATHINFO_EXTENSION);

if($extension == 'jpg' || $extension == 'jpeg'){
    header('Content-Type: image/jpeg');
    imagejpeg($image);
} elseif($extension == 'png'){
    header('Content-Type: image/png');
    imagepng($image);
} elseif($extension == 'gif'){
    header('Content-Type: image/gif');
    imagegif($image);
}

imagedestroy($image);
?>