Ili koristiš ImageMagick koji je besplatan, pravi mnogo bolji kvalitet kada se resize-uje (mada za 32 x 32 i nije toliko važno jer se tu ionako ništa ne vidi) i lako se koristi.
Za gd, evo ti jedan kod koji lista sadržaj foldera i radi ono što tebi treba (ovo sam ja onomad nešto testirao, radi ali ne garantujem za svaki slučaj jer sam čisto probao):
thumb.php:
Code:
<?php
$jpegDir = isset($folder) ? $folder : ""; // folder to copy
$destImg = isset($dest_img) ? $dest_img : ""; // destination image
$picMaxW = isset($picMaxW) ? $picMaxW : 100; // maximum width
$picMaxH = isset($picMaxH) ? $picMaxH : 100; // Maximum height
$jpeg = $jpegDir .'/'. $img;
$destJpeg = $jpegDir .'/'. $destImg;
if ($d=getimagesize($jpeg) ){
$picW = $d[0];
$picH = $d[1];
$percentW = $picMaxW / $picW;
$percentH = $picMaxH / $picH;
$percent = ($percentW < $percentH) ? $percentW : $percentH;
$sizeW = intval($picW * $percent);
$sizeH = intval($picH * $percent);
header('Content-type: image/jpeg');
$src = imagecreatefromjpeg($jpeg);
$dst = imagecreate($sizeW, $sizeH);
$white = imagecolorallocate($dst,255,255,255); // background color
// imagefill ($dst,0,0,$white);
// imagecopyresampled($dst,$src, 0, 0, 0, 0, $sizeW, $sizeH, $picW, $picH);
imagecopyresized ($dst, $src, 0, 0, 0, 0, $sizeW, $sizeH, $picW, $picH);
// imagejpeg($dst, $destJpeg); // to create file on disk and replace old file
imagejpeg($dst);
imagedestroy($dst);
imagedestroy($src);
return true;
}
return false;
?>
Bolji kvalitet daje ...resampled... nego ...resized... ali je moraš da imaš noviju verziju GD (u vreme kada sam ja ovo testirao, na većini komercijalnih servera nije postojala, pretpostavljam da je sada ima)
Kao što vidiš, ako hoćeš da pišeš thumbnails u file, skloni komentar ispred linije
// imagejpeg($dst, $destJpeg); // to create file on disk and replace old file
example (računa se da su sve slike u podfolderu "images"):
Code:
<html>
<head>
<title>Thumbnail create test</title>
</head>
<body>
<?php
if($dir=opendir('images')){
while($file=readdir($dir)){
if('.'==$file || '..'==$file) continue;
if(is_dir('images/'.$file)) continue;
echo "\r\n";
// printf('<a href="images/%s"><img src="thumb.php?img=%s" border="0" alt="%s"></a>', $file, $file, $file);
printf('<a href="images/%s"><img src="thumb.php?img=%s" border="0" alt="%s"></a>', $file, $file, $file);
}
closedir($dir);
}
?>
</body>
</html>
Goran Pilipović fka bluesman