问题描述
我想在用户上传个人资料照片时自动调整图片大小。这是图片上传PHP
I want to resize an image automatically when a user upload a profile picture. This is the image upload PHP
<?php
$user=$_SESSION['dbUser']['username'];
$db=new mysqli('host','username','password','database');
if($db->connect_errno){
echo $db->connect_error;}
$pull="select * from users where user='$user'";
$allowedExts = array("jpg", "jpeg", "gif", "png","JPG","PNG");
$extension = @end(explode(".", $_FILES["file"]["name"]));
if(isset($_POST['pupload'])){
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/PNG"))
&& ($_FILES["file"]["size"] < 200000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Uploaded Successully<br>";
echo "It may take up to half an hour to update.<br><br>";
if (file_exists("upload/" . $_SESSION['dbUser']['username']))
{
unlink("upload/" . $_SESSION['dbUser']['username'].".".$ext);
}
else
{
$pic=$_FILES["file"]["name"];
$conv=explode(".",$pic);
$ext=$conv['1'];
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_SESSION['dbUser']['username'].".jpg");
$url=$user.".jpg";
$query="update picture set url='$url', lastUpload=now() where user='$user'";
if($upl=$db->query($query)){
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
}
}
}
else
{
echo "File Size Limit Crossed 200 KB Use Picture Size less than 200 KB";
}
}
?>
推荐答案
user =
_SESSION [' dbUser '] [' username']; _SESSION['dbUser']['username'];
db = new mysqli(' host',' 用户名',' password',' database'); if( db=new mysqli('host','username','password','database'); if(