- 上传后仅存储图像库名称到数据库,例如。产品1.jpg
然后在不同的目录中创建具有相同名称的拇指..
上传文件 - uploads/product1.jpg
小拇指 - uploads/thumbs/small/product1.jpg
这是一个在 php 中创建拇指的函数
function createThumb($filepath, $thumbPath, $maxwidth, $maxheight, $quality=75)
{
$created=false;
$file_name = pathinfo($filepath);
$format = $file_name['extension'];
// Get new dimensions
$newW = $maxwidth;
$newH = $maxheight;
// Resample
$thumb = imagecreatetruecolor($newW, $newH);
$image = imagecreatefromstring(file_get_contents($filepath));
list($width_orig, $height_orig) = getimagesize($filepath);
imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newW, $newH, $width_orig, $height_orig);
// Output
switch (strtolower($format)) {
case 'png':
imagepng($thumb, $thumbPath, 9);
$created=true;
break;
case 'gif':
imagegif($thumb, $thumbPath);
$created=true;
break;
default:
imagejpeg($thumb, $thumbPath, $quality);
$created=true;
break;
}
imagedestroy($image);
imagedestroy($thumb);
return $created;
}
参数详情
$filepath // uploaded file path include name eg. uploads/product1.jpg
$thumbPath // thumbpath with name eg. uploads/thumbs/small/product1.jpg
$maxwidth // width for the thumb eg. 100
$maxheight// height for the thumb eg. 60
$quality=75 // quality for the thumb image 1 - 100 ...by default it is 75