0

I'm using the following code and an open service provided by wordpress to grab a screenshot thumbnail of a number of webpages on the fly

<img alt="<?php the_title(); ?>" src="http://s.wordpress.com/mshots/v1/<?php echo urlencode( get_post_meta(get_the_ID(), 'mjwlink-url', true )); ?>?w=300">

Problem is some of the links go to PDF, DOC or XLS files, in those cases I'd like to display a single alternative image.

I have absolutely no idea how to go about using the url in this way + given the fact I'm using urlencode I'm not sure it's even possible - any tips/advice/code appreciated.

Example outputs: http://s.wordpress.com/mshots/v1/http%3A%2F%2Fwww.reform.co.uk%2Fportals%2F0%2Fdocuments%2Fitcanbedonesingle.pdf?w=300

http://s.wordpress.com/mshots/v1/http%3A%2F%2Fwww.outoftrouble.org.uk%2F?w=300

toomanyairmiles
  • 6,465
  • 8
  • 43
  • 71

2 Answers2

0

The thing to do would be to check the file type, even by simply checking what comes after the dot.

You can check this before your statement like this:

$types = array('.pdf', '.doc', '.xls');
if(0 < count(array_intersect(array_map('strtolower', $filename, $types)))) {
  //go get the image
} else {
  //do whatever else you want to
}

where $types can include any types that you want to process differently, and $filename is the name of the file, obviously.

Taken from here, but slightly modified in your case.

Community
  • 1
  • 1
Jon Egeland
  • 12,470
  • 8
  • 47
  • 62
  • is this throwing an error? SO said you commented on this, but I don't see a comment here. Can you re-post it if you did? – Jon Egeland Dec 11 '11 at 02:11
0
$types = array('pdf', 'doc', 'xls');
$path_parts = pathinfo($filename);
if(!in_array($path_parts['extension'], $types)) {
  //go get the image
} else {
  //do whatever else you want to
}
Michael Mior
  • 28,107
  • 9
  • 89
  • 113