Thay đổi kích thước hình ảnh WordPress

Cập nhật lần cuối vào

Nếu bạn muốn thay đổi kích thước hình ảnh của WordPress để làm thumbnail thì bài viết này sẽ giúp ích cho bạn. Bạn có thể thay đổi kích thước thumbnail của hình ảnh thành bất kỳ kích thước nào. Thậm chí là bạn cũng có thể vừa thay đổi kích thước vừa cắt hình ảnh cho hiển thị đẹp hơn.

Sử dụng hàm: image_make_intermediate_size

Hàm image_make_intermediate_size sẽ giúp bạn thay đổi kích thước của bất kỳ hình ảnh nào trên trang WordPress của bạn.

Thay đổi kích thước hình ảnh WordPress

Bạn có thể sử dụng hàm nào vào bất kỳ hình ảnh nào. Tuy nhiên, trường hợp phổ biến nhất là bạn dùng chung với hàm the_post_thumbnail. Ví dụ bên dưới là mình dùng để thay đổi kích thước hình ảnh thumbnail khi người dùng thay đổi kích thước ảnh:

function hocwp_theme_image_downsize_filter( $downsize, $id, $size ) {
	global $hocwp_theme;

	if ( ! isset( $hocwp_theme->image_downsizes ) ) {
		global $_wp_additional_image_sizes;

		$hocwp_theme->image_downsizes = array();

		$sizes = get_intermediate_image_sizes();

		foreach ( $sizes as $size_name ) {
			if ( in_array( $size_name, array( 'thumbnail', 'medium', 'large' ) ) ) {
				$hocwp_theme->image_downsizes[ $size_name ]['width']  = get_option( $size_name . '_size_w' );
				$hocwp_theme->image_downsizes[ $size_name ]['height'] = get_option( $size_name . '_size_h' );
				$hocwp_theme->image_downsizes[ $size_name ]['crop']   = (bool) get_option( $size_name . '_crop' );
			} elseif ( isset( $_wp_additional_image_sizes[ $size_name ] ) ) {
				$hocwp_theme->image_downsizes[ $size_name ] = $_wp_additional_image_sizes[ $size_name ];
			}
		}
	}

	$sizes = $hocwp_theme->image_downsizes;

	$imagedata = wp_get_attachment_metadata( $id );

	if ( ! is_array( $imagedata ) ) {
		return false;
	}

	$image_path = get_attached_file( $id );

	if ( is_string( $size ) ) {
		if ( empty( $sizes[ $size ] ) ) {
			return false;
		}

		$current_size = $sizes[ $size ];

		$image_size = isset( $imagedata['sizes'][ $size ] ) ? $imagedata['sizes'][ $size ] : '';

		if ( ! empty( $image_size ) ) {
			if ( $current_size['width'] == $image_size['width'] && $current_size['height'] == $image_size['height'] ) {
				return false;
			}

			if ( ! empty( $image_size['width_query'] ) && ! empty( $image_size['height_query'] ) ) {
				if ( $image_size['width_query'] == $current_size['width'] && $image_size['height_query'] == $current_size['height'] ) {
					return false;
				}
			}

		}

		$resized = image_make_intermediate_size( $image_path, $current_size['width'], $current_size['height'], $current_size['crop'] );

		if ( ! $resized ) {
			return false;
		}

		$image_size = $resized;

		$image_size['width_query']  = $current_size['width'];
		$image_size['height_query'] = $current_size['height'];

		$imagedata['sizes'][ $size ] = $image_size;

		wp_update_attachment_metadata( $id, $imagedata );

		$att_url = wp_get_attachment_url( $id );

		return array( dirname( $att_url ) . '/' . $resized['file'], $resized['width'], $resized['height'], true );
	} else if ( is_array( $size ) ) {
		$crop = isset( $size['crop'] ) ? (bool) $size['crop'] : ( array_key_exists( 2, $size ) ? $size[2] : true );

		$new_width  = isset( $size['width'] ) ? $size['width'] : $size[0];
		$new_height = isset( $size['height'] ) ? $size['height'] : ( isset( $size[1] ) ? $size[1] : $new_width );

		if ( ! $crop ) {
			if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
				add_filter( 'jetpack_photon_override_image_downsize', '__return_true' );
				$data = wp_get_attachment_image_src( $id, 'full' );
				remove_filter( 'jetpack_photon_override_image_downsize', '__return_true' );
			} else {
				$data = wp_get_attachment_image_src( $id, 'full' );
			}

			if ( $data ) {
				if ( $data[1] > $data[2] ) {
					$ratio      = $data[1] / $new_width;
					$new_height = round( $data[2] / $ratio );
				} else {
					$ratio     = $data[2] / $new_height;
					$new_width = round( $data[1] / $ratio );
				}
			}
		}

		$ext      = pathinfo( $image_path, PATHINFO_EXTENSION );
		$new_path = preg_replace( '/^(.*)\.' . $ext . '$/', sprintf( '$1-%sx%s.%s', $new_width, $new_height, $ext ), $image_path );
		$att_url  = wp_get_attachment_url( $id );

		if ( file_exists( $new_path ) ) {
			return array( dirname( $att_url ) . '/' . basename( $new_path ), $new_width, $new_height, $crop );
		}

		$resized = image_make_intermediate_size( $image_path, $new_width, $new_height, $crop );

		$imagedata = wp_get_attachment_metadata( $id );

		$imagedata['sizes'][ $new_width . 'x' . $new_height ] = $resized;

		wp_update_attachment_metadata( $id, $imagedata );

		if ( ! $resized ) {
			return false;
		}

		return array( dirname( $att_url ) . '/' . $resized['file'], $resized['width'], $resized['height'], $crop );
	}

	return $downsize;
}

add_filter( 'image_downsize', 'hocwp_theme_image_downsize_filter', 10, 3 );

Đoạn code bên trên mình dùng filter image_downsize để thay đổi lại hình ảnh ngay lập tức. Khi người dùng cài đặt thay đổi kích thước ảnh thumbnail trong admin. Chúc bạn thành công.

Theo dõi
Thông báo của
guest

2 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận
phố nhạc
5 năm trước

đoạn code này m chep vô đâu vậy admin. m không hiểu lắm thánks

La Dịu
5 năm trước
Trả lời  phố nhạc

Đoạn code này bạn bỏ vào tập tin functions.php của giao diện hoặc plugin nhé. Mục đích là để thay đổi kích thước hình ảnh thành bất kỳ kích thước nào được gọi thông qua hàm get_the_post_thumbnail.

https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/