我需要创建一个用户可以上传其个人资料图片的个人资料页面。如您所知,由于尺寸的原因,用户上传的图像无法用于实际处理。这就是图像调整大小的地方。我有一个代码可以做到这一点。但是,它有问题。
它只将原始文件上传到mysql数据库目录/列。它不应该上传“thumb_”图像吗?除此之外,原始和thumb_图像都在文件夹中创建。
这是代码。希望你能告诉我它有什么问题。另外,你能告诉我如何限制上传到文件夹的图像数量?对于个人资料,我只想要两个图像(原始和拇指_)。
Profile.php
<?php
include "core/init.php";
include "includes/overall/header.php";
if (isset($_GET['username']) === true && empty($_GET['username']) === false) {
$username = $_GET['username'];
if (user_exists($username) === true) {
$user_id = user_id_from_username($username);
$profile_data = user_data($user_id, 'Username', 'Name', 'Email');
?>
<div id="body_content_sub">
<div class="title_line">
<div id="title">
<h5><?php echo $profile_data['Name']; ?></h5>
</div>
</div>
<?php
if (isset($_FILES['image']) === true) {
if (empty($_FILES['image']['name']) === true) {
echo "Please choose a file!";
} else {
//Preparing the variables
$name = $_FILES['image']['name'];
$temp = $_FILES['image']['tmp_name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$ext = strtolower(end(explode('.', $name)));
$size2 = getimagesize($temp);
$width = $size2[0];
$height = $size2[1];
$upload = md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000));
// Requirements
$maxwidth = 1920;
$maxheight = 1080;
$allowed = array('image/jpeg', 'image/png', 'image/gif');
// Recognizing the extension
switch ($type) {
// Image/Jpeg
case 'image/jpeg':
$ext = '.jpg';
break;
// Image/png
case 'image/png':
$ext = '.png';
break;
// Image/gif
case 'image/gif':
$ext = '.gif';
break;
}
// upload variables
$path = 'images/avatars/' . $upload . $ext;
$thumb_path = 'images/avatars/thumb_' . $upload . $ext;
if (in_array($type, $allowed) === true) {
if ($width < $maxwidth && $height < $maxheight) {
if ($size < 5242880) {
// check the shape of the image
if ($width == $height) {
$case = 1;
}
if ($width > $height) {
$case = 2;
}
if ($width < $height) {
$case = 3;
}
switch ($case) {
// Square
case 1:
$newwidth = 200;
$newheight = 200;
break;
// Lying rectangle
case 2:
$newheight =
200;
$ratio = $newheight / $height;
$newwidth = round($width * $ratio);
echo $newwidth . 'x' . $newheight;
break;
// Standing rectangle
case 3:
$newwidth = 200;
$ratio = $newwidth / $width;
$newheight =
round($height * $ratio);
break;
}
switch ($type) {
case 'image/jpeg';
$img =
imagecreatefromjpeg($temp);
$thumb =
imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $thumb_path);
break;
case 'image/png';
$img =
imagecreatefrompng($temp);
$thumb =
imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb, $thumb_path);
break;
case 'image/gif';
$img =
imagecreatefromgif($temp);
$thumb =
imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagegif($thumb, $thumb_path);
break;
}
change_avatar_image($user_id, $temp, $ext, $upload);
} else {
echo 'Your image size is too big.';
}
} else {
echo '<p>Your image resolution exceeds the limit.</p>';
}
} else {
echo 'You have uploaded a forbidden extension.';
}
}
}
if (empty($user_data['Avatars']) === false) {
echo '<img src="', $user_data['Avatars'], '">';
}
?>
<div id="avatar">
</div>
<div class="form">
<form action="" method="post" enctype="multipart/form-data"><br>
<input type="file" name="image"><br>
<input type="submit" value="Upload">
</form>
</div>
</div>
<?php
} else {
echo "Sorry, that user doesn't exist.";
}
} else {
header('Location: index.php');
exit();
}
include "includes/overall/footer.php"; ?>
的functions.php
function change_avatar_image($user_id, $temp, $ext, $upload) {
$path= 'images/avatars/' . $upload . $ext;
move_uploaded_file($temp, $path);
mysql_query("UPDATE users SET Avatars = '" . $path . "' WHERE user_id = " . `(int)$user_id);`
}
我为代码的外观道歉。我很难在这里正确地发布它。