文章详情
php对服务器上传的图像对图像进行缩放保存
Posted on 2023-02-19 08:05:23 by 主打一个C++
记录记录记录:上代码
为了节省服务器开销,上传的图像宽高大于100pix 就缩放至100*100
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 检查文件是否上传成功
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
// 获取上传的文件信息
$fileTmpPath = $_FILES['image']['tmp_name'];
$fileName = $_FILES['image']['name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
// 加载图片
list($width, $height) = getimagesize($fileTmpPath);
$src = null;
// 根据不同的图片类型创建图像资源
switch ($fileType) {
case 'image/jpeg':
$src = imagecreatefromjpeg($fileTmpPath);
break;
case 'image/png':
$src = imagecreatefrompng($fileTmpPath);
break;
case 'image/gif':
$src = imagecreatefromgif($fileTmpPath);
break;
}
// 进行缩放
if ($src) {
$newWidth = $width;
$newHeight = $height;
if ($width > 100 || $height > 100) {
$aspectRatio = $width / $height;
if ($aspectRatio > 1) {
$newWidth = 100;
$newHeight = 100 / $aspectRatio;
} else {
$newHeight = 100;
$newWidth = 100 * $aspectRatio;
}
}
// 创建新的空白图像
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// 保存缩放后的图像
$uploadFileDir = './uploads/';
$filePath = $uploadFileDir . 'resized_' . $fileName;
// 确保目标目录存在
if (!is_dir($uploadFileDir)) {
mkdir($uploadFileDir, 0777, true);
}
imagejpeg($dst, $filePath);
// 释放内存
imagedestroy($src);
imagedestroy($dst);
echo "图片已成功上传并缩放,保存路径为: " . $filePath;
} else {
echo "无法处理该文件类型";
}
} else {
echo "文件上传出错";
}
} else {
echo "请求方法不正确";
}
?>
//html同步测试代码:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试缩放图像</title>
</head>
<body>
<h1>上传并缩放图片</h1>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="image_scaling.php">
<input type="file" name="image" accept="image/*" required>
<button type="submit">上传并缩放</button>
</form>
</body>
</html>
//html+js异步测试代码,直接用jQuery
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>测试缩放图像</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>上传并缩放图片</h1>
<form id="uploadForm" enctype="multipart/form-data" method="post">
<input type="file" name="image" accept="image/*" required>
<button type="submit">上传并缩放</button>
</form>
<!-- 测试显示结果 -->
<div id="result"></div>
<script>
$(document).ready(function() {
$('#uploadForm').on('submit', function(e) {
e.preventDefault(); // 阻止表单的默认提交
var formData = new FormData(this); // 创建表单数据对象
$.ajax({
url: 'image_scaling.php', // 指定处理上传的服务器端脚本
type: 'POST', // 请求方式
data: formData, // 使用表单数据
contentType: false, // 不设置内容类型
processData: false, // 不处理数据
success: function(response) {
$('#result').html(response); // 显示服务器返回的结果
},
error: function(jqXHR, textStatus, errorThrown) {
$('#result').html('上传失败,请重试。'); // 显示错误信息
}
});
});
});
</script>
</body>
</html>
为了满足我的需求,固定高度为128,宽度按照比例减小进行缩放:
处理后的php代码:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 检查文件是否上传成功
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
// 获取上传的文件信息
$fileTmpPath = $_FILES['image']['tmp_name'];
$fileName = $_FILES['image']['name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
// 加载图片
list($width, $height) = getimagesize($fileTmpPath);
$src = null;
// 根据不同的图片类型创建图像资源
switch ($fileType) {
case 'image/jpeg':
$src = imagecreatefromjpeg($fileTmpPath);
break;
case 'image/png':
$src = imagecreatefrompng($fileTmpPath);
break;
case 'image/gif':
$src = imagecreatefromgif($fileTmpPath);
break;
}
// 进行缩放
if ($src) {
$newHeight = 128; // 设置新的高度为128
$aspectRatio = $width / $height; // 计算宽高比
$newWidth = $newHeight * $aspectRatio; // 按照比例计算新的宽度
// 创建新的空白图像
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// 保存缩放后的图像
$uploadFileDir = './uploads/';
$filePath = $uploadFileDir . 'resized_' . $fileName;
// 确保目标目录存在
if (!is_dir($uploadFileDir)) {
mkdir($uploadFileDir, 0777, true);
}
imagejpeg($dst, $filePath);
// 释放内存
imagedestroy($src);
imagedestroy($dst);
echo "图片已成功上传并缩放,保存路径为: " . $filePath;
} else {
echo "无法处理该文件类型";
}
} else {
echo "文件上传出错";
}
} else {
echo "请求方法不正确";
}
?>
*转载请注明出处:原文链接:https://cpp.vin/page/61.html