getimagesize()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

getimagesize()

Post 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..
bimbo
Forum Newbie
Posts: 3
Joined: Tue Apr 25, 2006 3:36 am

Post by bimbo »

u can use the function getimagesize($file)
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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 );

?>
Qaid
Forum Commoner
Posts: 33
Joined: Wed Feb 15, 2006 12:04 pm

Post by Qaid »

Thank you very much :D
Post Reply