Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2013 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5243340 to your computer and use it in GitHub Desktop.
Save anonymous/5243340 to your computer and use it in GitHub Desktop.
Grab Image For Wordpress
<?php
/**
* Image script by Eric Hamby
* EricHamby.Com
* Get an image from the current posts content and display it on the homepage
*
* @param int $width the width to display the image
* @param int $height the height to display the image
*/
define('BM_BLOGPATH', get_bloginfo('template_url'));
function eh_postImage($width = 0, $height = 0, $id = -1, $content = '', $title = '') {
$theImageDetails = bm_getPostImage($id, $content, $title);
$theImageSrc = $theImageDetails['src'];
$theImageSrc = preg_replace ('#\?.*#', '', $theImageSrc);
// if src found, then create a new img tag
if (strlen($theImageSrc)) {
$altText = '';
if ($theImageDetails['alt'] != '') {
$altText = $theImageDetails['alt'];
}
$imagePath = BM_BLOGPATH . '/includes/timthumb.php?w=' . $width . '&amp;zc=1&amp;h=' . $height . '&amp;src=' . urlencode($theImageSrc);
$theImage = '<img src="' . $imagePath . '" width="' . $width . '" height="' . $height . '" alt="' . $altText . '" />';
$theImage = apply_filters('bm_theThumbnailImage', $theImage);
return $theImage;
}
return FALSE;
}
/**
* get the first image associated with a post
*/
function bm_getPostImage($id = -1, $content = '', $title = '') {
if ($id < 0) {
global $post;
$id = $post->ID;
$content = $post->post_content;
$title = get_the_title();
}
$theImageSrc = '';
$imageArray = array(
'Image',
'image'
);
// check for custom fields
foreach ($imageArray as $image) {
$values = get_post_custom_values($image, $id);
if(isset($values[0]) && $values[0] != '') {
$theImageSrc = $values[0];
break;
}
}
// regex on post content
if ($theImageSrc == '') {
if ($content != '') {
// use regex to find the src of the image
preg_match_all ('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $content, $matches);
$imageCount = count ($matches);
if ($imageCount >= 1) {
for ($i = 1; $i <= $imageCount; $i += 2) {
if (isset($matches[$i][0])) {
$theImageSrc = $matches[$i][0];
break;
}
}
}
}
}
// post attachments
if ($theImageSrc == '') {
$values = get_children(array('post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order'));
if ($values) {
foreach ($values as $childId => $attachment) {
// add check for image post_mime_type (jpg, gif, png)
$theImageSrc = wp_get_attachment_image_src($childId, 'full');
$theImageSrc = $theImageSrc[0];
break;
}
}
}
$theImageSrc = bm_muImageUploadPath($theImageSrc);
// return values
$ret = array(
'src' => $theImageSrc,
'alt' => __('Thumbnail') . ' : ' . $title,
);
$ret = apply_filters('bm_getPostImage', $ret);
return $ret;
}
/**
* work out the path to the image if WordPress mu is used
*/
function bm_muImageUploadPath ($theImageSrc) {
// if wpmu then do the jiggy widget
global $blog_id;
if (isset($blog_id) && $blog_id > 0) {
$imageParts = explode('/files/', $theImageSrc);
if (isset($imageParts[1])) {
$theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
}
}
return $theImageSrc;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment