在Wordpress纵横比中自动使用已调整大小的图像而不是原件?

时间:2011-05-19 20:08:31

标签: image wordpress forms image-processing aspect-ratio

我会恭喜你的帮助。我在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');

http://pastie.org/1928349

1 个答案:

答案 0 :(得分:1)

如果我错了,请纠正我,但您似乎正在重写Wordpress中已存在的函数 - http://codex.wordpress.org/Function_Reference/add_image_size