public static function ShareaholicPublic::post_first_image in Share Buttons, Related Posts, Content Analytics - Shareaholic 8
Same name and namespace in other branches
- 7.3 public.php \ShareaholicPublic::post_first_image()
Copied straight out of the wordpress version, this will grab the first image in a post.
Return value
mixed either returns `false` or a string of the image src
1 call to ShareaholicPublic::post_first_image()
- ShareaholicPublic::get_image_url_for in ./
public.php - Get image used in a piece of content
File
- ./
public.php, line 259
Class
- ShareaholicPublic
- This class is all about drawing the stuff in publishers' templates that visitors can see.
Code
public static function post_first_image($body) {
preg_match_all('/<img.*?src=[\'"](.*?)[\'"].*?>/i', $body, $matches);
if (isset($matches) && isset($matches[1][0])) {
// Exclude base64 images; meta tags require full URLs
if (strpos($matches[1][0], 'data:') === false) {
// file_create_url function doesn't convert paths starting with "/" so check for "/" and trim it off if present
if (substr($matches[1][0], 0, 1) === "/") {
$first_img = substr($matches[1][0], 1);
}
else {
$first_img = $matches[1][0];
}
}
}
if (empty($first_img)) {
// return false if nothing there, makes life easier
return false;
}
return file_create_url($first_img);
}