Prevent and clean up the thumbnails generated by WordPress

WordPress generated a lot of thumbnails which occupied sizeable spaces. If we want to take full control of the uploaded images, it would be hard.

Prevent the thumnails generating

1. In Media setting, set all sizes to 0.

 

2. Access the options.php directly via “http://<your domain>/wp-admin/options.php“, search “medium_large_size_w” and set the value to 0.

 

3. Edit functions.php in the theme and add below lines. The purpose is to stop WordPress generating other image sizeds.

// Disable other image sizes
add_filter('add_image_size', '__return_null');
add_filter('set_post_thumbnail_size', '__return_null');

And also for more control, we can prevent WordPress from compressing JPEG by default 90% via adding below lines

// Prevent from compressing JPEG
add_filter('jpeg_quality', function($arg){return 100;});
add_filter('wp_editor_set_quality', function($arg){return 100;});

Sometime, the theme itself will generate thumbnails. It could vary. For example, in Verbosa theme’s “setup.php“, we need remove this line:

add_image_size('header', $verbosa_headerwidth, $verbosa_headerheight,	true);

 

Clean up existing thumbnails

1. Update the posts to point to the original image files.

The links are in the post_contents of posts table. However, MySQL does not provide REGEXP_REPLACE like Oracle, hence we have 2 options: 1) Export the posts table, update and import it back; 2) Edit the posts via web UI of WordPress directly.

No matter which way, we need below regular expressions to

1) Remove the links from the image:

Replace
<a href=.+?>(.+?)</a>
by
$1

2) Remove the parameters and styles from the img tag:

Replace
(<img).+?(src=".+?").+?(/>)
by
$1 $2 $3

3) Remove the size suffix from the image file name:

Replace
-\d{3,4}?x\d{3,4}?(\.?[a-z]{3})
by
$1

 

2. Update the media library to point to the original image files.

The links are in the meta_value of postmeta table. Seems the only way is to export, update and import it back.

Replace
(s:5:"sizes";).+?(s:10:"image_meta";)

by
$1a:0:{}$2

Reference

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Back to Top