我试图一次上传多张图片,每张图片都创建并存储了一个缩略图。脚本功能不起作用?

I'm attempting to upload multiple images at a time with each image having a thumb created and stored as well. Script function not working?

这是一个 updated/edited post 脚本,非常适合上传多张图片。问题是在尝试为每个上传的图像创建缩略图时。 GALLERY_IMG_PATHGALLERY_IMG_FULLSIZEGALLERY_IMG_THUMB 已定义在配置文件中,这些只是路径信息。 我正在使用的脚本:

$errors = array();
$uploadedFiles = array();
$extension = array("jpeg","jpg","png");
$bytes = 1024;
$KB = 1024;
$totalBytes = $bytes * $KB;
$counter = 0;
if(isset($_FILES["images"]["tmp_name"])) {
foreach($_FILES["images"]["tmp_name"] as $key=>$tmp_name) {
$temp_Name = $_FILES["images"]["tmp_name"][$key];
$images_Name = $_FILES["images"]["name"][$key];
if(empty($temp_Name)) {
break;
}
$counter++;
$UploadOk = true;
if($_FILES["images"]["size"][$key] > $totalBytes) {
$UploadOk = false;
array_push($errors, $images_Name." file size is larger than the 1 MB. You must 
upload images smaller than 1MB.");
}
$ext = strtolower(pathinfo($images_Name, PATHINFO_EXTENSION));
if(in_array($ext, $extension) == false){
$UploadOk = false;
array_push($errors, $images_Name." is an invalid file type. Images must be 
either of the following: jpeg | jpg | png");
}
if(file_exists(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name) == 
true) {
$UploadOk = false;
array_push($errors, "Attempted to upload an image that is already uploaded/the 
image name conflicts with an image that already uses the 
name:".$images_Name.".");
}     
if($UploadOk == true) {
move_uploaded_file($temp_Name, 
GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
array_push($uploadedFiles, $images_Name);
// Get the image size and type & Load the image into memory
$attrs = getimagesize (GALLERY_IMG_PATH."/".$images_Name);
$imageWidth = $attrs[0];
$imageHeight = $attrs[1];
$imageType = $attrs[2];
switch ($imageType) {
case IMAGETYPE_JPEG:
$imageResource = imagecreatefromjpeg 
(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
break;
case IMAGETYPE_PNG:
$imageResource = imagecreatefrompng 
(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$images_Name);
break;
default:
trigger_error ("Post::storeUploadedImage(): Unhandled or unknown image type 
($imageType)", E_USER_ERROR);
}
// Copy and resize the image to create the thumbnail
$thumbHeight = intval ($imageHeight / $imageWidth * GALLERY_THUMB_WIDTH);
$thumbResource = imagecreatetruecolor (GALLERY_THUMB_WIDTH, $thumbHeight);
imagecopyresampled($thumbResource, $imageResource, 0, 0, 0, 0, 
GALLERY_THUMB_WIDTH, $thumbHeight, $imageWidth, $imageHeight);
// Save the thumbnail
switch ($imageType) {
case IMAGETYPE_JPEG:
imagejpeg ($thumbResource, GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB, 100);
break;
case IMAGETYPE_PNG:
imagepng ($thumbResource, GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB, 100);
break;
default:
trigger_error ("Post::storeUploadedImage(): Unhandled or unknown image type 
($imageType)", E_USER_ERROR);
}
}
}
}
if($counter>0){
if(count($errors)>0)
{
echo "<b>Errors:</b>";
echo "<br/><ul>";
foreach($errors as $error) {
echo "<li>".$error."</li>";
}
echo "</ul><br/>";
}
if(count($uploadedFiles)>0) {
echo "<b>Uploaded Files:</b>";
echo "<br/><ul>";
foreach($uploadedFiles as $fileName) {
echo "<li>".$fileName."</li>";
}
echo "</ul><br/>";
echo count($uploadedFiles)." file(s) have successfully uploaded.";
}                               
}

html形式为:

<form action="editGallery" method="post" enctype="multipart/form-data" name="formUploadFile">
<label for="files[]">Select image/images to upload:</label>
<input type="file" name="images[]" id="images" accept="image/*" multiple="multiple" 
placeholder="Upload an image/images.">
<input type="submit" name="uploadImages" value="UploadImages">
</form>

现在脚本正在执行,在将一张图片上传到全尺寸目录后出现错误,我猜测它会尝试创建缩略图并给出错误。

最终我在这个已发布的问题中没有得到太多帮助或支持,但我终于让它工作了,这是最终输出:

/** Create Thumbnails For Images Uploaded */
function createThumbnail($imagesName, $thumbWidth, $thumbHeight, $imagesDir, 
$thumbsDir) {
$imagesSource = $imagesDir."/".$imagesName;
$thumbImages = $thumbsDir."/".$imagesName;
list($width,$height) = getimagesize($imagesSource);
$thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
if(preg_match('/[.](jpg|jpeg)$/i', $imagesName)) {
$image_source = imagecreatefromjpeg($imagesSource);
} else if (preg_match('/[.](png)$/i', $imagesName)) {
$image_source = imagecreatefrompng($imagesSource);
} else {
$image_source = imagecreatefromjpeg($imagesSource); 
}
imagecopyresized($thumb, $image_source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, 
$height);
if(preg_match('/[.](jpg|jpeg)$/i', $imagesName)) {
imagejpeg($thumb,$thumbImages, 65);
} else if (preg_match('/[.](png)$/i', $imagesName)) {
imagepng($thumb, $thumbImages, 5);
} else {
imagejpeg($thumb, $thumbImages, 65);
}
}
/** Upload An Image Or Multiple Images And Setup Errors */
$uploadedImages = array();
$errors = array();
$extension = array("jpeg", "jpg", "png", "JPEG", "JPG", "PNG");
$bytes = 2048;
$KB = 2048;
$totalBytes = $bytes * $KB;
$counter = 0;
$imagesDir = GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/";
$thumbsDir = GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB."/";
if(isset($_FILES["images"]["tmp_name"])) {
foreach($_FILES['images']['name'] as $key=>$val) {
$imagesName = str_replace(" ", "_", $_FILES['images']['name'][$key]);
$thumbSource = GALLERY_IMG_PATH.GALLERY_IMG_FULLSIZE."/".$imagesName;
$counter++;
$UploadOk = true;
if($_FILES["images"]["size"][$key] > $totalBytes) {
$UploadOk = false;     // Size Limitation Per Image
array_push($errors, $imagesName." file size is larger than the 2MB. You must upload 
images smaller than 2MB.");
}
$ext = strtolower(pathinfo($imagesName, PATHINFO_EXTENSION));
if(in_array($ext, $extension) == false) {
$UploadOk = false;  // Image Extensions Handled/Accepted
array_push($errors, $imagesName." has an invalid image extension. Images must contain 
only these extensions: *.jpeg | *.jpg | *.png");
header("Location: Admin?admin_action=editGallery&error=invalidExtension");
}
if(file_exists(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$imagesName) == true) {
$UploadOk = false;  // Prevent Image/Images Uploaded With Same Name
array_push($errors, "You attempted to upload an image that is already uploaded/the 
image name conflicts with an image that already uses the name:".$imagesName.". If it's 
not a duplicate image than simply rename it to have a unique difference.");
}     
if($UploadOk == true) {   // No Errors Occurred: Execute Upload & Create Thumbnail
if(move_uploaded_file($_FILES['images']['tmp_name'][$key], $imagesDir.$imagesName)) {
array_push($uploadedImages, $imagesName);
$imagesSource = $imagesDir."/".$imagesName;
list($width,$height) = getimagesize($imagesSource);
$thumbWidth = 290;
$thumbHeight = intval ($height / $width * $thumbWidth);
createThumbnail($imagesName, $thumbWidth, $thumbHeight, $imagesDir, $thumbsDir);
}
}
}
}