# Xử lý ảnh, image trong wordpress  full phần 1(ok)

## 1. Disable image scaling in WordPress (a version 5.3 update)

```
add_filter( 'big_image_size_threshold', '__return_false' );
i
// https://stackoverflow.com/questions/62996135/remove-scaled-from-automatically-scaled-image-wordpress
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FlH1YfzZGgegQDKpRGIwm%2FScreenshot_2.png?alt=media\&token=fa6824ef-18b2-48e0-ac12-691d19f6912a)



![](broken-reference) ![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FyPfGbO8yyZN1ce7QQdvI%2FScreenshot_3.png?alt=media\&token=37dc4f3f-4b04-4839-9976-1755f1627fb8)

Kết quả&#x20;

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FNm7cfCayTU1dsjPGJf9r%2FScreenshot_4.png?alt=media\&token=015b980b-061f-418b-8e87-df461d389c0f)

Câu hỏi đặt ra có cách nào thay đổi bằng kích thước ảnh gốc không để tôi xóa kích thước ảnh 150x150 đi cho đỡ tốn bộ nhớ :)

Muốn nghiên cứu rõ hơn thì chúng ta tìm hiểu ở file&#x20;

C:\xampp\htdocs\wordpress3\wp-includes\media.php

C:\xampp\htdocs\wordpress3\wp-admin\includes\image.php

### 2. Muốn loại bỏ kích thước \['medium', 'medium\_large','thumbnail','large', '1536x1536', '2048x2048'] trong wordpress

```
add_filter( 'big_image_size_threshold', '__return_false' );
add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1);
function remove_default_img_sizes( $sizes ) {
  $targets = ['medium', 'medium_large','thumbnail','large', '1536x1536', '2048x2048','woocommerce_thumbnail','woocommerce_single','woocommerce_gallery_thumbnail','shop_catalog','shop_single','shop_thumbnail'];
  foreach($sizes as $size_index=>$size) {
    if(in_array($size, $targets)) {
      unset($sizes[$size_index]);
    }
  }
  return $sizes;
}
```

Hướng dẫn cách lấy tất cả các kích thước ảnh đang sử dụng trên website :(

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FBhLj7UxdD8KW63Bu99mW%2FScreenshot_6.png?alt=media\&token=ad401f87-200f-4736-96a5-7d8a4b9c637a)

### 3. Xử lý ảnh kết hợp với lệnh cmd hỗ trợ của wordpress để loại bỏ nhưng kích thước không mong muốn :))

![](broken-reference) ![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FhMhXAL6gyh9aM4OhYwbr%2FScreenshot_8.png?alt=media\&token=be91ea56-2e82-40fd-9132-cb529bfdd907)

![](broken-reference) ![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FPDTf4E1kphAqZYZ2PWaO%2FScreenshot_9.png?alt=media\&token=1af80413-fc78-44d5-b6fe-226a59e79c72)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FgcylI5maNjsMMiS46q6Z%2FScreenshot_10.png?alt=media\&token=5f0bddf1-4cfb-4c13-b02a-3566f7145edd)

### **4. My default media setting in WordPress blog is 150x150, 300x300, 1024x1024** respectively for **thumbnail**, **medium**, **large** images. Now I am designing a theme that need different size thumbnail images to show in theme. For this, I go to the media setting again and changed the setting with my news sizes like **72x72**, **250x250**, **400x400** respectively for **thumbnail**, **medium**, **large** images.

But now to resize my old uploaded images, I used [**Regenerate Thumbnails**](https://wordpress.org/plugins/regenerate-thumbnails/) WordPress plugin and that generated my new media setting images size easily. So this solved my main problem.

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FcWPCBj7YTiIRaSE3WB9Q%2FScreenshot_3.png?alt=media\&token=010fc6b6-c8c1-476f-8278-cebce1b0e484)

![](broken-reference) ![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FYQc5OjEsBM48FApmHKxv%2FScreenshot_4.png?alt=media\&token=191698f6-93c3-4300-865a-35cb2b8f241e)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FFToqLvd33HmTBbvsMC8c%2FScreenshot_5.png?alt=media\&token=b6ef3f0b-a53f-4e8d-ba9e-7920802667fe)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FVnz0My9tTCw1HLO7Q2rI%2FScreenshot_6.png?alt=media\&token=a9cf770a-ddd9-438c-bd4e-738e44aa93ce)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FH69V5tQlYjWPqvAGpQ01%2FScreenshot_7.png?alt=media\&token=3d719694-1d90-48f9-9af1-8bf8f9db9f4a)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F3thdkILbxDZ7G3YOlShS%2FScreenshot_8.png?alt=media\&token=7c426489-df60-4aea-936a-d82266d58cfa)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FC0omJpQpuAKTTJNRTYiD%2FScreenshot_9.png?alt=media\&token=20cf2d6c-2054-4e80-b1fa-baebc173f52a)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FPq5zvS90G5pNVVq7FerD%2FScreenshot_13.png?alt=media\&token=f4dba896-c223-49d2-977b-fcd83cf1da78)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FNbPPEz4VDaof82xRizuQ%2FScreenshot_10.png?alt=media\&token=80955bbd-870b-4ade-82b8-7c3b1006b6f4)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FGqDY8D2n3gplpuvJtlIB%2FScreenshot_11.png?alt=media\&token=1734dc8c-44cb-4b41-9439-5e8b117aa0d5)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F1rE3TtX5WtIDVEXdLCbe%2FScreenshot_12.png?alt=media\&token=fbdf9048-41fe-41f6-b25a-74c1dd537096)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FyeNd7cv1XthkT6M8MHvR%2FScreenshot_14.png?alt=media\&token=85b9451e-99ed-4f31-8330-850c8578782d)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FZJ2H4R15acC6libwG7J6%2FScreenshot_15.png?alt=media\&token=4c86c35a-9bf5-4dbe-b6fb-a19785aa04da)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2Fie9B2v60R2GW8hZnHyi7%2FScreenshot_16.png?alt=media\&token=c7d5a281-f71d-43f6-90d8-372283ce2251)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FYArM3j3H7jTjNyk2BY9u%2FScreenshot_17.png?alt=media\&token=3e766bf3-da17-4cbc-a0be-1b4264994df2)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FWwtq1zFeigIBI6ipV46j%2FScreenshot_18.png?alt=media\&token=5425df6f-3072-40d0-a159-80843a92cfbd)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F6M7cXk73q4X8cQipPmiA%2FScreenshot_19.png?alt=media\&token=ec7c288e-b29d-4fa0-b07a-d7e2c46f14ce)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FYnmhwf206MxnSr8Mdogi%2FScreenshot_20.png?alt=media\&token=b6702b44-f8df-4538-b556-9cdc9d21f8cd)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F5aHGge1e0fEIwnqncBSR%2FScreenshot_22.png?alt=media\&token=7da06582-ec37-418d-900a-e963b4b9a9f7)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F5aHGge1e0fEIwnqncBSR%2FScreenshot_22.png?alt=media\&token=7da06582-ec37-418d-900a-e963b4b9a9f7)

### 5. Cách sử dụng remove\_image\_size kết hợp với init cũng có tác dụng như trên :)

```
function remove_extra_image_sizes() {
  foreach ( get_intermediate_image_sizes() as $size ) {
    if ( !in_array( $size, array( 'medium', 'medium_large', 'large' ) ) ) {
      remove_image_size( $size );
    }
  }
}
add_action('init', 'remove_extra_image_sizes');
```

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FIK07CIQ9HCVGx9iduQJT%2FScreenshot_1.png?alt=media\&token=8733b13c-f6f9-4554-a662-77ac80d22c93)

Kết hơp với cầu lệnh C:\xampp\htdocs\wordpress3>wp media regenerate --yes

```
wp media regenerate --yes
```

![](broken-reference) ![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2F31iPtLjevWtR44tvhqn8%2FScreenshot_2.png?alt=media\&token=0cad2109-79be-410e-b2fe-89fbf1dd308a)
