classes

Comments and questions that doest not fit anywhere else.
Locked
Guest

classes

Post by Guest » Wed Jan 07, 2004 8:02 pm

in your new version you usses classes, but i got a question about your library:

you access them with __CLASS__::__FUNCTION__ and you create not a class in your 'basic' / 'start' class.

So my question is: is the cost of memory putting the functions in a variable so high, it is better to access it with __CLASS__::__FUNCTION__ then putting library in your 'basic_class'->library and accessing you library with $this->library->__FUNCTION__ and in library itself calling a dirrerent libary $this->__FUNCTION__.

OR do i sound just stupid ?

Allan Hansen
getID3() v2 developer
Posts: 445
Joined: Sun May 04, 2003 2:22 pm
Location: Holmegaard, Denmark

Post by Allan Hansen » Thu Jan 08, 2004 2:24 pm

You are a little unclear, but I guess you refer to the getid3_lib class. The functions are accessed from multiple modules, so it is logical to bundle them in a library class. I do not think it takes more memory moving the function to the main getid3 class, but it would be a mess. And that is actually one of the important things about 1.7 - everything is wrapped in classes to contain the mess.

Guest

what i mean

Post by Guest » Thu Jan 08, 2004 5:44 pm

well i mean something like this:
the nice part is that you can access all data by a single varriable....

[php]<?php
class getID3
{
function getID3()
{
// define paths ...

$this->library = new getid3_lib ( );
}

function analyze( $filepath)
{
// if jpeg file found:

$class = new getid3_jpg ($this);

return $this->info;
}
}


