问题启事
注:该要领已失效,文章能够住手阅读!
尽人皆知,小2的网站近期改版了,缩略图也放大了,越发的展示主题的风采了;然则实在一向隐藏着一个问题没处理,而这个问题,从第二次改版的时刻就伴跟着了,就是缩略图的尺寸照样用的网站上线初所用的缩略图尺寸。
只管背景的设置-多媒体-缩略图尺寸修改了,可这是新上传的图片才会生成新的尺寸的缩略图,小2想了许多方法,也从郑力大神那看到用ps加数据库批量的要领,但是小2站里图片太多,缩略图尺寸也庞杂不一,曾还做过中文文件名md5转换,用ps家数据库批量的要领实在是庞杂和累人!
疑问
怎样才疾速又轻易的处理wordpress缩略图尺寸从新裁剪的问题呢?
从奶嘴那相识到谷歌的timthumb很好用,小2就百度了下,确切找到相干的文献,经由过程探索,如今经由过程timthumb就能够自在的给缩略图裁剪本身设定的缩略图,能够说一个文章的缩略图能够有几个缩略图的尺寸,做到全站一切要展示缩略图的处所的尺寸都是最清楚的!
处理问题
下面我们直接说教程吧
<?php
function post_thumbnail( $width = 100,$height = 80 ){
global $post;
if( has_post_thumbnail() ){ //假如有缩略图,则显现缩略图
$timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
$post_timthumb = '<img src="'.get_bloginfo("template_url").'/timthumb.php?src='.$timthumb_src[0].'&h='.$height.'&w='.$width.'&zc=1" alt="'.$post->post_title.'" class="thumb" />';
echo $post_timthumb;
} else {
$post_timthumb = '';
ob_start();
ob_end_clean();
$output = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $index_matches); //猎取日记中第一张图片
$first_img_src = $index_matches [1]; //猎取该图片 src
if( !empty($first_img_src) ){ //假如日记中有图片
$path_parts = pathinfo($first_img_src); //猎取图片 src 信息
$first_img_name = $path_parts["basename"]; //猎取图片名
$first_img_pic = get_bloginfo('wpurl'). '/cache/'.$first_img_name; //文件地点地点
$first_img_file = ABSPATH. 'cache/'.$first_img_name; //保留地点
$expired = 604800; //逾期时候
if ( !is_file($first_img_file) || (time() - filemtime($first_img_file)) > $expired ){
copy($first_img_src, $first_img_file); //长途猎取图片保留于当地
$post_timthumb = '<img src="'.$first_img_src.'" alt="'.$post->post_title.'" class="thumb" />'; //保留时用原图显现
}
$post_timthumb = '<img src="'.get_bloginfo("template_url").'/timthumb.php?src='.$first_img_pic.'&h='.$height.'&w='.$width.'&zc=1" alt="'.$post->post_title.'" class="thumb" />';
} else { //假如日记中没有图片,则显现默许
$post_timthumb = '<img src="'.get_bloginfo("template_url").'/images/default_thumb.gif" alt="'.$post->post_title.'" class="thumb" />';
}
echo $post_timthumb;
}
}
?>
将上面的代码放进wordpress主题文件夹的functions.php内,然后下载文章底部的 timthumb.php 文件放到wordpress主题的根目次下!
然后经由过程
<?php post_thumbnail(243,182); ?>
这个函数就能够挪用出裁剪好尺寸为243*182的缩略图了,响应的缩略图构造代码参照:
<a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>"><?php post_thumbnail(243,182); ?></a>
个中243代表宽,182代表高,你们在运用的时刻替换成你们的尺寸即可!
然后我们还要给网站根目次下建立cache目次,并赋予777权限,如许就能够全自动挪用裁剪好的缩略图了,而这些缩略图也就都存在了cache文件夹里,运用起来很轻易!
衍生疑问
这个时刻有人会问,那体系自带的生成缩略图的功用这么办?每次上传图片,体系都邑生成几个尺寸的缩略图,有了timthumb还需要体系自带的缩略图生成功用吗?小2的回覆是:随意你!
wordpress批量删除悉数文章缩略图的要领能够下载一些插件来举行删除!
关于wordpress缩略图尺寸从新裁剪,这是timthumb篇,另有七牛篇今后再引见给人人!
衍生疑问2
近来听奶嘴说timthumb有权限上破绽,横竖大叔不懂,这里就交叉个教程吧,nginx与Apache制止目次实行php文件权限!
location ~ /cache/.*.(php|php5)?$ {
deny all;
}
制止cache目次实行php文件权限
2、多个目次
location ~ /(cache|upload)/.*.(php|php5)?$ {
deny all;
}
制止cache与upload目次实行php文件权限
设置完后,从新reload nginx即可。
1、单个目次
Apache制止目次实行php文件权限
以Apache 模块体式格局运转 PHP,你能够在vhosts设置文件中增加以下代码:
<Directory /www/www./upload>
php_flag engine off
</Directory>
<Directory ~ "^/www/.*/upload">
<Files ~ ".php">
Order allow,deny
Deny from all
</Files>
</Directory>
制止upload目次实行php文件权限
友谊提醒,这个nginx中和apache+windows设置目次权限要领是有所区别的
文件下载
小编恐怕代码高亮有问题,专程将functions.php和timthumb.php都打包给人人来下载!

评论列表