我使用以下代码上传,重命名,压缩,创建缩略图一切正常,最近我注意到创建缩略图时,它还为以前上传的图像创建了缩略图图像的新副本(为上传的缩略图创建缩略图,也上传图片)
问题:
提交表单时,它会创建用于上传图像和已上传图像(较旧的图像文件)的大拇指。”>
我该如何解决这个问题
if (!empty($_POST)) { if (isset($_FILES['files'])) { $uploadedFiles = array(); foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) { $errors = array(); $file_name = md5(uniqid("") . time()); $file_size = $_FILES['files']['size'][$key]; $file_tmp = $_FILES['files']['tmp_name'][$key]; $file_type = $_FILES['files']['type'][$key]; if ($file_type == "image/gif") { $sExt = ".gif"; } elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") { $sExt = ".jpg"; } elseif ($file_type == "image/png" || $file_type == "image/x-png") { $sExt = ".png"; } if (!in_array($sExt, array('.gif', '.jpg', '.png'))) { $errors[] = "Image types alowed are (.gif, .jpg, .png) only!"; } if ($file_size > 2097152000) { $errors[] = 'File size must be less than 2 MB'; } $desired_dir = "$_SERVER[DOCUMENT_ROOT]/upload/file/"; if (empty($errors)) { if (is_dir($desired_dir) == false) { mkdir("$desired_dir", 0700); } if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) { $uploadedFiles[$key] = array($file_name . $sExt, 1); } else { echo "Couldn't upload file " . $_FILES['files']['tmp_name'][$key]; $uploadedFiles[$key] = array($_FILES['files']['tmp_name'][$key], 0); } } else { } } foreach ($uploadedFiles as $key => $row) { if (!empty($row[1])) { $codestr = '$file' . ($key + 1) . ' = $row[0];'; eval($codestr); } else { $codestr = '$file' . ($key + 1) . ' = NULL;'; eval($codestr); } } } $orig_directory = "$desired_dir"; $thumb_directory = "$_SERVER[DOCUMENT_ROOT]/upload/thumb/"; $dir_handle = opendir($orig_directory); if ($dir_handle > 1) { $allowed_types = array('jpg', 'jpeg', 'gif', 'png'); $file_type = array(); $ext = ''; $title = ''; $i = 0; while ($file_name = readdir($dir_handle)) { if ($file_name == '.' || $file_name == '..') { continue; } $file_type = \explode('.', $file_name); $ext = strtolower(array_pop($file_type)); $title1 = implode('.', $file_type); $title = htmlspecialchars($title1); if (in_array($ext, $allowed_types)) { $nw = 125; $nh = 90; $source = "$desired_dir{$file_name}"; $stype1 = explode(".", $source); $stype = $stype1[count($stype1) - 1]; $dest = "$_SERVER[DOCUMENT_ROOT]/upload/thumb/{$file_name}"; $size = getimagesize($source); $w = $size[0]; $h = $size[1]; switch ($stype) { case 'gif': $simg = imagecreatefromgif($source); break; case 'jpg': $simg = imagecreatefromjpeg($source); break; case 'png': $simg = imagecreatefrompng($source); break; } $dimg = resizePreservingAspectRatio($simg, $nw, $nh); imagepng($dimg, $dest); compress($source, "$desired_dir/" . $file_name, 50); } }closedir($dir_handle); } $stmt = $conn->prepare("INSERT INTO allpostdata(im1, im2, im3, im4)" . " VALUES (:im1, :im2, :im3, :im4)"); $stmt->bindParam(':im1', $file1, PDO::PARAM_STR, 100); $stmt->bindParam(':im2', $file2, PDO::PARAM_STR, 100); $stmt->bindParam(':im3', $file3, PDO::PARAM_STR, 100); $stmt->bindParam(':im4', $file4, PDO::PARAM_STR, 100); if ($stmt->execute()) { header('Location: /post/price_plan.php'); }exit; } function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source); } elseif ($info['mime'] == 'image/gif') { $image = imagecreatefromgif($source); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source); } imagejpeg($image, $destination, $quality); return $destination; } function resizePreservingAspectRatio($img, $targetWidth, $targetHeight) { $srcWidth = imagesx($img); $srcHeight = imagesy($img); $srcRatio = $srcWidth / $srcHeight; $targetRatio = $targetWidth / $targetHeight; if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight)) { $imgTargetWidth = $srcWidth; $imgTargetHeight = $srcHeight; } else if ($targetRatio > $srcRatio) { $imgTargetWidth = (int) ($targetHeight * $srcRatio); $imgTargetHeight = $targetHeight; } else { $imgTargetWidth = $targetWidth; $imgTargetHeight = (int) ($targetWidth / $srcRatio); } $targetImg = imagecreatetruecolor($targetWidth, $targetHeight); $targetTransparent = imagecolorallocate($targetImg, 255, 0, 255); imagefill($targetImg, 0, 0, $targetTransparent); imagecolortransparent($targetImg, $targetTransparent); imagecopyresampled($targetImg, $img, 0, 0, 0, 0, $targetWidth, $targetHeight, $srcWidth, $srcHeight); return $targetImg; }
赏金编辑
如果有任何更好,更快速的功能要做。
我只需要上传,重命名,压缩,创建缩略图并将名称保存到数据库
我使用以下代码上传,重命名,压缩,创建缩略图,一切正常,最近我注意到在创建缩略图时,它会为以前上传的缩略图创建新的缩略图副本...
该代码中有很多不良的php编程习惯(例如,使用eval和常规数据流)。要对其进行分解,请执行以下操作:该脚本首先验证上载的文件,然后将其移动到临时目录。然后,它为temp目录中的所有文件计算缩略图。
代码需要更多的优化。您每次都再次迭代file
文件夹,而不是循环刚刚上传的文件。