WordPress.org

Ready to get started?Download WordPress

Forums

Image Resizing Function - An Alternative to Timthumb (32 posts)

  1. MatthewRuddy
    Member
    Posted 1 year ago #

    Hey all. I've been working hard on updating my plugin the 'Easing Slider'. A fairly massive update is on the way, and being a slideshow plugin image resizing is always a hot topic.

    I had been using Timthumb (like a lot of people), but often spent quite a while looking for a WordPress based alternative. I came across the 'vt_resize' function, which initially I felt did the trick, until I found out it wouldn't upscale images (which was vitally needed). It wasn't a good enough solution.

    So I bit the bullet and decided to come up with my own function. It is heavily based on some of WordPress's internal resizing functions, except it crops identically to Timthumb itself.

    It also supports the upcoming WordPress 3.5 update (an alternative function is used via an 'if' statement), which gives us access to some handy image manipulation classes to ease the pain.

    So far after a bit of testing it appears to be working very well. I'm really happy with it. It saves the resized images in the WordPress uploads folder, as per usual. This is great, because not only are the images resized but they are static files, which is ideal.

    I've created a Github page for the function here. It is released under GNU general public license, so feel free to use it as you wish!

    http://matthewruddy.github.com/Wordpress-Timthumb-alternative/index.html

    There is only one drawback: it won't work with external images. It's just too complicated and slow to do so, and probably not worth the effort. If you need to resize an external image, save it to your computer and upload it to the WordPress Media Library. Then use it from there.

    That's it really. If anyone finds any bugs, then let me know and I'll get them sorted quickly!

    Now you've no excuse to use Timthumb at all!

  2. rabmcnab
    Member
    Posted 1 year ago #

    I just wanted to say thanks very much for this - I've just come across it and have been trying it out on 3.5. Absolutely fantastic, well done. The functionality of TimThumb using built in WordPress functions - grand work fellah!

  3. Dave Navarro, Jr.
    Member
    Posted 1 year ago #

    Very nice. And extremely useful.

    One problem I am having is that I need to resize images to a specific size (400x250) and crop if the height is greater than 250. However, I need to control the cropping (Top, Middle, Bottom). Next time you're fiddling with this, any change you can add support to control the cropping?

  4. Augusto
    Member
    Posted 1 year ago #

    Matthew, sorry about the fool question, but what's the best way to include these functions in my theme?
    Just make a "include_once resize.php", in my functions.php file?

    thanks!
    and congratulations for your great work!

  5. MatthewRuddy
    Member
    Posted 1 year ago #

    Thanks for the great responses, glad to hear people are getting good use of it :)

    @dnavarrojr, controlling the crop positioning is something that has been suggested before but have yet to implement. Simply haven't had the time I'm afraid. Definitely going to look into it when I can!

    @gustao, yes, that's all it takes. Simply include the file or paste its contents into your themes functions.php file. Either will work nicely!

  6. styledev
    Member
    Posted 1 year ago #

    @MatthewRuddy - Thanks for the great code! I have forked it and sent you a pull request. Just some small changes to reduce load on the DB.

  7. Augusto
    Member
    Posted 1 year ago #

    Matthew,

    any idea about this error (on a remote server)?

    Parse error: syntax error, unexpected ':' in /home/storage/bla/bla/bla/resize.php on line 45

    on my local mamp wordpress installations, everything works fine!
    Great work!!

    in time: PHP Version 5.2.17

  8. MatthewRuddy
    Member
    Posted 1 year ago #

    Could be syntax errors with an older version of PHP. Try changing lines 45 & 46 to this:

    $width  = ( $width === NULL ) ?  get_option( 'thumbnail_size_w' ) : $width;
    $height = ( $height === NULL ) ? get_option( 'thumbnail_size_h' ) : $height;
  9. Augusto
    Member
    Posted 1 year ago #

    You're right, Matthew!

    Again, thank you so much for your support and the great work!

  10. MatthewRuddy
    Member
    Posted 1 year ago #

    Glad to hear :) No problem at all!

  11. styledev
    Member
    Posted 1 year ago #

    I have added Positional Cropping, here is the updated if($crop) snippet: https://gist.github.com/styledev/5042132

  12. onaicul
    Member
    Posted 11 months ago #

    Hi,

    I have just updated my site to WP 3.5.1 (late, yes) and now no thumbnails are appearing with the new posts on the main page. There are images associated with the individual posts on their page, but the scripts are not generating thumbnails (or even a div for the thumbnail to go in) on the index page.

    Is there an update to this theme that we need for 3.5.1?

  13. styledev
    Member
    Posted 11 months ago #

    @onaicul - That is odd, I am running this code on multiple sites that are on WP 3.5.1.

    I would recommend turning on WP_Debug in wp-config.php and see if any errors are being thrown.

  14. MatthewRuddy
    Member
    Posted 11 months ago #

    @onaicul - This isn't a theme; it's a standalone function for resizing images using native WordPress functionality. Not sure if your theme has included it, but are you sure this is related?

    The functionality would have nothing to do with native WordPress thumbnails.

  15. onaicul
    Member
    Posted 11 months ago #

    Hello, thanks for your answers.
    The problem is with timthumb i think.
    You can see the last 5 new posts dont have picture... http://www.perfeita.net

  16. styledev
    Member
    Posted 11 months ago #

    Hey @onaicul - This thread is for an alternative to timthumb and not timthumb itself.

    You should go here: http://www.binarymoon.co.uk/projects/timthumb/

  17. AntonCa
    Member
    Posted 10 months ago #

    Hey!

    I would really like to use your script but unfortanetely it only gives me back 150x150px in size no matter what values i put in. It only seems to grab the get_option( 'thumbnail_size_w' ) values.

    Have tried it in the twentyeleven theme and the same thing happens there.

    Any idea why?

  18. MatthewRuddy
    Member
    Posted 10 months ago #

    Are you sure the dimensions are being entered correctly and as integers? Should work fine - no one has reported any issues like this.

  19. AntonCa
    Member
    Posted 10 months ago #

    Unfortunately, yes.

    The following:
    <?php
    $image_resized = matthewruddy_image_resize( 'http://localhost/bohus/wp-content/uploads/2013/06/bohus-1.jpg', 300, 200, true, false );

    echo $image_resized['width'];
    echo $image_resized['url'];
    ?>

    Gives me:
    150http://localhost/bohus/wp-content/uploads/2013/06/bohus-1-150x150.jpg

    I have disabled all the plugins and are using the twentyeleven theme.

  20. AntonCa
    Member
    Posted 10 months ago #

    Just did a clean install from the latest WordPress version and the problem remains. I'm running localhost Uniserver on my computer. Can it be problems with the server settings?

  21. MatthewRuddy
    Member
    Posted 10 months ago #

    Could be that your version of PHP doesn't include either the GD_Library or Imagemagick extensions. WordPress requires one of either for the resizing functionality to work.

    Can you confirm that you have these PHP packages installed?

  22. AntonCa
    Member
    Posted 10 months ago #

    I get this from phpinfo():

    GD Support enabled
    GD Version bundled (2.0.34 compatible)
    FreeType Support enabled
    FreeType Linkage with freetype
    FreeType Version 2.4.10
    GIF Read Support enabled
    GIF Create Support enabled
    JPEG Support enabled
    libJPEG Version 8
    PNG Support enabled
    libPNG Version 1.2.50
    WBMP Support enabled
    XPM Support enabled
    libXpm Version 30411
    XBM Support enabled

  23. MatthewRuddy
    Member
    Posted 10 months ago #

    Not sure what to suggest then. Hard to tell what could be going on from here.

    Try it from your webhost, and ensure the images you're using are larger than the specified dimensions - the script won't resize images smaller than the dimensions specified.

    Other than that, fairly clueless. Very sorry! Wish I could be of better assistance.

  24. AntonCa
    Member
    Posted 10 months ago #

    No problem Matthew! Thank you anyway! I will try it out on Mamp on mac and on my dev-server.

    I have some more questions that also might be of interest for future readers:
    Will this script increase loading time compared to using add_image_size?
    Are there any other disadvantages compared to add_image_size?

    And my main problem with add_image_size:
    Will the images reload after you edit them with WP's Media Editor (after manually cropping for example)?

    Thank you in advance and good work with the script. Can't believe that this isnt built in WP function.

  25. MatthewRuddy
    Member
    Posted 10 months ago #

    Regarding your questions:

    add_image_size won't resize any images added before the new image size is added, so you'd need to use a plugin such as Regenerate Thumbnails to refresh all of your image sizes. You'd also have to do this every time you change your image dimensions as well, which can become a pain when testing. Alternatively, this resizing script resizes the images on-the-fly, just like Timthumb. No need to refresh anything.

    On the very first page load, when the resized image has to be generated, yes there will be a slower load time, but after that it is as quick as if the function wasn't being used. Once the resized image is created, it's saved in the WordPress uploads directory as usual and loaded from there - no PHP resizing required after that first initial page load.

    If you alter the image sizes, the script will simply generate a new resized image. All resized images are deleted when the image itself is deleted from the Media Library, so nothing gets left behind.

    Hope that helps in answering your questions. Feel free to ask anymore if you have them :) Hope I've been clear!

  26. AntonCa
    Member
    Posted 10 months ago #

    Thank you Matthew! You have been absolutely clear.

    I can't get this to work though.
    I have tried this both on my local mamp server (mac) and on my webhost.
    The problem remains. I get 150x150 size no matter what i do. Too bad. Really looked forward to working with this script!

  27. MatthewRuddy
    Member
    Posted 10 months ago #

    Very sorry Anton, not sure what could be going wrong! Hope you can find an alternative solution :)

  28. sortofme
    Member
    Posted 9 months ago #

    I weirdly have the same issue. Whatever sizes I give the script, it always exports them 150x150
    see this here:
    http://d.screenagers.at/quivogne/archive/products/diskator-m-2/
    my code:

    <?php
    $imgwidth=364;
    $imgheight=240;
    $imgresized= matthewruddy_image_resize( $src, $imgwidth, $imgheight);
    ?>
    <img alt="<?php echo esc_attr($alt); ?>" src="<?php echo $imgresized['url']; ?>" />

  29. MatthewRuddy
    Member
    Posted 9 months ago #

    Very unusual! Haven't managed to figure this one out yet, very sorry!

  30. GraphicBum
    Member
    Posted 9 months ago #

    I think the issue with the 150x150 has to do with lines 45 and 46 again. By the looks of it the conditionals are set backward forcing the width and height to set as the default 150x150

Topic Closed

This topic has been closed to new replies.

About this Topic