Wordpress默认情况下,裁剪的图片会直接裁剪图片的中间部分,例如你上传了一张美女图片,上传上去由于图片的尺寸超出了内部规定的尺寸,wordpress会对其进行裁剪,按照规定的尺寸裁剪好后生成另外一张图片进行保存,可
Wordpress默认情况下,裁剪的图片会直接裁剪图片的中间部分,例如你上传了一张美女图片,上传上去由于图片的尺寸超出了内部规定的尺寸,wordpress会对其进行裁剪,按照规定的尺寸裁剪好后生成另外一张图片进行保存,可惜的是,当你调用这张图片的时候发现,美女的头被卡擦了,这样的缩略图完全失去了吸引读者的功效,下面我们就来解决这个问题
复制代码代码如下:
<?PHP
add_filter( 'intermediate_image_sizes_advanced', 'bt_intermediate_image_sizes_advanced' );
add_filter( 'wp_generate_attachment_metadata', 'bt_generate_attachment_metadata', 10, 2 );
function bt_add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
global $_wp_additional_image_sizes;
$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => $crop );
}
function bt_intermediate_image_sizes_advanced( $sizes ) {
return array();
}
function bt_generate_attachment_metadata( $metadata, $attachment_id ) {
$attachment = get_post( $attachment_id );
$uploadPath = wp_upload_dir();
$file = path_join($uploadPath['basedir'], $metadata['file']);
if ( !preg_match('!^image/!', get_post_mime_type( $attachment )) || !file_is_displayable_image( $file ) ) return $metadata;
global $_wp_additional_image_sizes;
foreach ( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
else
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
else
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
$sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'];
else
$sizes[$s]['crop'] = get_option( "{$s}_crop" );
}
foreach ( $sizes as $size => $size_data ) {
$resized = bt_image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
return $metadata;
}
function bt_image_make_intermediate_size( $file, $width, $height, $crop = false ) {
if ( $width || $height ) {
$resized_file = bt_image_resize( $file, $width, $height, $crop, null, null, 90 );
if ( !is_wp_error( $resized_file ) && $resized_file && $info = getimagesize( $resized_file ) ) {
$resized_file = apply_filters('image_make_intermediate_size', $resized_file);
return array(
'file' => wp_basename( $resized_file ),
'width' => $info[0],
'height' => $info[1],
);
}
}
return false;
}
function bt_image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
if ($orig_w <= 0 || $orig_h <= 0)
return false;
// at least one of dest_w or dest_h must be specific
if ($dest_w <= 0 && $dest_h <= 0)
return false;
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);
if ( !is_array( $crop ) || count( $crop ) != 2 ) {
$crop = apply_filters( 'image_resize_crop_default', array( 'center', 'center' ) );
}
switch ( $crop[0] ) {
case 'left': $s_x = 0; break;
case 'right': $s_x = $orig_w - $crop_w; break;
default: $s_x = floor( ( $orig_w - $crop_w ) / 2 );
}
switch ( $crop[1] ) {
case 'top': $s_y = 0; break;
case 'bottom': $s_y = $orig_h - $crop_h; break;
default: $s_y = 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 );
}
function bt_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
$image = wp_load_image( $file );
if ( !is_resource( $image ) )
return new WP_Error( 'error_loading_image', $image, $file );
$size = @getimagesize( $file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
// Rotate if EXIF 'Orientation' is set
// This code is from the reverted patch at
// <a href="Http://core.trac.wordpress.org/changeset/11746/trunk/wp-includes/media.php">/file/imgs/upload/202206/12/a2d41nmapys.jpg";
$return = imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) );
if ( !$return )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
}
imagedestroy( $newimage );
// Set correct file permissions
$stat = stat( dirname( $destfilename ));
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
@ chmod( $destfilename, $perms );
return $destfilename;
}
这种方法需要你用bt_add_image_size代替原来我们习惯的add_image_size,具体的使用方法我们就不详细解释,总之这个函数给出了第三个参数,让你可以规定裁剪的开始位置。
而实际上,更为简洁的方法是,我找到一个插件,似乎可以实现这个功能,你不妨下载尝试。
Shailan.com Staff则提供了一个更直接的方法,就是修改wordpress系统文件/wp-includes/media.php,找到:
复制代码代码如下:
$s_x = floor( ($orig_w - $crop_w) / 2 );
$s_y = floor( ($orig_h - $crop_h) / 2 );
把其中的$s_y修改为:
复制代码代码如下:
$s_y = 0;// 或者你想要的值
总之,这种方法直接有效且最容易令人理解,因为$s_x和$s_y就是wordpress系统在裁剪时,裁剪区域的起始位置。而且他还推荐了一个插件:Regenerate Thumbnails,应是基于这种方法来实现的。
在stackexchange社区还有人用这样的方法更加便捷,或许你可以借鉴,但是否奏效还需要验证:
复制代码代码如下:
add_filter('wp_generate_attachment_metadata', 'custom_crop');</p> <p>function custom_crop($metadata) {</p> <p> $uploads = wp_upload_dir();
$file = path_join( $uploads['basedir'], $metadata['file'] ); // original image file
list( $year, $month ) = explode( '/', $metadata['file'] );
$target = path_join( $uploads['basedir'], "{$year}/{$month}/".$metadata['sizes']['medium']['file'] ); // intermediate size file
$image = imagecreatefromjpeg($file); // original image resource
$image_target = wp_imagecreatetruecolor( 44, 44 ); // blank image to fill
imagecopyresampled($image_target, $image, 0, 0, 25, 15, 44, 44, 170, 170); // crop original
imagejpeg($image_target, $target, apply_filters( 'jpeg_quality', 90, 'image_resize' )); // write cropped to file</p> <p> return $metadata;
}
--结束END--
本文标题: wordpress从顶部开始裁剪图片的方法
本文链接: https://lsjlt.com/news/32177.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-27
2023-10-27
2023-10-26
2023-10-25
2023-10-21
2023-10-21
2023-10-18
2023-10-12
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0