[wp-hackers] Extract first image from a post
Mike Schinkel
mikeschinkel at gmail.com
Sun Nov 23 23:15:16 GMT 2008
-----Original Message-----
From: wp-hackers-bounces at lists.automattic.com
[mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Alex
Hempton-Smith
Sent: Sunday, November 23, 2008 5:30 PM
To: wp-hackers at lists.automattic.com
Subject: Re: [wp-hackers] Extract first image from a post
Okay, after some searching I've managed to get it working with some very
complicated (for me anyway) code, it goes completely over my head but at
least it works!
For anybody who's interested:
function teaserimg()
{
ob_start();
the_content();
$html = ob_get_contents();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $html,
$matches, PREG_SET_ORDER);
$p = $matches [0] [1];
// maximum width is defined here
$mw = '215';
// maximum height is defined here
$mh = '160';
if(list($w,$h) = @getimagesize($p)) {
foreach(array('w','h') as $v) { $m = "m{$v}";
if(${$v} > ${$m} && ${$m}) { $o = ($v == 'w') ? 'h' : 'w';
$r = ${$m} / ${$v}; ${$v} = ${$m}; ${$o} = ceil(${$o} * $r); } }
return("<img src='{$p}' alt=' ' width='{$w}' height='{$h}' />"); } }
-- Alex
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers
More information about the wp-hackers
mailing list