从timthumb中提取新的图像尺寸

时间:2012-09-21 20:47:03

标签: php image resize dimensions timthumb

我正在使用timthumb来调整图像大小,因为如果我只输入其中一个尺寸,它会很好地缩放它们。但是,我想知道是否可以提取新调整大小的图像的尺寸,以便我可以动态地将其添加到img标记属性。

我试了这个没有运气:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

虽然我不确定如何直接读取timthumb在缓存中生成的拇指,但这应该可行:

$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]); 

答案 1 :(得分:-1)

我的想法是在将图像上传到服务器之前对其进行缩放。如果你有很多图像调整它们这种方式是一个昂贵的操作。另外你不需要timthumb php可以自己做这个。因此,如果您真的需要来调整服务器上的图像大小,请尝试类似......

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

我不知道它是否有效,但它对我来说是正确的

- EDIT-- 没关系,这对你不起作用