Aug 18

By default timthumb offers a cropping starting from center middle. Sometimes you may need some flexibility with a script so we used our knowledge to modify it a bit and make it more functionak for our needs. There are approx. 30 lines of code to add to make it work (see below). This supposed to make the use of plugins based on timthumb more flexible.By default timthumb offers a cropping starting from center middle. Sometimes you may need some flexibility with a script so we used our knowledge to modify it a bit and make it more functionak for our needs. There are approx. 30 lines of code to add to make it work (see below). This supposed to make the use of plugins based on timthumb more flexible.

Also an edited version is available for download.

Added “cropfrom” filter, accepted are values:

1. middle,

2. middleleft,

3. middleright,

4. topleft,

5. topright,

6. topcenter,

7. bottomleft,

8. bottomright,

9. bottomcenter,

simple modification done in 1 place on timthumb.php – easy for any level.

If you want to edit an original timthumb code:

1. Download recent timthumb version.

Read all about it and it’s usage if you not familiar with it yet.

2. Open it in text editor, and:

find: (line 59)

$quality         = preg_replace(“/[^0-9]+/”, “”, get_request(“q”, 80));

$filters        = get_request(“f”, “”);

add below:

// get crop position – added by studio-xl.com

$crop_from = get_request( ‘cropfrom’, ‘middle’ ) ;

// get crop position – added by studio-xl.com

find: (line 160)

imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );

REPLACE with:

// Crop position – modification by Studi-XL.com

if($crop_from ==”middle” )

// crop from center

{ imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h ); }

// crop from middle left

elseif ($crop_from ==”middleleft” )

// crop from middle right

{ imagecopyresampled( $canvas, $image, 0, 0, 0, $src_y, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”middleright” )

// crop from top center

{ imagecopyresampled( $canvas, $image, 0, 0, 2*$src_x, $src_y, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”topcenter” )

// crop from top center

{ imagecopyresampled( $canvas, $image, 0, 0, $src_x, 0, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”topleft” )

// crop from top left

{ imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”topright” )

// crop from top center

{ imagecopyresampled( $canvas, $image, 0, 0, 2*$src_x, 0, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”bottomcenter” )

// crop from bottom center

{ imagecopyresampled( $canvas, $image, 0, 0, $src_x, 2*$src_y, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”bottomleft” )

// crop from bottom left

{ imagecopyresampled( $canvas, $image, 0, 0, 0, 2*$src_y, $new_width, $new_height, $src_w, $src_h ); }

elseif ($crop_from ==”bottomright” )

// crop from bottom center

{ imagecopyresampled( $canvas, $image, 0, 0, 2*$src_x, 2*$src_y, $new_width, $new_height, $src_w, $src_h ); }

elseif  (!$crop_from)

// no data

{ imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h ); }

// Crop position – modification by Studi-XL.com

* You may delete any block that you don’t need or leave it all for a max. flexibility.

3. Usage:

by default timthumb query string looks something like this:

src=/images/whatever.jpg&h=150&w=150&zc=1

we added 1 more filter which tells timthumb where to position cropping, you need to add &cropfrom=[position] to it, for example:

src=/images/whatever.jpg&h=150&w=150&zc=1&cropfrom=topleft

If you want an already updated file

You may download a modified ver. This one is fully working and used in our WP blogs.

Dowload edited timthumb.php

Demo of all positions

demo link

15 Responses to “Timthumb cropping MOD”

  1. rafael Says:

    Perfect! I was looking much for some like this. Tks guys

  2. PSD to HTML Says:

    Very nice! I had been looking for anything on how to change the cropping feature of TT and you’ve done it!! AWESOME WORK!! Greatly appreciated.

  3. Indicar la posición del Crop con Timthumb | Summarg Says:

    [...] fotografías de plano americano, por ejemplo, nos decapitará al objetivo. Por ello la gente de Studio-XL desarrolló una modificación para indicarle a Timthumb cómo debe [...]

  4. Gustavo Says:

    Thanks a lot for this mod, I was looking for such options, witch actually make it easy to be modified into what can then be controlled from the admin to choose the crop option. I just manually applied your mod to the timthumb script I was using, per your instructions and tested it, and works like a charm ..and thanks for the clear instructions of “find this”…replace with”. Mad props!

  5. johan Says:

    Awesome! Please note that the thumbnails in the downloadable version become PNG’s instead of JPG’s. Might not look the same in different browsers and the filesize is a little havier.

  6. johan Says:

    I can’t seem to set the filetype of the thumbs to JPEG. Which lines do I have to change?

  7. TimThumb, script PHP para redimensionar y optimizar imágenes | Nosolocodigo Says:

    [...] la imagen menos apropiada. Para evitar esto Studio-XL ha desarrollado una modificación del script. Se puede descargar desde su sitio. Su script funciona exactamente igual, pero añade la opción de especificar el parametro [...]

  8. Todd Says:

    Hi, after uploading the new .php file, where to I go to add the modifiers to the imgsrc? I tried adding it in the thumbnail field in wordpress, and that worked, but it broke the lightbox popup.

  9. cj Says:

    any way to change the default thumbnail size like change it to 200×200 instead of 15×150 ?

    thanks

  10. TimThumb, script PHP para redimensionar y optimizar imágenes | {WebProgramo.com} Says:

    [...] la imagen menos apropiada. Para evitar esto Studio-XL ha desarrollado una modificación del script. Se puede descargar desde su sitio. Su script funciona exactamente igual, pero añade la opción de especificar el parámetro [...]

  11. Jonny Says:

    Hi,

    I installed as per the instructions however it still doesn’t work. Do I need to change the custom field “post_thumb” as well? I have edited all mentions of the “src=/images/whatever.jpg&h=150&w=150&zc=1″ part of the code in all of the php files of my template and added the “&cropfrom=topleft” to each of these mentions, but it still doesn’t seem to work…

    Any idea?

    Thanks,

    Jonny

  12. Saree Says:

    I was looking for this code for last few days. Thanks and keep it up.

  13. joanie Says:

    Hi there

    Thanks for the modified code. I uploaded it and everything looks the same. At least nothing broke, that’s progress! BUT i want to have all the images cropped TOP CENTER, and don’t know what to do with this line from your example above:

    src=/images/whatever.jpg&h=150&w=150&zc=1&cropfrom=topleft

    Where do i put that bit of code modified to
    src=/images/whatever.jpg&h=150&w=150&zc=1&cropfrom=topcenter
    ???

    Is there a diff php file i need to change?

    Thanks sooo much!

    J

  14. Xakep Says:

    joanie, hi
    Yes, you need to download the updated php file for timthumb above, or update your existing as shown in article.

    And you put this code:
    src=/images/whatever.jpg&h=150&w=150&zc=1&cropfrom=topcenter

    whenever and wherever you call to the image (img src=…)

  15. krishna Says:

    /* This is for wordpress */

    <img src="/timthumb.php?src=ID, $key, true); ?>&h=175&w=189&zc=1&cropfrom=topcenter&” style=”border: none;” />

    /* This is for wordpress */

Leave a Reply

Notify me of follow-up comments via email.