Great question! WordPress缺少一些更高级别的图像管理功能,这些功能可以使文件维护变得更加简单。WordPress的核心团队一直威胁要为许多版本添加增强图像功能;最终他们可能真的会这么做!在此之前,您必须使用插件和/或自定义代码的补丁。
更多好消息和坏消息:good news WordPress与PHP的结合是否具有大量较低级别的图像处理功能,但bad news 它有大量的低级图像处理功能,您必须从中决定使用哪种功能!不久前我发布了list of image handling functions found in WordPress 如果你扫描一下,你可能会看到有几种不同的方法可以剥这只猫的皮。
尽管如此,我选择了一种方法,下面给出了大量的评论,而不是在本文中进行解释。您可以将其复制到网站的根目录/downsize-images.php
并从浏览器中调用它。如果超时,不要担心;我写它是为了跟踪它已经做了什么,所以只要继续运行它,直到你看到它打印出来“完成!”
NOTE: 您需要设置常量MAX_RESIZE_IMAGE_TO
无论你想要你的最大维度是什么;我举了一个例子640
像素。运行后,您将有一组图像文件,扩展名为.save
已附加。一旦你对它按照你想要的方式运行感到满意,你就可以删除那些.save
文件。我没有自动删除它们,以防脚本出错。
(IMPORTANT: 是SURE TO BACKUP 你的数据库和上传目录BEFORE 你来运行这个。它在我的机器上工作,但你可能有我没有测试的东西,它可能会导致损坏,所以备份!DO NOT 晚点回来说我没有警告你!!!):
<?php
include "wp-load.php"; // Need this to load WordPress core
include "wp-admin/includes/image.php"; // Needed for wp_create_thumbnail()
define(\'MAX_RESIZE_IMAGE_TO\',640); // SET YOUR MAX DIMENSION HERE!!!!
// Check our "queue" to see if we\'ve previously only run to partial competion.
$attachment_ids = get_option(\'attachment_ids_to_resize\');
if ($attachment_ids==\'Done!\') { // Make sure we don\'t do it again
echo \'Already done.\';
} else {
if (empty($attachment_ids)) { // If this is the first time, this array will be empty. Get the IDs.
global $wpdb;
$attachment_ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type=\'attachment\'");
}
foreach($attachment_ids as $index => $attachment_id) {
// Get metadata: [width,height,hwstring_small,file,sizes[file,width,height],image_meta[...]]:
$metadata = wp_get_attachment_metadata($attachment_id);
// Get upload_dir: [path,url,subdir,basedir,baseurl,error]:
$upload_dir = wp_upload_dir();
// Get full path to original file
$filepath = "{$upload_dir[\'basedir\']}/{$metadata[\'file\']}";
// Make a smaller version constrained by largest dimension to MAX_RESIZE_IMAGE_TO
$smaller_file = wp_create_thumbnail( $filepath, MAX_RESIZE_IMAGE_TO);
// If the file was really created
if (@file_exists($smaller_file)) {
// Append a ".save" to original file name, just in case (you can manually delete *.save)
rename($filepath,"{$filepath}.save");
// Get this smaller files dimensions
list($width,$height) = getimagesize($smaller_file);
// Strip the base directory off the filename (i.e. \'/Users/username/Sites/sitename/wp-content/uploads/\')
$metadata[\'file\'] = str_replace("{$upload_dir[\'basedir\']}/",\'\',$smaller_file);
$metadata[\'height\'] = $height;
$metadata[\'width\'] = $width;
// Get the the small size, 128x96 or smaller if need be
list($uwidth,$uheight) = wp_constrain_dimensions($metadata[\'width\'], $metadata[\'height\'], 128, 96);
$metadata[\'hwstring_small\'] = "height=\'$uheight\' width=\'$uwidth\'";
// Make sure none of the sizes are larger than the new size
foreach($metadata[\'sizes\'] as $size => $size_info) {
if (intval($size_info[\'height\'])>intval($height) ||
intval($size_info[\'width\'])>intval($width)) {
// If too large get the full file path of the sized image
$old_file = "{$upload_dir[\'path\']}/{$size_info[\'file\']}";
// Tack a ".save" extension on it, just in case (you can manually delete *.save)
rename($old_file,"{$old_file}.save");
// Remove it from the metadata since it\'s too large
unset($metadata[\'sizes\'][$size]);
}
}
// Update the attachement metadata with the new smaller values
wp_update_attachment_metadata($attachment_id,$metadata);
// Update the name of the other place the orginal file name is stored
update_attached_file($attachment_id,$metadata[\'file\']);
}
// Remove this attachment from our "queue"
unset($attachment_ids[$index]);
// Save the new smaller "queue" in case we need to run this again.
update_option(\'attachment_ids_to_resize\',$attachment_ids);
}
// If we\'ve actually processed them all
if (count($attachment_ids)==0) {
// Store a marker so we won\'t accidentally run this again
update_option(\'attachment_ids_to_resize\',\'Done!\');
}
echo \'Done!\';
}