0

So i am using this wordpress function to get the users image

the_author_meta('author_image', the_author_ID() 

and it will either return something.jpg or something.png or something.gif if it finds an image otherwise it will return an integer like 2330. How would i do a preg_match or some conditional to let me know if an image is present. I was thinking of doing a preg_match to see if there is a period in the name but if someone has a better idea that would be great..

Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • It seems that you're storing two different things in the author_image usermeta. Perhaps that should be fixed? Unless that's on purpose. – Jeff Jan 27 '11 at 19:22

2 Answers2

5

Simpler:

if (is_numeric($author_image)){
  // this is presumably not a file
}
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

If all you want to do is check the extension of the file to see if it ends with something (ex. '.jpg', '.png', etc.) you can use the solution presented here:

startsWith() and endsWith() functions in PHP

I do not have familiarity with the library that you are using, but there really should be a better way to detect if the file is actually an image (some sort of meta data). Maybe reading the documentation will help?

EDIT: I misread the part about the function returning integers if an image is not found. The is_numeric() solution is probably enough, but I'll leave my answer up to give you options (for example, if you want to distinguish between image types).

Community
  • 1
  • 1
Kevin
  • 1,889
  • 14
  • 11