codice:
<?php
class immagineInfo
{
var $_width;
var $_height;
var $_path;
var $_size;
var $_formato;
function immagineInfo($path)
{
$this->_width=0;
$this->_height=0;
$this->_formato="";
$this->_path=$path;
$this->_size=filesize($path);
$this->setInfo();
}
//interfaccia-------------------------------------------------------------------
function getWidth()
{
return $this->_width;
}
function getHeight()
{
return $this->_height;
}
function getSize()
{
return $this->_size;
}
function getFormato()
{
return $this->_formato;
}
//------------------------------------------------------------------------------
//Ricava dai file ASCII i bytes da leggere per le dimensioni
function readBytes($offset, $numBytes)
{
$fd=fopen($this->_path, "r") or die ("Impossibile aprire il file $directory/$entryDir/$entryFile");
if($offset > 0)
{
$buffer = fread($fd, $offset-1);
//print "
(Debug)Buffer dentro readBytes:".$buffer;
}
if($numBytes== -1)
return fread($fd, $this->_size);
else
return fread($fd, $numBytes);
}
//Converte valori esadecimali in numeri interi decimali
function decConvert($str, $mode)
{
if($mode==1) //Dall'inizio //Dalla fine
return (integer)(ord(substr($str,0,1))+ (ord(substr($str,-1))*256));
else if($mode==2) //Dalla fine //Dall'inizio
return (integer)(ord(substr($str,-1))+ (ord(substr($str,0,1))*256));
}
//Setta le dimensioni di altezza e larghezza del file grafico
function setInfo()
{
$path=strtoupper($this->_path);
$formato=substr($path,-3);
$this->_formato=$formato;
switch($formato)
{
case "GIF":
$this->_width= $this->decConvert($this->readBytes(7, 2), 1);
$this->_height= $this->decConvert($this->readBytes(9, 2), 1);
break;
case "BMP":
$this->_width=$this->decConvert($this->readBytes(19, 2), 1);
$this->_height=$this->decConvert($this->readBytes(23, 2), 1);
break;
case "PNG":
$this->_width=$this->decConvert($this->readBytes(19, 2), 2);
$this->_height=$this->decConvert($this->readBytes(23, 2), 2);
break;
case "JPG":
$buffer=$this->readBytes(0, -1);
//print "
(debug)Buffer:".$buffer;
$fileSize = strlen($buffer);
$target = chr(255).chr(216).chr(255);
$targetFound = (integer)strpos($buffer, $target)+1;
if($targetFound>0)
{
$pos = $targetFound + 2;
$endLoop = False;
while($endLoop == False && $pos < $this->_size )
{
while(ord(substr($buffer,$pos,1))==255 and $pos < $this->_size)
{
$pos=$pos+1;
}
if(ord(substr($buffer,$pos,1))<192 or ord(substr($buffer,$pos,1))>195)
{
$markerSize = $this->decConvert(substr($buffer,$pos+1,2),2);
$pos = $pos + $markerSize + 1;
}
else
$endLoop = True;
}
}
$this->_width=$this->decConvert(substr($buffer, $pos + 6, 2), 2);
$this->_height=$this->decConvert(substr($buffer, $pos + 4, 2), 2);
break;
}
}
}
//Uso della classe--------------------------------------------------------------
$immagineInfo[0]= new immagineInfo("prova.gif");
$immagineInfo[1]= new immagineInfo("prova.bmp");
$immagineInfo[2]= new immagineInfo("prova.png");
$immagineInfo[3]= new immagineInfo("prova.jpg");
for($i=0; $i< sizeof($immagineInfo);$i=$i+1)
{
print "
ImmagineInfo".$i.": ";
print $immagineInfo[$i]->getWidth();
print "x";
print $immagineInfo[$i]->getHeight();
print "\t";
print $immagineInfo[$i]->getSize();
print "bytes\t";
print $immagineInfo[$i]->getFormato();
print "
";
}
?>
ora, l'uso della classe in se mi è chiaro, (è ben spiegato in fondo al file) i miei dubbi sono più su alcuni simboli/definizioni usate nel codice che non riesco a capire, e non ho trovato le spiegazioni nemmeno nelle guide php sulla sezione guide di html.it. probabilmente sono banalissime, ma non riesco a capirle senza un aiuto