note 68664 deleted from ref.image by nlopess
| From: | nlopess@php.net | Date: | Fri, 11 Aug 2006 22:48:31 +0000 |
| Subject: | note 68664 deleted from ref.image by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-115813@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at robert-beran dot de
----
Here my function to remove JPEG Metadata (german comments):
<?php
//Diese Funktion dient dazu nicht benötigten Code aus JPEG Bildern zu filtern
function jpg_clean( $filename, $destination )
{
//Zuerst handle erstellen um $filename binär auszulesen
$handle = fopen ($filename, "rb");
//AnschlieÃend immer die Segmente mit der gröÃe auslesen
$segment[] = fread ($handle, 2);
//Wenn die ersten beiden Bytes nicht 0xFF 0xD8 entsprechen - abbruch!
if($segment[0] === "\xFF\xD8")
{
//Jetzt schauen ob neues Segment 0xFF entspricht - wenn nicht abbruch
$segment[] = fread ($handle, 1);
//Wenn Segment vorhanden - fahre fort!
if($segment[1] === "\xFF")
{
//Dateizeiger an den Anfang setzen
rewind ($handle);
//Nun wird die ganze Datei durchsucht
while( ! feof( $handle ) )
{
$daten = fread( $handle, 2 );
//Prüfe auf spezielle Segmente - falls diese vorhanden sind -> Zeiger neu setzen
if( (preg_match("/FFE[1-9a-zA-Z]{1,1}/i",bin2hex($daten))) || ($daten ===
"\xFF\xFE") )
{
//Position des Dateizeigers
$position = ftell($handle);
//GröÃe des Segments auslesen
$size = fread( $handle, 2 );
//GröÃe ausrechnen
$newsize = 256*hexdec(bin2hex($size{0}))+hexdec(bin2hex($size{1}));
//Hier nun neue Position bestimmen -> Position hinter dieser Zone
$newpos = $position + $newsize;
//Dateiziger setzen
fseek ($handle, $newpos);
}
else
{
$newfile[] = $daten;
}
}
//Hier File Handle schlieÃen
fclose ($handle);
//Wenn Schleife durch ist haben wir newfile als Array
$newfile = implode('',$newfile);
//Und schreiben dies nun in Angegebenen destination
$handle = fopen($destination, "wb");
fwrite($handle, $newfile);
fclose($handle);
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
?>
The function will read the JPG file ($filename) and will copy the cleaned JPG file to
($destination).
It might be more helpful to make a function which checks all the segments - thats more secure i
think ;)
But its good to get rid off script code infiltration and stuff like that in jpg images.
if there are any questions - ask me - thx!
greetz
robert