WordPress.org

Ready to get started?Download WordPress

Forums

How to edit inline-uploading.php to resize images to 480px max width? (7 posts)

  1. vanner
    Member
    Posted 8 years ago #

    I'm trying to edit inline-uploading.php (WP 2.0.1) to make the thumbmail images the script creates to resize to 480px max width for all images (also keeps with proportional height). Any ideas on how to do this?

  2. leflo
    Member
    Posted 8 years ago #

    I'm about to program a plugin for this, because this bothers me too. But i cannot say when it will be begun or even ready nor how to do it.

  3. RyeBrye
    Member
    Posted 8 years ago #

    Not a bad idea. I really want to add on some more functionality to the inline-upload to add an inline-snag to grab images I have hosted on another server (i.e. flickr, or somewhere else on the web) and then snag them to the blog server the same way it does when you upload images...

  4. sethneal
    Member
    Posted 8 years ago #

    Has there been any updates on this? I really need some easy solution like this as well... I've been searching these posts a lot and can't seem to find anything that resolves a simple issue like image (re)size upon upload... it would be really helpful...

    Thanks.

  5. eads
    Member
    Posted 8 years ago #

    Yeah, inline-uploading.php definitely is not poetic code. Who wrote that thing? Ugh.

    I have made a bit of progress, the main thing being that one can override what lives in that upload iframe:


    function new_inline() {
    return 'http://slashdot.org';
    }
    add_filter('uploading_iframe_src', 'new_inline');

    The start of a plugin...

  6. eads
    Member
    Posted 8 years ago #

    Okay, here's a solution, but a partial one. Certainly enough to start actually recreating inline-uploading.php in a sane way (as a plugin in a folder with multiple files that encapsulate the various functionality), and enough to handle the issue at hand.

    So you need a plugin of some variety that does this:


    function new_inline() {
    global $post_ID;
    $uploading_iframe_ID = (0 == $post_ID ? $temp_ID : $post_ID);
    $uploading_iframe_src = "ii-inline-uploading.php?action=view&post=$uploading_iframe_ID";
    return get_bloginfo('url') . '/wp-content/plugins/' . $uploading_iframe_src;
    }

    add_filter('uploading_iframe_src', 'new_inline');

    ii-inline-uploading.php is just inline-uploading.php copied into the plugins directory. You don't have to rename it. Anyway, now you can hack at this file (this is how you'd make it sane, if you were so inclined). For our purposes, we'll just hack at it in a somewhat unsophisticated way.

    First, you need to change the require_once(...) call at the top so that our new uploading file knows what from what. This line should now look like:


    define('ABSPATH','/path/to/wordpress');
    require_once(ABSPATH . 'wp-admin/admin.php');

    Now, you'll want to replace the if statement that starts if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) { (line 84 or thereabouts, ymmv). Basically, you just need to replace the logic with a different calculation scheme for resizing:


    if ( $imagedata['width'] * $imagedata['height'] < 3 * 1024 * 1024 ) {
    $max = 165;

    if ($imagedata['height'] > $imagedata['width']) {
    $aspect = $imagedata['width']/$imagedata['height'];
    $max = $max / $aspect;
    }

    $thumb = wp_create_thumbnail($file,$max);

    if ( @file_exists($thumb) ) {
    $newdata = $imagedata;
    $newdata['thumb'] = basename($thumb);
    update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
    } else {
    $error = $thumb;
    }
    }
    } ...

    Okay, one last thing, how does this scheme work? Well, you just set a max width ($max=165). If the image is taller than it is wide, then you have to scale the max because wp_create_thumbnail wants a "largest side". In the taller-than-wide scenario, that means the height will get scaled to your preferred max, and the width will be less. You adjust the max side variable by dividing by the aspect ratio (width * height). In fancy math terms, the inverse of the aspect ratio is the scaling factor which lets you adjust the height to achieve the desired width.

    Again, this is pretty much a brute force attack and there might be some problems, but it works, suggests some other possibilities, and doesn't require you to hack away at core WordPress files.

  7. makwak
    Member
    Posted 7 years ago #

    Has anyone worked out what would be the most useful image plugin in WP 2+?

    For example, I have a site to which several people will be contributing, half with no skills at images at all... so when they upload an image, I want the upload to resize the image to max 500pixels (or whatever). To be able to create thumbnails of consistant height or width would be cool too...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.