Dieses Script validiert JPEG Dateien
<?php
/*
JPEG Validator
Aktion: PHP Scripte für die armen dieser Welt
Der Erlös geht für mein Pausenbrot drauf
Copyright (c) 2004 by Phillip 'Firebird' Berndt
*/
function chrToVal($text)
{
$hexCode = '';
for($i=0;$i<strlen($text);$i++)
$hexCode .= dechex(ord($text[$i]));
return hexdec($hexCode);
}
function validateJpeg($text)
{
$jpgLen = chrToVal(substr($text, 0, 2));
$jpgIdentify = substr($text, 5, 5);
if(ord($jpgIdentify[0]) != 0x10 || substr($jpgIdentify, 1) != 'JFIF')
return false;
$jpgVersion = substr($text, 11, 2);
$jpgVersion = chrToVal($jpgVersion[0]).'.'.chrToVal($jpgVersion[1]);
if($jpgVersion != '1.2')
return false;
$pixelFormat = chrToVal(substr($text, 13, 1));
$xDensity = chrToVal(substr($text, 14, 2));
$yDensity = chrToVal(substr($text, 16, 2));
if($xDensity != $yDensity) return false;
$xThumb = chrToVal(substr($text, 18, 1));
$yThumb = chrToVal(substr($text, 19, 1));
if(($xThumb == 0 && $yThumb != 0) || ($xThumb != 0 && $yThumb == 0))
return false;
return true;
}
$jpegContent = implode('', file('Beispiel.jpg'));
$isValid = validateJpeg($jpegContent) ? 'valid' : 'invalid';
echo('This picture is '.$isValid);
?>