Last active
October 10, 2018 03:30
-
-
Save volfegan/c1641a5868a7fbcec4b233598cde3e44 to your computer and use it in GitHub Desktop.
Simple php function to parse an image file and find if it has that image's signature (magic number) according to its extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Author: Daniel L. Lacerda | |
// Created: 2018/10/05 | |
// Updated: - | |
// License: MIT | |
/* | |
* Reads the first bytes of the image file and checks its signature are true|false for that file extension | |
* | |
* @param actual $file | |
* @param string $fileExtension | |
* @return boolean | |
**/ | |
function check_img_signature($file, $fileExtension) { | |
$image_extensions = array("bmp"=>IMAGETYPE_BMP,"jpg"=>IMAGETYPE_JPEG,"jpeg"=>IMAGETYPE_JPEG,"png"=>IMAGETYPE_PNG,"gif"=>IMAGETYPE_GIF); | |
//file extension is not in the list | |
if ( !array_key_exists($fileExtension, $image_extensions) ) return false; | |
//http://php.net/manual/en/function.exif-imagetype.php | |
if (exif_imagetype($file) == $image_extensions[$fileExtension]) return true; | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment