您需要使用 PHP图像魔术师 or GD处理图像的函数。
以 GD 为例,它就像这样简单......
function resize_image($file, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($file);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
你可以调用这个函数,就像这样......
$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);
从个人经验来看,GD 的图像重采样也确实显着减小了文件大小,尤其是在对原始数码相机图像进行重采样时。