How do I resize/access original images in wordpres

2019-02-20 12:30发布

问题:

Where do the images resize in wordpress? In a database? In a folder?

How would I go about resizing the original image (not create a new version). I ask this, because I uploaded quite a few images that are too large and slow down load times on a wordpress site, and I want to resize them so there aren't any dimensions larger than 1500px.

I've already looked at a few plugins, including "regenerate thumbnails", but there may be one that actually does what I want that I haven't been able to find yet.

回答1:

Well, the images resize "on the fly", then stored in the server and its information recorded in the database.

The original remains and all "thumbnails" are generated. In this case, "thumbnails" refers to all WP generated image sizes, big or small.

I answered the same question at WordPress StackExchange. And the solution comes from this article: How to automatically use resized images instead of originals.

This script will replace the uploaded image (if bigger than the larger size defined in your settings) by the large image generated by WordPress to save space in your server, and save bandwidth if you link a thumbnail to the original image, like when a lightbox plugin is used.

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'];

    // 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;
}


回答2:

You can use WordPress' add_theme_support and add_image_size to add multiple sizes for an image, which means that WordPress will create a copy of the post thumbnail with the specified dimensions when you upload a new thumbnail. To learn more about these technics you can read this tutorial.

There is also another function in WordPress and it's image_resize that can be used to scale down an image to fit a particular size and save a new copy of the image.

Most developers uses add_image_size to add multiple image size to show in different palces, i.e. you can use one image as a featured image in your home page and also can use another size of same image in the single.php page. To do this you have to use

add_theme_support( 'post-thumbnails' );
add_image_size( 'homepage-thumb', 220, 180 ); // 220 pix width,180 pix height
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

To show the homepage-thumb image in your home page you can use

if ( has_post_thumbnail() ) { the_post_thumbnail( 'homepage-thumb' ); }

Or in the single.php template you can use

if ( has_post_thumbnail() ) { the_post_thumbnail( 'singlepost-thumb' ); }

Also you can take a look at this plugin and this article could be useful too. Hope it helps.



回答3:

To my knowledge there is no plugin that will directly resize your original images and change their meta data, but I've found a workaround (I had a similar problem to yours) which does not require coding.

  1. Download your images by FTP (images are stored within the wp-content/uploads/ directory)
  2. Make your resize/editing operations with the software of your choice and re-upload your images using FTP exactly to the same place they were before (you should not change anything in the folder structure or filenames)
  3. Install WPR Rebuild Meta Data plugin in your Wordpress Admin. Go to "Tools -> Rebuild Meta". This will rebuild all the modified image meta data (for example it will update the new image sizes into the database)
  4. Install Force Regenerate Thumbnails plugin in your Wordpress Admin. You can use it from "Tools -> Force Regenerate Thumbnails" to do it for all your images, or from the Wordpress Media Manager to do it just for the images you've changed. This will delete the old thumbnails and custom image sizes, and create fresh ones from your new images.