有些网站有些GIF的动态缩略图,如果还用timthumb来剪裁的话就变成静态图了,这时需要写一个函数来判断是否为GIF,是的话就不用
timthumb来剪裁。
function ImgUrl($url) { if(strpos($url,'gif') !== false) { echo $url; } else{ echo TEMPLATE_URL.'/timthumb.php?src='.$url.'h=180w=300zc=1'; } }
其中TEMPLATE_URL是emlog中获取模板路径的函数,wordpress的话是get_bloginfo(“template_url”),因为timthumb一般在模板目录里放,后面那些参数就是高-宽-剪裁方式,还有压缩比q。
这个呢就是直接需要用缩略图的地方调用timImage(url,100,80,1);就行,会返回一个处理好的链接。
function timImage($url,$h,$w,$z) { $imgUrl = TEMPLATE_URL."timthumb.php?src=".$url."&h=".$h."&w=".$w."&zc=".$z; return $imgUrl; }
这个是emlog中正则获取文章中第一个图片URL,如果没有,取模板目录images/random其中随机图片。
wordpress的话是代码中加个global $post; 然后$value['content']替换为
$post
->post_content
,然后是替换模板路径那个函数.
function getImage(){ $imgsrc = preg_match_all("|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $value['content'], $img); $imgsrc = !empty($img[1]) ? $img[1][0] : ''; $thum_src = $img[1][0]; $thum_img = TEMPLATE_URL."timthumb.php?src=".$thum_src."&h=150&w=220&zc=1"; if (empty($thum_src)) { $thum_src = TEMPLATE_URL."images/random/".rand(1,20).".jpg"; $thum_img = TEMPLATE_URL."timthumb.php?src=".$thum_src."&h=150&w=220&zc=1"; } echo $thum_img;//return $thum_img; }