WordPress实现缩略图功能

WordPress内置的缩略图功能我不太喜欢。每次上传图片都会生成一个小图。不爽。

这是提供一种比较简单的方法,非常方便。从某些主题里面分离出来的。

缩略图功能

1,timthumb:实现缩略图。

  • 最新版下载:http://code.google.com/p/timthumb/ (如存为timthumb.php)。
  • 把timthumb.php放到wp主题的目录下(如../wp-content/themes/twentyten/)。
  • 再在主题的目录下新建一个目录cache(应该是会自动生成的,有错误的话就自己手动)

里面有:

// external domains that are allowed to be displayed on your website
$allowedSites = array (
'flickr.com',
'picasa.com',
'blogger.com',
'wordpress.com',
'img.youtube.com',
);

这些都是允许抓取的外部链接的图像,自己根据需要加上自己常用的。

2,主题目录下的functions.php

把下面两个函数丢进主题下面的functions.php里面。

function get_image_url($postID,$thumborno){

$iNum='';

// Get the post ID

$iPostID = $postID;

 $files =& get_children("post_parent=$iPostID&post_type=attachment&post_mime_type=image");

 if($files){

  $keys = array_keys($files);

  $iNum=$keys[0]; 

 }

if( $thumborno == 1 ) 

{

   $sImageUrlSet = wp_get_attachment_thumb_url($iNum);

 }

 elseif( $thumborno == 2 ) 

{

 $sImageUrlSet =wp_get_attachment_medium_url($iNum);

 }

 else

 {

 $sImageUrlSet = wp_get_attachment_url($iNum);

}

/* 这里开始调用c12pulltheimgurl函数,抓取外部图片。 不需要调用外部图片的,可注释掉*/
  if( empty($sImageUrlSet) )   {

 $thepostimages = c12pulltheimgurl($iPostID);  

 foreach($thepostimages as $thepostimage)

 {

 $sImageUrlSet=$thepostimages[0];

 }
 }
 /* 调用外部图片这里结束 */


if( !empty($sImageUrlSet) )  {   
$sImageUrl = $sImageUrlSet;   
if($AntisnewsOptions[$themeoptionsprefix.'_precleanthumburl'] == "on"){
$sImageUrl=prepsimageurl($sImageUrl);
}   
}   else   {
$sImageUrl='http://liucheng.name/wp-content/themes/premiumnews/images/no-img-thumb.jpg'; //都没有图片时,指定一个默认的图片   
}
return $sImageUrl;
}


function c12pulltheimgurl($thepostid)  {
$theimgstring=get_the_content($thepostid);
$c12tcmts = '/<img (?:.*?)src=(?:"|\'){1}(.*?)(?:"|\'){1}/is';
$theimgmtchesarr = array();
$procmatchs = preg_match_all($c12tcmts,$theimgstring,$theimgmtchesarr);
if( $procmatchs!==false && isset($theimgmtchesarr[1]) )  {
return $theimgmtchesarr[1];
}  else  {
return false;
}
}

3, 在需要显示缩略图的地方用下面的代码调用:

<?php $thecimage = get_image_for_crop($post->ID,$thumborno=1);?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/timthumb.php?src=<?php echo $thecimage; ?>&w=100&h=57&zc=1&q=95" alt="<?php the_title(); ?>" /></a>

解释一下上面的&w=100&h=57&zc=1&q=95。 w是宽,h是高,zc可选0(不按原图的比例缩放)和1(按原图的比例),q是生成缩略图的质量,100最大。

其它的css 样式就自己搞定了。。

效果看本站。。 一直都在用的。。

~完

60 回复
  1. mice says:

    [可怜] 这个东西蛮好用的,就是新版本貌似有问题,会重复生成那些生成过的缩略图,用老版本就木有那个问题… [擦汗]

    柳城 回复:

    既然有这种问题。。。 [擦汗]

    mice 回复:

    前段时间发现的..就因为这个PHP文件CPU直接涨到70%(新版本的),换了老版本以后就变成30%了..我也没具体研究什么原因造成的..

  2. 荣斌 says:

    柳城,不知道怎么的,觉得你的网站访问速度变慢了。在miaoVIP上,最好的插件应当是wp super cache哦。 [呲牙]

  3. www.saxue.com says:

    楼主这样做麻烦多了,只需要 修改media.php文件即可

    要把’alt’ => trim(strip_tags( get_post_meta($attachment_id, ‘_wp_attachment_image_alt’, true) )), // Use Alt field first
    ’title’ => trim(strip_tags( $attachment->post_title )),
    改成:
    ’alt’ => the_title(”,false,”),
    ’title’ => the_title(”,false,”),
    没有那么麻烦,下面是我写的文章:

    http://www.saxue.com/wordpress/wordpress-insert-image/

    柳城 回复:

    不升级随便你改。。

评论已关闭。