Get Width and Height From a Filename


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.