問題描述
我已經新增了一個影像大小使用這個:
add_image_size('property-featured', 484, 393, true);
它在過去一年的工作非常好,已經生成了約 1GB 的大小的影像,但是這種影像尺寸已經不再需要了。我想清除一年中建立的影像大小。
到目前為止,我已經刪除了這行程式碼,但它並沒有清理建立的影像。
刪除這些影像的正確方法是什麼?
最佳解決方案
我發現一個外掛,為我做這件事:Additional image sizes (zui)
When you delete an image size that size will not be created for all NEW images you upload. However, images created for deleted sizes still exist on the server as well as the image attachment metadata for those sizes. This feature will physically delete those images from the server as well as the image attachment meta data for those sizes. Use this feature at your own risk. There is no undo.
更新
雖然外掛工作奇蹟,並做了大量的清理,它不能清除由 WordPress 從過去刪除的影像留下的檔案。我用這個 home brew 指令碼清理了一些剩下的影像:
<?php
$files = find_all_files("/home/****/public_html/wp-content/uploads");
$files2 = array();
foreach($files as $key => $file) {
if(1 == preg_match("#150xd+.jpg$#", $file)) {
$files2[] = $file;
unlink($file);
}elseif(1 == preg_match("#300xd+.jpg$#", $file)) {
$files2[] = $file;
unlink($file);
}elseif(1 == preg_match("#d+x300.jpg$#", $file)) {
$files2[] = $file;
unlink($file);
}elseif(1 == preg_match("#d+x150.jpg$#", $file)) {
$files2[] = $file;
unlink($file);
}elseif(1 == preg_match("#d+x1024.jpg$#", $file)) {
$files2[] = $file;
unlink($file);
}
}
print_r($files2);
function find_all_files($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
foreach(find_all_files("$dir/$value") as $value)
{
$result[]=$value;
}
}
return $result;
}
?>
次佳解決方案
我使用 http://wordpress.org/extend/plugins/regenerate-thumbnails/重新建立我的縮圖,據我所知,它刪除所有的影像大小,然後重新生成它們。這可能是一個緩慢的,取決於多少影像+影像大小被重新掃描。
另一種方法是透過 SSH 執行此操作,並使用以下命令找到它們:
find ./uploads/* -iname '*-484x393.*' -ls
然後執行如下命令:
find ./uploads/* -iname '*-484x393.*' -exec rm {} ;
請確保您在執行此命令之前備份所有內容。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。