March 3, 2012

Check image type if renamed from .png to .jpg

Question by John

If it possible to check if the type of image if it was renamed from .png to .jpg?

I need this because when I use a resize function my website stop working if the uploaded image was renamed from .png to .jpg (I made my code to accept only .jpg images)

Answer by dev-null-dweller

getimagesize will contain info about image type:

$info = getimagesize('file.png');
if($info[2] == IMAGETYPE_JPEG){

}

Answer by Starx

You can always read the image type to check an image using exif_imagetype()

$image_type = exif_imagetype($filename);

Example:

<?php
if (exif_imagetype('image.gif') != IMAGETYPE_GIF) {
    echo 'The picture is not a gif';
}
?>

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!