Search code examples
phpimageresizedimensionstimthumb

Extract new image dimensions from timthumb


I'm using timthumb to resize my images because it scales them nicely if I only enter one of the dimensions. However I want to know if it's possible to extract the new resized image's dimensions so that I can add that dynamically to the img tag attributes.

I tried this with no luck:

$fullpath = '/lib/timthumb.php?src='.$image.'&w=100';
$my_image = array_values(getimagesize($fullpath));
list($width, $height, $type, $attr) = $my_image;

Any ideas?


Solution

  • While I'm not sure how to read the thumb generated in the cache by timthumb directly, this should work:

    $fullpath = '/lib/timthumb.php?src='.$image.'&w=' . $newWidth;
    
    #array with 0 being the width, and 1 being the height, and some other values as well
    $oldDims = getimagesize($image);
    
    #timthumb uses floor(), so mimicing here
    $newHeight = floor($newWidth / $oldDims[0] * $oldDims[1]);