相信很多人对WordPress的缩略图不太满意,因为WordPress系统会默认裁剪三种缩略图,然后博客上传的图片就会生成不同大小的三份,而其中两份又没有使用,这样下来严重占用并浪费了我们的网站空间,今天暖岛交大家一种方法,完美结局文章缩略图的困扰:使用timthumb.php进行裁剪缩略图。
温馨提示:
timthumb需要主机支持GD库;timthumb处理过程需要一定的服务器资源支持;timthumb不支持外链图片;timthumb出现过漏洞;作者表示不再更新timthumb。首先关闭WordPress缩略图功能:
把缩略图大小、中等、大尺寸的宽高都设置为0或留空,那么以后上传图片的时候,就只有原始大小一张图片一个大小规格。
下载timthumb.php
下载后,上传timthumb.php到当前主题文件夹,并在该主题文件夹中创建一个命名为cache的文件夹,设置cache文件夹的权限为755或777。
//WordPress输出缩略图地址function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values("thumbnail") ) { //输出自定义域图片地址 $values = get_post_custom_values("thumbnail"); $post_thumbnail_src = $values [0]; } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); $post_thumbnail_src = $thumbnail_src [0]; } else { $post_thumbnail_src = ''; ob_start(); ob_end_clean(); $output = preg_match_all('//i', $post->post_content, $matches); $post_thumbnail_src = $matches [1] [0]; //获取该图片 src if(empty($post_thumbnail_src)){ $post_thumbnail_src = get_bloginfo('template_url')."/images/no-image.jpg"; //如果日志中没有图片,则显示默认图片 } }; echo $post_thumbnail_src;}使用方法
在需要显示缩略图的地方添加调用代码:
其中h为缩略图的高度,w为缩略图宽度,zc有两个属性值,1表示裁剪,0表示按设置的高宽压缩,不裁剪。
附修补timthumb.php漏洞问题:
使用最新版的timthumb.php(但作者表示不再更新)服务器目录权限设置删除白名单在文件中找到以下代码,删除或注释掉
// external domains that are allowed to be displayed on your website$allowedSites = array ( 'flickr.com', 'picasa.com', 'blogger.com', 'wordpress.com', 'img.youtube.com',);暖岛网(nuandao.cn)原创文章,转载必须以链接形式标明本文链接