The MU forums have moved to WordPress.org

Image file resize at upload.... (45 posts)

  1. palkat
    Member
    Posted 17 years ago #

    I have spent days and many hours on these forums and google looking for a way to hack the WORDPRESS MU so when you use the standard image uploader it would resize images if they are larger than say 1024x1024 to a new size. I have people that are uploading images to show in thier blogs but each photo is 1.5meg-2meg in size! Ouch! the physical display size is fine as it is resized via html but i would like a way like coppermine that if the image is X kbs it would be resize down when saved to the server automaticly.

    Does anyone have an answer? i looked at the image gallery programs and i would rather just keep with the built in image system that came with Wordpress MU and have that hacked to do the image file size, resizing.

    Can anyone help? Please?

  2. donncha
    Key Master
    Posted 17 years ago #

    This Google search brought up some likely bits of code you can use:
    http://www.google.com/search?q=php+gd+resize+image

    I wouldn't bother doing it because loading a large image and resizing it takes up a huge amount of memory and CPU which, unless you're Flickr and that's what you do, is probably better spent on serving text content.

  3. andrea_r
    Moderator
    Posted 17 years ago #

    I have users with the same issues, and what I think I'm going to do is modify the upload box with text to warn them of picture limits.

  4. palkat
    Member
    Posted 17 years ago #

    donncha, thanks for the leads!
    I do understand to impact on the cpu of size crunching but need this to happen regaurdless.

    I guess another question is:

    Where in the include-upload.php script is it writting the main uploaded image to the server? I just want to head that off with a resize before it write the file to the server. I already go the thumbnail down but can not seem to catch it when it FIRST write the uploaded file.

    Any input??

  5. donncha
    Key Master
    Posted 17 years ago #

    palkat - your guess is as good as mine, I haven't looked at that file in ages but it shouldn't be too hard to modify. If you come up with a nice way of resizing I'd love to see the patch in Trac!

  6. andrea_r
    Moderator
    Posted 17 years ago #

    Me too! I'd bake you cookies. :D

  7. palkat
    Member
    Posted 17 years ago #

    PUlling hair out! im so bad at PHP coding...i just cant find/figure out what variable i need to run through the resize code(like the resize code for the thumbnail) of the main file before it is written to the server.

  8. palkat
    Member
    Posted 17 years ago #

    Woo Hoo...got it <in a round about way>!
    Here is what i did. after i writes the uploaded file, then it creats the thumb, i then cloned the thumb process into another process that then re-renders the original file and overwirtes the huge original file.

    Example: you upload a 1024x800 size image. I set my code to say any images over say 640px wide reduce them to a new
    size of 640px wide (perportioned). so what it does is saves the uploaded file like normal, then creates the thumbnail like normal, then it checks to see if the uploaded file is wider than 640px if not it is done, if it IS wider than 640px it will rerender it to 640px wide and save it over the original. tested and worked. im sure someone will find some fine tuning or corrections but for what we need for now it is a great temporary but working fix. here is the code:

    in /wp-admin/admin-functions.php:

    just before:
    function has_meta($postid) ....

    (about line 923ish) past this in:
    --------------------------------------------------

    function wp_create_smallerimage($file, $max_side, $effect = '') {

    // 1 = GIF, 2 = JPEG, 3 = PNG

    if (file_exists($file)) {
    $type = getimagesize($file);

    // if the associated function doesn't exist - then it's not
    // handle. duh. i hope.

    if (!function_exists('imagegif') && $type[2] == 1) {
    $error = __('Filetype not supported. Thumbnail not created.');
    }
    elseif (!function_exists('imagejpeg') && $type[2] == 2) {
    $error = __('Filetype not supported. Thumbnail not created.');
    }
    elseif (!function_exists('imagepng') && $type[2] == 3) {
    $error = __('Filetype not supported. Thumbnail not created.');
    } else {

    // create the initial copy from the original file
    if ($type[2] == 1) {
    $image = imagecreatefromgif($file);
    }
    elseif ($type[2] == 2) {
    $image = imagecreatefromjpeg($file);
    }
    elseif ($type[2] == 3) {
    $image = imagecreatefrompng($file);
    }

    if (function_exists('imageantialias'))
    imageantialias($image, TRUE);

    $image_attr = getimagesize($file);

    // figure out the longest side

    if ($image_attr[0] > $image_attr[1]) {
    $image_width = $image_attr[0];
    $image_height = $image_attr[1];
    $image_new_width = $max_side;

    $image_ratio = $image_width / $image_new_width;
    $image_new_height = $image_height / $image_ratio;
    //width is > height
    } else {
    $image_width = $image_attr[0];
    $image_height = $image_attr[1];
    $image_new_height = $max_side;

    $image_ratio = $image_height / $image_new_height;
    $image_new_width = $image_width / $image_ratio;
    //height > width
    }

    $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
    @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);

    // If no filters change the filename, we'll do a default transformation.
    if ( basename($file) == $thumb = apply_filters('thumbnail_filename', basename($file)) )

    //original code to rename the picture to ".thumbnail.xxx" , i removed the '.thumbnail' to make this overwrite the original file.
    //$thumb = preg_replace('!(\.[^.]+)?$!', __('.thumbnail').'$1', basename($file), 1);
    $thumb = preg_replace('!(\.[^.]+)?$!', __('').'$1', basename($file), 1);

    $thumbpath = str_replace(basename($file), $thumb, $file);

    // move the thumbnail to it's final destination
    if ($type[2] == 1) {
    if (!imagegif($thumbnail, $thumbpath)) {
    $error = __("Thumbnail path invalid");
    }
    }
    elseif ($type[2] == 2) {
    if (!imagejpeg($thumbnail, $thumbpath)) {
    $error = __("Thumbnail path invalid");
    }
    }
    elseif ($type[2] == 3) {
    if (!imagepng($thumbnail, $thumbpath)) {
    $error = __("Thumbnail path invalid");
    }
    }

    }
    } else {
    $error = __('File not found');
    }

    if (!empty ($error)) {
    return $error;
    } else {
    return $thumbpath;
    }
    }
    --------------------------------------------------
    now in file /wp-admin/inline-uploading.php
    just before:
    } else {
    add_post_meta($id, '_wp_attachment_metadata', array());
    }
    (About line 95ish) paste the following:
    --------------------------------------------------
    // this sets the size of how big an image MUST BE to reduce the original to new render size
    if ( $imagedata['width'] > 640 ) {

    // $max is the width of the new rendered image
    $max = 640;

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

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

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

    if you want your rerender image to be bigger than the default of 640wide then change the 640 in the following lines to the set width you want on the above code:

    // this sets the size of how big an image MUST BE to reduce the original to new render size
    if ( $imagedata['width'] > 640 ) {

    // $max is the width of the new rendered image
    $max = 640;

  9. palkat
    Member
    Posted 17 years ago #

    BTW:andrea_r

    You still good for the cookies?!??! ;-) LOL

  10. modifiedcontent
    Member
    Posted 17 years ago #

    Is there no working plugin for this? It's a pretty essential element when you want to build a public, idiot-proof community site. A warning text for picture size limit won't do it.

  11. modifiedcontent
    Member
    Posted 17 years ago #

    Anyone know if this is being worked on?

  12. drmike
    Member
    Posted 17 years ago #

    A bunch of responses come to mind but I'll just suggest that you read the posts up above as the solution is posted there.

  13. modifiedcontent
    Member
    Posted 17 years ago #

    Thanks, I see a lot of code above that I can apparently paste into other files. I will try that. Don't I need some kind of image editing library installed for that to work?

    But there's no plugin for these kind of functions? Will the code above produce a clickable thumbnail with the original in a popup?

    Not that I necessarily need that. Maybe a simpler, sturdy solution baked into the code makes more sense. I don't know...

  14. noisyscott
    Member
    Posted 17 years ago #

    Palkat,

    Thanks for the great workaround. I love being able to throw images up and have the server do all the resizing. Nice and efficient.

    I am however having a problem with thumbnails; they are no longer being created. The strange thing is that after logging out then back in after applying your code changes to admin-functions.php and inline-uploading.php thumbnails were created. Each subsequent upload, logout/login try has however not produced thumbnails - all I get are resized originals.

    Anyone elese seeing this?

  15. andrea_r
    Moderator
    Posted 17 years ago #

    "Will the code above produce a clickable thumbnail with the original in a popup?"

    There isn't a popup function for pics in WP - either with this code or without.

    noisyscott, I tried this on my sytem - even logged out and logged in as a different user - and it worked as expected.

  16. noisyscott
    Member
    Posted 17 years ago #

    Well I found out what my problem was; my new camera's resolution was larger than wp-admin/inline-uploading.php allowed. I was originally testing this on some older/smaller images and of course encountered no problem. The issue is that wp-admin/inline-uploading.php (line 87 in 2.0.4) will abort thumbnail creation if the image is greater than 3*1024*1024. All I had to do was change that to 4*1024*1024 to fix the problem.

  17. ilg
    Member
    Posted 17 years ago #

    about palkat hack...
    my line where this "function has_meta($postid)" in on line 793. Original 2.0.4 file.

    I have inserted the piece of script

    result... the upload panel doens't show in admin > write post/page

  18. seandfunk
    Member
    Posted 17 years ago #

    is there a way of setting the qulaity for the jpg ?

    it seems to be compressing somewhere as well as resizing.

  19. dsader
    Member
    Posted 17 years ago #

    palkat, andrea_r, noisyscott

    Try ibox. Put the ibox files in the themes folder instead of every individual theme folder. Add the script call to wp-header. All that's needed is a re="ibox" added to image tags.

    Drool-worthy images. Yummy.

    dsader

  20. tnts
    Member
    Posted 17 years ago #

    hi,
    the code works great with for exsample 1024x768 pictures, but if i try to upload 2592x1944 picture straight from camera, the upload "window" gives error: "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2560 bytes) in ../wp-admin/admin-functions.php on line 850"

    edit: I found quite nice MS Power Toy for resizing images to use before uploading with WP. of course it's one more click too much, but got to live with it. here's the link

    I guess the problem is in the server i'm using? Any way to get it work without changing the server host?

  21. thedesigncoalition
    Member
    Posted 17 years ago #

    Palkat (or anybody)

    Does anybody know where to add the second part of the code now that there is no longer an inline-uploading.php in the latest version of wp?

  22. andrea_r
    Moderator
    Posted 17 years ago #

    I'm trying to find that out myself. :-/ I just discovered this wasn't working.

  23. andrea_r
    Moderator
    Posted 17 years ago #

    In the wp_upload_tab_upload_action function (in wp-admin/upload-functions.php) after this line:

    $imagedata = wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

    add the following

    	//image resizing
    	// this sets the size of how big an image MUST BE to reduce the original to new render size
    $imagesize = getimagesize($file);
    $imagedata = array();
    $imagedata['width'] = $imagesize['0'];
    $imagedata['height'] = $imagesize['1'];
    
    if ( $imagedata['width'] > 640 ) {
    
    // $max is the width of the new rendered image
    $max = 640;
    
    if ($imagedata['height'] > $imagedata['width']) {
    $aspect = $imagedata['width']/$imagedata['height'];
    $max = $max / $aspect;
    }
    
    $thumb = wp_create_smallerimage($file,$max);
    
    if ( @file_exists($thumb) ) {
    $newdata = $imagedata;
    $newdata['thumb'] = basename($thumb);
    update_post_meta($id, '_wp_attachment_metadata', $newdata, $imagedata);
    } else {
    $error = $thumb;
    }
    }
    

    Change the 640's above to whatever you want your max dimension to be.

    Ron (for Andrea)

  24. andrea_r
    Moderator
    Posted 17 years ago #

    Also note we had to change bits of that second part of code.

  25. suleiman
    Member
    Posted 17 years ago #

    i wonder why this isn't in trac yet? So many themes are broken by users uploading massive pictures.

  26. tmuka
    Member
    Posted 17 years ago #

    thanks for this code, it was very helpful to me!

  27. andrea_r
    Moderator
    Posted 17 years ago #

    I'll have to submit it to trac sometime and see if it gets added, i guess.

  28. andcanitbe
    Member
    Posted 16 years ago #

    Excellent script guys, works brilliantly. Just a note on the image quality, simply put a value in this line:

    if (!imagejpeg($thumbnail, $thumbpath)) {

    e.g

    if (!imagejpeg($thumbnail, $thumbpath, 100)) {

    100 being top quality 99 a little less etc etc

  29. Ovidiu
    Member
    Posted 16 years ago #

    just to add to this conversation:

    I was always happy using the imagemanager plugin, but now I found a plugin that fits in better: http://blog.japonophile.com/flexible-upload/

    although: I HAVE NOT TESTED IT IN WPMU but I think its better because it integrates its features into the wp upload tab.

    AND if you modify it, I guess it would be possible to enforce a certain max. size of pictures sitewide., maybe someone gives it a try as I don't have any time left for the time being.

  30. honewatson
    Member
    Posted 16 years ago #

    Any updates on flexible-upload for mu?

About this Topic

  • Started 17 years ago by palkat
  • Latest reply from photolord