0

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?

jonthoughtit
  • 165
  • 1
  • 17

2 Answers2

3

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]); 
ernie
  • 6,356
  • 23
  • 28
  • I'm testing this out and so far it seems to be doing exactly as I need. Will follow up and let you know after further testing. Thanks! – jonthoughtit Sep 21 '12 at 22:54
  • It should work, since it's basically the exact same calculation timthumb is doing ;) – ernie Sep 21 '12 at 22:55
-1

My idea would be to scale the images before they are uploaded to the server. If you have a lot of images resizing them this way is an expensive operation. Also you don't need timthumb php can do this on it's own. So if you really need to resize images on the server try something like...

$size = gewtimagesize($image);
$dimensions = $size['3'];//returns height="xxx" width="xxx"
$img = <<<IMG
    <img src="path/to/img" $dimensions >
IMG;
echo $img;

I don't know if it works, but it looks right to me

--EDIT-- Nevermind this won't work for you

noel
  • 2,257
  • 3
  • 24
  • 39
  • He's using timthumb for that right? I was answering his question " 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." – noel Sep 21 '12 at 21:22
  • `$image` is the old, original image. The issue is that timthumb dynamically generates an image in cache, so he can't use `getimagesize()` as he doesn't have the file path to the new image . . . – ernie Sep 21 '12 at 21:24