This is an updated/edited post of a script which works great for uploading multiple images. The problem is when trying to create a thumb for each image uploaded. GALLERY_IMG_PATH, GALLERY_IMG_FULLSIZE, and GALLERY_IMG_THUMB are defined in the config file those are just path information. The script I'm using:
$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.";
}
}
The html form is:
<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>
Right now the script is executing and errors out after one image uploads to the fullsize directory and from what I'm guessing is it attempts to create the thumb and gives an error.
Ultimately I didn't get much help or support in this posted question but I did finally get it working with this being the final output:
/** 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);
}
}
}
}