Return to Snippet

Revision: 41086
at February 11, 2011 19:56 by brownrl

Initial Code
*  Function: getWH
*  Description: Return the width and height from a given string
*  Param:  $f a string that hopefully has ###x### some where in it
*  Caveat: This will not work on every single file name ever conceived by man.
*          ex, something45x77x88x90x13.jpg not only is a stupid name for a file
*              but will foobar this function.
function getWH( $f )
        // First we get the height by stripping off the numbers that follow an 'x'
	preg_match( "/^.*x([0-9]+).*$/" , strtolower( $f ) , $matches );
	$height = $matches[1];

        // Next we reverse the string and do the same thing for the width	
	$f = strrev( $f );
	preg_match( "/^.*x([0-9]+).*$/" , strtolower( $f ) , $matches );
	$width = strrev( $matches[1] ); // re-reverse

        // Probably there exists a better regular expression to do it in one step.
        // Then again there is probably a regular expression that can cure world hunger
        // Maybe someday I will figure out both!
	$dims[0] = $width;
	$dims[1] = $height;
	return $dims; // return a simple array

Initial URL

Initial Description
I use this little guy to get the width and height from a filename. Often when I need to show a flash swf file I don't have an easy way to tell the width and the height of the file for display. However is the file name is "some_flash_file 300x250.swf" then this litte guy will help a lot.

Initial Title
Get Width and Height From a Filename

Initial Tags

Initial Language