在PHP表单中创建缩略图时出现问题

在PHP表单中创建缩略图时出现问题,php,gd,Php,Gd,我很难让脚本的这部分创建缩略图,缩略图文件夹中有0777,但代码不会创建缩略图,有什么建议吗 if (isset($_FILES['user_photo']) && !empty($_FILES['user_photo']['name'])) { $upload_dir ="images"; $thum_dir= "thumbnails"; $img = $_FILES["user_photo"]["na

我很难让脚本的这部分创建缩略图,缩略图文件夹中有0777,但代码不会创建缩略图,有什么建议吗

if (isset($_FILES['user_photo']) && !empty($_FILES['user_photo']['name'])) {

$upload_dir ="images";
$thum_dir= "thumbnails";
$img =   $_FILES["user_photo"]["name"];  
//$temp = explode(".", $_FILES["user_photo"]["name"]);
//$file_name = $temp[0].'_'.time();
//$file_ext = $temp[1];
//$img = $file_name . '.' . $file_ext;



$orgfile = $_FILES["user_photo"]["tmp_name"];
list($width,$height)= getimagesize($orgfile);
$newfile = imagecreatefromjpeg($orgfile);
$newwidth = "150";
$newheight = "100";
$thum = $_FILES["user_photo"]["name"];

$truecolor = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($truecolor, $newfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($truecolor,$thum,100);

copy($_FILES['user_photo']['tmp_name'], "$upload_dir/$img");
copy($_FILES['user_photo']['name'], "$thum_dir/$thum");
}

对于一个可管理的图像上传系统,我强烈建议使用MYSQL来帮助节省服务器上的空间,并使选择和显示图像更容易

为此,您需要创建一个具有id(主键、自动递增键)的MYSQL表,然后为user、image和thumb创建3个附加列,将它们设置为null Yes,并将排序规则设置为utf8\u general\u ci

现在创建用于访问数据库和表的用户凭据。我不打算谈那个部分

最后,为图像创建2个文件夹。在本例中,这两个文件夹称为“图像”和“拇指”。两个文件夹的权限都应设置为755

 <?php
 if (isset($_FILES['user_photo'])) {

 // Your MYSQL connection file
 require_once ('db.php');

 // Thumbnail maker function to be called later - do not alter this
 function make_thumb($src, $dest, $desired_width) {
 $source_image = imagecreatefromjpeg($src);
 $width = imagesx($source_image);
 $height = imagesy($source_image);
 $desired_height = floor($height * ($desired_width / $width));
 $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
 imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
 imagejpeg($virtual_image, $dest);
 }

 // File settings
 $username = "user01"; // Example: you need some info to identify the user
 $target_dir = "/images/"; // Folder for main images
 $extension = array("jpeg","jpg","png"); // images only - add more extensions as required
 $limit = 5242880; // max 5mb - change this if required
 $upload = true; // Initially set to true unless checks fail!
 $ext = strtolower(pathinfo($_FILES["user_photo"]["name"], PATHINFO_EXTENSION)); // Get file extension
 $time = preg_replace('/(0)\.(\d+) (\d+)/', '$3$1$2', microtime()); // Unique prefix file name based on time
 $dot = "."; // Just a dot
 $new_filename = $time . $dot . $ext; // Full file name

 // Delete image files & MYSQL record if already uploaded
 $check = mysqli_query($con, "select image from images where user='$username'");
 if (mysqli_num_rows($check) > 0) {
 while ($row = mysqli_fetch_array($check)) {
 $imageold = $row['image'];
 unlink($_SERVER['DOCUMENT_ROOT'] . "/images/$imageold"); // Delete old image main file
 unlink($_SERVER['DOCUMENT_ROOT'] . "/thumbs/$imageold"); // Delete old image thumb file
 $delete = mysqli_prepare($con, "delete from images where user=?");
 mysqli_stmt_bind_param($delete, "s", $username);
 mysqli_stmt_execute($delete);
 }
 }

 // Check file size
 if ($_FILES["user_photo"]["size"] > $limit) {$upload = false;}

 // Check file extension
 if (in_array($ext, $extension) == false) {$upload = false;}

 // Upload file if checks ok + insert image record to MYSQL table
 if ($upload == true) {
 move_uploaded_file($_FILES['user_photo']['tmp_name'], $target_dir . $new_filename);
 $insert = mysqli_prepare($con, "insert into images (user,image) values (?,?)");
 mysqli_stmt_bind_param($insert, "ss", $username, $new_filename);
 mysqli_stmt_execute($insert);
 }

 // If main image inserted, create thumb
 if ($insert) {
 echo "FILE UPLOADED";
 $select = mysqli_query($con, "select image from images where user='$username'");
 if (mysqli_num_rows($select) == 1) {
 while ($row = mysqli_fetch_array($select)) {
 $imagename = $row['image'];
 $src = "/images/$imagename";
 $dest = "/thumbs/$imagename";
 $desired_width = "250"; // This will create a square thumb at 250px, alter size if required
 make_thumb($src, $dest, $desired_width); // Create thumb
 // Now update existing user record to include thumb
 $update = mysqli_prepare($con, "update images set thumb=? where user=?");
 mysqli_stmt_bind_param($update, "ss", $imagename, $username);
 mysqli_stmt_execute($update);
 }
 }
 }

 }
 ?>

当您将用户图像上传到目录时,我强烈建议您使用MYSQL,这样您就可以记录图像名称,并在用户更新图像时取消旧图像的链接(删除)。这将有助于节省服务器上的空间。