Posted By

brownrl on 02/11/11


Tagged

flash regular Expression filename


Versions (?)

Get Width and Height From a Filename


 / Published in: PHP
 

URL: http://www.itsgotto.be/cv.php

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 "someflashfile 300x250.swf" then this litte guy will help a lot.

  1. /**
  2. *
  3. * Function: getWH
  4. * Description: Return the width and height from a given string
  5. *
  6. * Param: $f a string that hopefully has ###x### some where in it
  7. *
  8. * Caveat: This will not work on every single file name ever conceived by man.
  9. * ex, something45x77x88x90x13.jpg not only is a stupid name for a file
  10. * but will foobar this function.
  11. *
  12. **/
  13. function getWH( $f )
  14. {
  15. // First we get the height by stripping off the numbers that follow an 'x'
  16. preg_match( "/^.*x([0-9]+).*$/" , strtolower( $f ) , $matches );
  17. $height = $matches[1];
  18.  
  19. // Next we reverse the string and do the same thing for the width
  20. $f = strrev( $f );
  21. preg_match( "/^.*x([0-9]+).*$/" , strtolower( $f ) , $matches );
  22. $width = strrev( $matches[1] ); // re-reverse
  23.  
  24. // Probably there exists a better regular expression to do it in one step.
  25. // Then again there is probably a regular expression that can cure world hunger
  26. // Maybe someday I will figure out both!
  27.  
  28. $dims[0] = $width;
  29. $dims[1] = $height;
  30.  
  31. return $dims; // return a simple array
  32. }

Report this snippet  

You need to login to post a comment.