我知道这很旧,但我仍然需要解决这个问题,当我在函数中使用它时,这可能对新图像有效。php,但我确实有一些问题和图像变得不可用,我也无法利用这一点来更新现有的图像。我有大约2000张现有的图片,我需要把它们全部浏览一遍,把所有旧的大图片缩小。我在前端创建了一个只有管理员才能访问的页面,并使用了此代码。
这里最大的区别在于,不是删除OG图像,而是将“大”图像重命名为OG名称。(这对我不起作用,我尝试这种方法时丢失了很多图像),因此我复制了大图像,并将其命名为与OG图像相同的副本。因此,大图像会被较小的“大”图像覆盖。同样在前端,我必须改变元数据的更新方式。
Test this on individual posts first, be sure this works for you before changing numposts to -1. I stupidly lost about 100 images trying to run the original code without carefully checking the results first.
更新现有图像的前端代码:
function replace_uploaded_image_frontend($image_data, $attachment_id) {
// if there is no large image : return
if (!isset($image_data[\'sizes\'][\'large\'])) return $image_data;
// paths to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir[\'basedir\'] . \'/\' .$image_data[\'file\'];
$current_subdir = substr($image_data[\'file\'],0,strrpos($image_data[\'file\'],"/"));
$large_image_location = $upload_dir[\'basedir\'] . \'/\'.$current_subdir.\'/\'.$image_data[\'sizes\'][\'large\'][\'file\'];
//Original code was delete OG file and rename the large file to the OG name. This means you no longer have the large size image. So...
//Instead I want to take the large file and make a copy and overwrite it over the large file.
$file_to_be_copied = $large_image_location;
$copied_file_name = $uploaded_image_location;
//make a copy of the large image and name that the title of the original image
if (!copy($file_to_be_copied, $copied_file_name)) {
echo "failed to copy $file...\\n";
}
fixImageMeta($attachment_id );
}
function fixImageMeta($attach_id) {
$file = get_attached_file($attach_id);
if (!empty($file)) {
$info = getimagesize($file);
$meta = array (
\'width\' => $info[0],
\'height\' => $info[1],
\'hwstring_small\' => "height=\'{$info[1]}\' width=\'{$info[0]}\'",
\'file\' => basename($file),
\'sizes\' => array(), // thumbnails etc.
\'image_meta\' => array(), // EXIF data
);
update_post_meta($attach_id, \'_wp_attachment_metadata\', $meta);
}
}
$args = array(
\'post_type\' => \'exhibitions\',
\'p\' => 12926,
\'order\' => \'DESC\',
/*\'date_query\' => array(
\'after\' => array(
\'year\' => 2016,
\'month\' => 2,
\'day\' => 28,
),
\'before\' => array(
\'year\' => 2016,
\'month\' => 6,
\'day\' => 30,
),
),*/
\'numberposts\' => 1
);
$myposts = get_posts($args);
foreach ($myposts as $mypost){
$attachment_id = get_post_thumbnail_id( $mypost->ID );
echo \'<br>Attach ID: \' . $attachment_id . \' \';
$unfiltered = false;
$image_data = wp_get_attachment_metadata( $attachment_id, $unfiltered );
print_r($image_data);
replace_uploaded_image_frontend($image_data, $attachment_id);
}
我想这当然是由于一些损坏的图像而导致的几次错误。因此,在尝试一次检查完所有日期并发现错误后,我不得不花一些时间检查日期范围,以确保所有日期都得到更新。
新图像的代码,在函数中。php
function replace_uploaded_image($image_data) {
// if there is no large image : return
if (!isset($image_data[\'sizes\'][\'large\'])) return $image_data;
// paths to the uploaded image and the large image
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir[\'basedir\'] . \'/\' .$image_data[\'file\'];
$current_subdir = substr($image_data[\'file\'],0,strrpos($image_data[\'file\'],"/"));
$large_image_location = $upload_dir[\'basedir\'] . \'/\'.$current_subdir.\'/\'.$image_data[\'sizes\'][\'large\'][\'file\'];
//Instead I want to take the large file and make a copy and overwrite it over the original file.
$file_to_be_copied = $large_image_location;
$copied_file_name = $uploaded_image_location;
//make a copy of the large image and name that the title of the original image
if (!copy($file_to_be_copied, $copied_file_name)) {
echo "failed to copy $file...\\n";
}
// update image metadata and return them
$image_data[\'width\'] = $image_data[\'sizes\'][\'large\'][\'width\'];
$image_data[\'height\'] = $image_data[\'sizes\'][\'large\'][\'height\'];
return $image_data;
}
add_filter(\'wp_generate_attachment_metadata\',\'replace_uploaded_image\');