简体   繁体   English

上传时自动将Wordpress图像调整为最大宽度和高度?

[英]Automatically resize Wordpress images to a maximum width and height upon uploading?

I created a blog where some users can upload images through the Wordpress dashboard. 我创建了一个博客,其中一些用户可以通过Wordpress仪表板上传图像。 The site gets bogged down quickly because the original images are so big. 由于原始图像太大,网站很快陷入困境。 Some users don't have the knowledge to resize the images themselves before uploading them, and I don't want to have to resize them manually. 有些用户在上传图片之前没有自己调整图片大小的知识,我不想手动调整图片大小。

Is there any way I can set a maximum width and height for uploaded images? 有什么办法可以为上传的图像设置最大宽度和高度吗? I don't even want the original to remain on the website. 我甚至不希望原件留在网站上。 I want the largest version of the image on the website to match the width and height restrictions I set. 我希望网站上最大的图像版本与我设置的宽度和高度限制相匹配。

add this code in your theme's functions.php it will replace the original image with the the re-sized version. 在主题的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');

Article Source: http://goo.gl/nkszUn 文章来源: http//goo.gl/nkszUn

Well why dont you create a new image size? 那你为什么不创建一个新的图像大小? http://codex.wordpress.org/Function_Reference/add_image_size http://codex.wordpress.org/Function_Reference/add_image_size

And use that image on your templates. 并在模板上使用该图像。

This will work for new image uploads as well as older ones replacing user uploaded large images automatically with your defined Large size from admin panel's media settings : 这适用于新的图片上传以及较旧的图片上传,用管理员面板的媒体设置自定义大尺寸替换用户上传的大图片:

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
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']; // ** This only works for new image uploads - fixed for older images below.
    $current_subdir = substr($image_data['file'],0,strrpos($image_data['file'],"/"));
    $large_image_location = $upload_dir['basedir'] . '/'.$current_subdir.'/'.$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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM