あなたの助けに感謝します。WordPress のカスタム フィールドを使用して、投稿に画像をアップロードするためのフォームを作成しました。すべて正常に動作します。また、このコードを配置して、元の画像 (誰かが巨大な画像サイズを投稿した場合) を置き換え、自動的にサイズを変更しました。実際には画像のサイズを変更しますが、最大幅 500px、最大高さ 800px を意味するアスペクト比を維持しません。そのワルーを取り、そのサイズにトリミングされます。高さをトリミングではなくプロポーショナルにしたい! これは functions.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'];
$large_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['large']
['file'];
// delete the uploaded image
unlink($uploaded_image_location);
// rename the large image
rename($large_image_location,$uploaded_image_location);
// update image metadata and return them
$image_data['width'] = $image_data['sizes']['large']['width'];
$image_data['height'] = $image_data['sizes']['large']['height'];
unset($image_data['sizes']['large']);
return $image_data;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');