class getid3_lib
{
function example_function ( $imgData, $parent="" ) {
$height = '';
$width = '';
$type = '';
if ((substr($imgData, 0, 3) == GETID3_GIF_SIG) && (strlen($imgData) > 10)) {
$dim = unpack('v2dim', substr($imgData, 6, 4));
$width = $dim['dim1'];
$height = $dim['dim2'];
$type = 1;
} elseif ((substr($imgData, 0, 8) == GETID3_PNG_SIG) && (strlen($imgData) > 24)) {
$dim = unpack('N2dim', substr($imgData, 16, 8));
$width = $dim['dim1'];
$height = $dim['dim2'];
$type = 3;
} elseif ((substr($imgData, 0, 3) == GETID3_JPG_SIG) && (strlen($imgData) > 4)) {
///////////////// JPG CHUNK SCAN ////////////////////
$imgPos = 2;
$type = 2;
$buffer = strlen($imgData) - 2;
while ($imgPos < strlen($imgData)) {
// synchronize to the marker 0xFF
$imgPos = strpos($imgData, 0xFF, $imgPos) + 1;
$marker = $imgData[$imgPos];
do {
$marker = ord($imgData[$imgPos++]);
} while ($marker == 255);
// find dimensions of block
switch (chr($marker)) {
// Grab width/height from SOF segment (these are acceptable chunk types)
case GETID3_JPG_SOF0:
case GETID3_JPG_SOF1:
case GETID3_JPG_SOF2:
case GETID3_JPG_SOF3:
case GETID3_JPG_SOF5:
case GETID3_JPG_SOF6:
case GETID3_JPG_SOF7:
case GETID3_JPG_SOF9:
case GETID3_JPG_SOF10:
case GETID3_JPG_SOF11:
case GETID3_JPG_SOF13:
case GETID3_JPG_SOF14:
case GETID3_JPG_SOF15:
$dim = unpack('n2dim', substr($imgData, $imgPos + 3, 4));
$height = $dim['dim1'];
$width = $dim['dim2'];
break 2; // found it so exit
case GETID3_JPG_EOI:
case GETID3_JPG_SOS:
return false; // End loop in case we find one of these markers
default: // We're not interested in other markers
$skiplen = (ord($imgData[$imgPos++]) << 8) + ord($imgData[$imgPos++]) - 2;
// if the skip is more than what we've read in, read more
$buffer -= $skiplen;
if ($buffer < 512) { // if the buffer of data is too low, read more file.

static $valid_parent = false;

if ( ! $valid_parent )
{
if ( empty ( $parent ) )
{
die ( "Need parent class for extra info." );
}

if ( strpos ( get_class ( $parent ), "id3" ) === false )
{
die ( "Need a valid class for extra info." );
}

if ( ! isset ( $parent->file['fd'] ) || ! is_resource ( $parent->file['fd'] ) ) {
$parent->info['errors'][] = "Unable to locate the filepointer";
return false;
} $valid_parent = true;
}

$imgData .= fread( $parent->file['fd'],$skiplen+1024 );
$buffer += $skiplen + 1024;
}
$imgPos += $skiplen;
break;
} // endswitch check marker type
} // endif loop through JPG chunks
} // endif chk for valid file types

return array($width, $height, $type);
} // end function

function GetDataImageSize($imgData) {
$height = '';
$width = '';
$type = '';
if ((substr($imgData, 0, 3) == GETID3_GIF_SIG) && (strlen($imgData) > 10)) {
$dim = unpack('v2dim', substr($imgData, 6, 4));
$width = $dim['dim1'];
$height = $dim['dim2'];
$type = 1;
} elseif ((substr($imgData, 0, 8) == GETID3_PNG_SIG) && (strlen($imgData) > 24)) {
$dim = unpack('N2dim', substr($imgData, 16, 8));
$width = $dim['dim1'];
$height = $dim['dim2'];
$type = 3;
} elseif ((substr($imgData, 0, 3) == GETID3_JPG_SIG) && (strlen($imgData) > 4)) {
///////////////// JPG CHUNK SCAN ////////////////////
$imgPos = 2;
$type = 2;
$buffer = strlen($imgData) - 2;
while ($imgPos < strlen($imgData)) {
// synchronize to the marker 0xFF
$imgPos = strpos($imgData, 0xFF, $imgPos) + 1;
$marker = $imgData[$imgPos];
do {
$marker = ord($imgData[$imgPos++]);
} while ($marker == 255);
// find dimensions of block
switch (chr($marker)) {
// Grab width/height from SOF segment (these are acceptable chunk types)
case GETID3_JPG_SOF0:
case GETID3_JPG_SOF1:
case GETID3_JPG_SOF2:
case GETID3_JPG_SOF3:
case GETID3_JPG_SOF5:
case GETID3_JPG_SOF6:
case GETID3_JPG_SOF7:
case GETID3_JPG_SOF9:
case GETID3_JPG_SOF10:
case GETID3_JPG_SOF11:
case GETID3_JPG_SOF13:
case GETID3_JPG_SOF14:
case GETID3_JPG_SOF15:
$dim = unpack('n2dim', substr($imgData, $imgPos + 3, 4));
$height = $dim['dim1'];
$width = $dim['dim2'];
break 2; // found it so exit
case GETID3_JPG_EOI:
case GETID3_JPG_SOS:
return false; // End loop in case we find one of these markers
default: // We're not interested in other markers
$skiplen = (ord($imgData[$imgPos++]) << 8) + ord($imgData[$imgPos++]) - 2;
// if the skip is more than what we've read in, read more
$buffer -= $skiplen;
if ($buffer < 512) { // if the buffer of data is too low, read more file.
// $imgData .= fread( $fd,$skiplen+1024 );
// $buffer += $skiplen + 1024;
return false; // End loop in case we find run out of data
}
$imgPos += $skiplen;
break;
} // endswitch check marker type
} // endif loop through JPG chunks
} // endif chk for valid file types

return array($width, $height, $type);
} // end function
}

class getid3_jpg
{

function getid3_jpg( &$parent) {

$this->info =& $parent->info;
$this->library =& $parent->library;
$this->file =& $parent->file;

$this->info['fileformat'] = 'jpg';
$this->info['video']['dataformat'] = 'jpg';
$this->info['video']['lossless'] = false;
$this->info['video']['bits_per_sample'] = 24;
$this->info['video']['pixel_aspect_ratio'] = (float) 1;

fseek($this->file['fp'], $this->file['avdataoffset'], SEEK_SET);

list($width, $height, $type) = $this->library->GetDataImageSize(fread($this->file['fp'], $this->file['size']));
list($width, $height, $type) = $this->library->example_function(fread($this->file['fp'], $this->file['size']));
list($width, $height, $type) = $this->library->example_function(fread($this->file['fp'], GETID3_FREAD_BUFFER_SIZE), $parent);
}

}
?>[/php]

Locked