Page 1 of 1

getimagesize()

Posted: Thu Jun 15, 2006 6:05 pm
by Qaid
Hi,

I wanted to output the actual imagesize of the image.

An example: image on the server http://example.com/test.jpg will output 33,5 kb.

I had a look at http://dk2.php.net/manual/da/function.getimagesize.php but can't find the right solution. The url will only output width height bits channels... but not the size..

Thank you..

Posted: Thu Jun 15, 2006 9:32 pm
by bimbo
u can use the function getimagesize($file)

Posted: Thu Jun 15, 2006 9:54 pm
by printf
local file....

filesize();


remote file...

fsockopen(), read header Content Length <= no always given by server
fsockopen(), fread() complete file, strlen() file
fopen(), fread() complete file, strlen() file
file_get_contents(), strlen() file

A quick function...

Code: Select all

<?


function normalize_size ( $file, $type )
{
	$text = array ( ' Bytes (B)', ' Kilobytes (KB)', ' Megabytes (MB)' );

	if ( $type == 1 )
	{
		$temp = filesize ( $file );
	}
	else
	{
		$temp = strlen ( file_get_contents ( $file ) );
	}

	$i = 0;

	while ( $i <= 2 )
	{
		if ( $temp > 1024 ) 
		{
			$i++;

			$temp = ( $temp / 1024 );
		}
		else
		{
			break;
		}
	}

	return ( number_format ( $temp, 0, '', '.') . $text[$i] );
}

// usage

// type of file (1 = local, 2 = remote)

$type = 1;

// the local file

$file = './a.png';

// show the result

echo 'Local File: ' . $file . ', Size: ' . normalize_size ( $file, 1 );

echo '<br />';

// type of file (1 = local, 2 = remote)

$type = 2;

// the remote file

$file = 'http://www.google.com/intl/en/images/logo.gif';

// show the result

echo 'Remote File: ' . $file . ', Size: ' . normalize_size ( $file, 2 );

?>

Posted: Fri Jun 16, 2006 9:11 am
by Qaid
Thank you very much :D