首先验证上传文件类型与大小,使用PHP的GD库实现图片上传后生成多种尺寸缩略图,支持等比缩放与居中裁剪两种模式,并可通过批量处理函数自动创建小、中、大等多规格缩略图用于不同场景展示。

上传图片并生成多种尺寸缩略图是Web开发中常见的需求,特别是在用户头像、商品展示等场景。PHP结合GD库或ImageMagick可以轻松实现图片上传、按比例缩放裁剪、压缩质量控制等功能。
1. 图片上传基础处理
确保表单使用enctype="multipart/form-data",接收文件后先验证类型和大小,再移动到临时目录。
示例代码:
\$uploadDir = 'uploads/'; \$allowedTypes = ['image/jpeg', 'image/png', 'image/webp'];if (\$_FILES['image']['error'] === UPLOAD_ERR_OK) { \$tmpName = \$_FILES['image']['tmp_name']; \$mimeType = mime_content_type(\$tmpName);
if (in_array(\$mimeType, \$allowedTypes)) { \$fileName = uniqid() . '_' . basename(\$_FILES['image']['name']); \$filePath = \$uploadDir . \$fileName; if (move_uploaded_file(\$tmpName, \$filePath)) { echo "上传成功:{\$filePath}"; } } else { echo "不支持的文件类型"; }}
立即学习“PHP免费学习笔记(深入)”;
2. 按比例生成缩略图(保持宽高比)
使用GD库创建等比缩放图像,避免变形。核心是计算新尺寸,保留原始比例。
关键步骤:
- 获取原图宽高:getimagesize()
- 根据目标尺寸计算缩放比例
- 创建透明背景的目标画布(适用于PNG/WebP)
- 使用imagecopyresampled()进行高质量重采样
function createThumbnail(\$srcPath, \$dstPath, \$targetWidth, \$targetHeight, \$quality = 80) {
list(\$origWidth, \$origHeight) = getimagesize(\$srcPath);
// 计算缩放比例
\$ratio = min(\$targetWidth / \$origWidth, \$targetHeight / \$origHeight);
\$newWidth = intval(\$origWidth * \$ratio);
\$newHeight = intval(\$origHeight * \$ratio);
// 创建目标画布
\$thumb = imagecreatetruecolor(\$targetWidth, \$targetHeight);
\$bg = imagecolorallocate(\$thumb, 255, 255, 255);
imagefill(\$thumb, 0, 0, \$bg);
// 载入源图像
switch (mime_content_type(\$srcPath)) {
case 'image/jpeg':
\$source = imagecreatefromjpeg(\$srcPath);
break;
case 'image/png':
\$source = imagecreatefrompng(\$source);
imagesavealpha(\$thumb, true);
imagealphablending(\$thumb, false);
break;
case 'image/webp':
\$source = imagecreatefromwebp(\$srcPath);
break;
default:
return false;
}
// 居中粘贴缩略图
\$x = (\$targetWidth - \$newWidth) / 2;
\$y = (\$targetHeight - \$newHeight) / 2;
imagecopyresampled(\$thumb, \$source, \$x, \$y, 0, 0,
\$newWidth, \$newHeight, \$origWidth, \$origHeight);
// 保存并释放内存
imagejpeg(\$thumb, \$dstPath, \$quality);
imagedestroy(\$thumb);
imagedestroy(\$source);}
立即学习“PHP免费学习笔记(深入)”;
3. 裁剪式缩略图(固定尺寸居中截取)
适用于头像或需要统一尺寸的展示区域。先等比缩放到大于目标尺寸,再从中心裁剪。
实现逻辑:
- 计算放大后的中间尺寸
- 截取中心区域为目标大小
function cropThumbnail(\$srcPath, \$dstPath, \$width, \$height, \$quality = 80) {
list(\$w, \$h) = getimagesize(\$srcPath);
\$source = match(mime_content_type(\$srcPath)) {
'image/jpeg' => imagecreatefromjpeg(\$srcPath),
'image/png' => imagecreatefrompng(\$srcPath),
'image/webp' => imagecreatefromwebp(\$srcPath),
default => false
};
if (!\$source) return false;
\$thumb = imagecreatetruecolor(\$width, \$height);
// 缩放比例
\$ratio = max(\$width / \$w, \$height / \$h);
\$newW = intval(\$w * \$ratio);
\$newH = intval(\$h * \$ratio);
// 中心裁剪偏移
\$x = (\$width - \$newW) / 2;
\$y = (\$height - \$newH) / 2;
imagecopyresampled(\$thumb, \$source, \$x, \$y, 0, 0,
\$newW, \$newH, \$w, \$h);
imagejpeg(\$thumb, \$dstPath, \$quality);
imagedestroy(\$thumb);
imagedestroy(\$source);}
立即学习“PHP免费学习笔记(深入)”;
4. 批量生成多尺寸版本
上传一张图后,自动生成小图、中图、大图等不同用途的缩略图。
\$sizes = [
['width' => 100, 'height' => 100, 'type' => 'crop'],
['width' => 300, 'height' => 200, 'type' => 'scale'],
['width' => 800, 'height' => 600, 'type' => 'scale']
];
foreach (\$sizes as \$size) {
\$output = "thumbs/thumb{$size['width']}x{$size['height']}{\$fileName}";
if (\$size['type'] === 'crop') {
cropThumbnail(\$filePath, \$output, \$size['width'], \$size['height'], 90);
} else {
createThumbnail(\$filePath, \$output, \$size['width'], \$size['height'], 85);
}
}
基本上就这些。注意开启GD扩展、设置合适的upload_max_filesize和post_max_size,并对输出路径做安全过滤。实际项目中建议加入缓存判断和错误日志记录。











