Friday 13 March 2009

Php, Joomla: alphacontent(joomla component) show youtube thumbnail

A very good frontpage component for joomla, AlphaContent, can pick up photos from article as thumbnail on the article list. However, it just pick up thumbnail for articles, which have photos inside, not for those only embed video object.

Modify the 'function findIMG( $contenttext, $showfirstimg)', in /components/com_alphacontent/assets/includes/alphacontent.functions.php, will solve this problem.

Solution:
=====================

<?php

$contenttext = <<<youtube
<div><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="425" height="350"><param name="width" value="425" /><param name="height" value="350" /><param name="src" value="http://www.youtube.com/v/e_0oqGy0OyY" /><embed type="application/x-shockwave-flash" width="425" height="350"
src="http://www.youtube.com/v/e_0oqGy0OyY"></embed></object></div>

youtube
;
$showfirstimg = '1';

//<img width="20" src="http://cnfeed.co.uk/images/stories/screenshot.png"></img>

function findIMG( $contenttext, $showfirstimg ) {
$image = "";

// check is there any img tag/images
if (preg_match_all('#<img(.*)>#', $contenttext, $match0) ) {
//print_r($match0);
if ( count($match0) ) {
$n = sizeof($match0[1]);
if ( $showfirstimg=='2' ) {
$contenttext = $match0[1][$n-1];
} else $contenttext = $match0[1][0];
}
// else $contenttext may just host video 'src'

// for image search
if ( preg_match_all('#src="(.*)"#Uis', $contenttext, $match ) ) {
//print_r($match);
if ( count($match) ) {
$n = sizeof($match[1]);
if ( $showfirstimg=='2' ) {
$image = $match[1][$n-1];
} else $image = $match[1][0];
}
return $image;
}
}

// for youtube video search, just the first video thumbnail
if ( preg_match('#src="(.*)"#Uis', $contenttext, $match_video ) ) {
//print_r($match_video);
if ( count($match_video) ) {
if (preg_match('#youtube#i', $match_video[0], $match_temp) ) {
$youtube_src = $match_video[1];
//print_r($youtube_src);
if (preg_match('/v[\=\/][a-zA-Z0-9_]{1,}&/',$youtube_src, $image_0)) {
preg_match('/v[\=\/](.*)&/',$image_0[0], $image_1);
} else {
preg_match('/v[\=\/](.*)/',$youtube_src, $image_1);
}

//print_r($image_1);
$vid = ( $image_0 === null ) ? Vj9ChXA9y8I : $image_1[1];
$image = "http://img.youtube.com/vi/".$vid."/0.jpg";
}

// for other video search, static image to inform others: othervideo.png
else {
$image = "http://cnfeed.co.uk/images/othervideo.png";
}
}
}

if ($image == ''){
$image = "http://cnfeed.co.uk/images/noimage.png";
}

return $image;
}

echo findIMG( $contenttext, $showfirstimg );
?>

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter