我的代码现在是这样的:
$imgOrginalsize = getimagesize($file);
$ratio = $imgOrginalsize[0] / $imgOrginalsize[1];
$h = 160;
$w = 160;
$file_name = basename($file);
switch (strtolower($imgOrginalsize['mime'])) {
case 'image/jpeg':
$img = imagecreatefromjpeg($file);
$new = imagecreatetruecolor($w, $h);
imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $imgOrginalsize[0], $imgOrginalsize[1]);
imagejpeg($new, $pathToSave . $file_name, 100);
imagedestroy($new);
break;
case 'image/png':
$img = imagecreatefrompng($file);
$new = imagecreatetruecolor($w, $h);
imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $imgOrginalsize[0], $imgOrginalsize[1]);
imagepng($new, $pathToSave . $file_name, 100);
imagedestroy($new);
break;
default:
var_dump('error default');
exit();
}
现在拇指看起来不好,例如被压缩-原来的img是更高的oyu知道我的意思,我不需要在缩略图所有图片-我可以从左上角(或从原来的img中心)交叉,但我不想压缩或拉伸它: