我会感谢你的帮助。我在 Wordpress 中使用了自定义字段并创建了表单以在帖子中上传图片。一切正常。我还放置了这段代码来替换原始图像(如果有人发布了巨大的图像大小),它会自动调整它的大小。它实际上调整了图像的大小,但它没有保持纵横比,即最大 500 像素宽度,最大 800 像素高度。它需要这个值并被裁剪到那个大小。我希望高度成比例而不是裁剪!这转到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');