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

上传图片并生成多种尺寸缩略图是Web开发中常见的需求,特别是在用户头像、商品展示等场景。PHP结合GD库或ImageMagick可以轻松实现图片上传、按比例缩放裁剪、压缩质量控制等功能。
确保表单使用enctype="multipart/form-data",接收文件后先验证类型和大小,再移动到临时目录。
示例代码:
\$uploadDir = 'uploads/';
\$allowedTypes = ['image/jpeg', 'image/png', 'image/webp'];
<p>if (\$_FILES['image']['error'] === UPLOAD_ERR_OK) {
\$tmpName = \$_FILES['image']['tmp_name'];
\$mimeType = mime_content_type(\$tmpName);</p><pre class='brush:php;toolbar:false;'>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免费学习笔记(深入)”;
使用GD库创建等比缩放图像,避免变形。核心是计算新尺寸,保留原始比例。
关键步骤:
function createThumbnail(\$srcPath, \$dstPath, \$targetWidth, \$targetHeight, \$quality = 80) {
list(\$origWidth, \$origHeight) = getimagesize(\$srcPath);
<pre class='brush:php;toolbar:false;'>// 计算缩放比例
\$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免费学习笔记(深入)”;
适用于头像或需要统一尺寸的展示区域。先等比缩放到大于目标尺寸,再从中心裁剪。
实现逻辑:
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
};
<pre class='brush:php;toolbar: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免费学习笔记(深入)”;
上传一张图后,自动生成小图、中图、大图等不同用途的缩略图。
\$sizes = [
['width' => 100, 'height' => 100, 'type' => 'crop'],
['width' => 300, 'height' => 200, 'type' => 'scale'],
['width' => 800, 'height' => 600, 'type' => 'scale']
];
<p>foreach (\$sizes as \$size) {
\$output = "thumbs/thumb<em>{$size['width']}x{$size['height']}</em>{\$fileName}";
if (\$size['type'] === 'crop') {
cropThumbnail(\$filePath, \$output, \$size['width'], \$size['height'], 90);
} else {
createThumbnail(\$filePath, \$output, \$size['width'], \$size['height'], 85);
}
}</p>基本上就这些。注意开启GD扩展、设置合适的upload_max_filesize和post_max_size,并对输出路径做安全过滤。实际项目中建议加入缓存判断和错误日志记录。
以上就是php如何上传图片并生成多种尺寸缩略图_php按比例裁剪与压缩质量设置的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号