修改WordPress thumbnail截图截取的起始位置
如果你在使用WordPress的thumbnail功能, 你会发现它从大图截出来的小图都是从中间那一块截出来的. 有时我们的需求并不是这样, 我们需要它从顶部开始计算并截取.
修改方法:
1. 在wp-includes中找到media.php;
2. 我的media.php中是351行, 352行 (或者你通过$s_y 或
$s_X来搜索
):
$s_x = floor( ($orig_w - $crop_w) / 2 );
$s_y = floor( ($orig_h - $crop_h) / 2 );
这一块就是大图的截取功能.
3. 具体修改:
从顶部开始截图:
$s_y = 0; //floor( ($orig_h - $crop_h) / 2 );
从左边开始截图:
$s_x = 0; //floor( ($orig_w - $crop_w) / 2 );
从左上角开始截图:
$s_y = 0; // floor( ($orig_h - $crop_h) / 2 );
$s_x = 0; // floor( ($orig_w - $crop_w) / 2 );
然后保存推出media.php即可.
如果你怕下次升级media.php文件被覆盖, 那么你可以把这个功能放到主题的function.php文件中(如果下面代码复制过去单引号有问题, 记得改成英文的):
function geek100_image_resize_dimensions( $payload, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){
// Change this to a conditional that decides whether you want to override the defaults for this image or not.
if( false )
return $payload;
if ( $crop ) {
// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
$aspect_ratio = $orig_w / $orig_h;
$new_w = min($dest_w, $orig_w);
$new_h = min($dest_h, $orig_h);
if ( !$new_w ) {
$new_w = intval($new_h * $aspect_ratio);
}
if ( !$new_h ) {
$new_h = intval($new_w / $aspect_ratio);
}
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);
$s_x = 0; // [[ formerly ]] ==> floor( ($orig_w - $crop_w) / 2 );
$s_y = 0; // [[ formerly ]] ==> floor( ($orig_h - $crop_h) / 2 );
} else {
// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
$crop_w = $orig_w;
$crop_h = $orig_h;
$s_x = 0;
$s_y = 0;
list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
}
// if the resulting image would be the same size or larger we don't want to resize it
if ( $new_w >= $orig_w && $new_h >= $orig_h )
return false;
// the return array matches the parameters to imagecopyresampled()
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
add_filter( 'image_resize_dimensions', 'geek100_image_resize_dimensions', 10, 6 );