Help with a plugin on MU

Hi, I threw the following code into a .php file with the requisite meta to make it work as a plugin. It's just code I found a while back and have used on a bunch of site. It's throwing errors occasionally when the image is over a certain resolution or file size (not sure which). I'm wondering if anyone who's better at code/MU than myself who can give me some idea of why it's doing this?

EDIT: sorry I forgot to explain. This code renames the large size image to the same as the original, fullsize, so that it replaces it. This means you can set the large size under Settings>Media and that will become the fullsize image. It's super helpful in many cases.

Here's the code:

<?php
/*
Plugin Name: Resize Fullsize Image
Plugin URI: http://fiddlerstudios.com/
Description: Resizes the full-size image to the "large" size defined under Settings>Media.
Version: 0.1
Author: Unknown
Author URI: http://fiddlerstudios.com
License: GPL2
*/

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
	/* limits image size to large setting */
function replace_uploaded_image($image_data) {

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

?>

Here's the error:

Warning: rename(/home/mainsite/public_html/wp-content/blogs.dir/7/files/2012/02/test-name-800x600.jpg,/home/fiddlers/public_html/wp-content/blogs.dir/7/files/2008/10/test-name.jpg) [function.rename]: No such file or directory in /home/fiddlers/public_html/wp-content/plugins/resize-image-plugin.php on line 27
601
  • Tevya

    Okay, I looked into this further. It works fine on one of the other sites on this same Multisite install. So I know it's got to be related to this specific theme/site.

    If you look at the Warning:

    Warning: rename(/home/fiddlers/public_html/wp-content/blogs.dir/7/files/2012/02/DSCF2897-450x600.jpg,/home/fiddlers/public_html/wp-content/blogs.dir/7/files/2008/10/DSCF2897.jpg) [function.rename]: No such file or directory in /home/fiddlers/public_html/wp-content/plugins/resize-image-plugin.php on line 27
    687

    You'll notice that the file that's being renamed has a different path than where it's being renamed to. You'll notice it's "/2012/02/" in the first and "/2008/10/" when being renamed.

    EDIT: I checked the file path, and it's being uploaded to the "2008/10/" folder, which it shouldn't be. It should be uploaded to the current month "2012/02/". Then when it goes to rename it in "2012/02/", it can't because the file is not there.

    I tried turning off date-based folder organization for that site, and it works just fine, no problems. However, I really prefer to have that organization in place. Any of you WPMUDEV geniuses have any idea how I can make this work? I'd really appreciate it. In fact, I'm surprised that a lot more people don't use this plugin to limit the file size of image uploads on Multisite.

  • Tevya

    Okay, one more update. I figured it out. Since I migrated this site from a single-site install, it has old pages/posts. The page I was working on was from 2008/10 so that's where the image was getting uploaded to. However the plugin assumed it was the current month (I guess). So when it would go to rename the file in 2012/02 it wasn't there. Simply updating the publish date of the page to the current month, fixed the problem.

    Anyone know if there's some way I can modify the code to look in the correct folder if it's an older post/page?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.