在 WordPress 宽高比中自动使用调整大小的图像而不是原始图像?

发布于 2024-11-08 05:44:41 字数 1160 浏览 0 评论 0原文

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

http://pastie.org/1928349

I would appriciate Your help. I used custom field in Wordpress and created the form to upload the image in the post. Everything works fine. I also placed this code to replace the original image ( if someone posted huge image size ) which automatically resizes it. It in fact resizes the image but it doesn't keep the aspect ratio meaning max 500px width, max 800px height. It takes that walues and becomes croped to that size. I want the height to be proportional not croped! this goes to 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-11-15 05:44:41

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

Correct me if I'm wrong, but you seem to be rewriting a function that already exists in Wordpress - http://codex.wordpress.org/Function_Reference/add_image_size

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文