<?PHP
class ImgResizer {
private $originalFile = '';
public function __construct($originalFile = '') {
$this -> originalFile = $originalFile;
}
public function resize($newWidth, $targetFile) {
if (empty($newWidth) || empty($targetFile)) {
return false;
}
$src = imagecreatefromjpeg($this -> originalFile);
list($width, $height) = getimagesize($this -> originalFile);
$newHeight = ($height / $width) * $newWidth;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
if (file_exists($targetFile)) {
unlink($targetFile);
}
imagejpeg($tmp, $targetFile, 85); // 85 is my choice, make it between 0 – 100 for output image quality with 100 being the most luxurious
}
}
$files=array();
$fdata=$_FILES['userfile'];
if(is_array($fdata['name'])){
for($i=0;$i<count($fdata['name']);++$i){
$files[]=array(
'name' =>$fdata['name'][$i],
'type' => $fdata['type'][$i],
'tmp_name'=>$fdata['tmp_name'][$i],
'error' => $fdata['error'][$i],
'size' => $fdata['size'][$i]
);
}
} else $files[]=$fdata;
//begin upload code
$continue = 1;
$adr = "pics/" . $_GET["adr"] . "/";
$maxSize = 1000000000;
foreach ($files as $file) {
if(($file['type'] == "image/gif") || ($file['type'] == "image/jpeg") || ($file['type'] == "image/pjpeg") || ($file['type'] == "image/png")) {
}
else {
$errorMessage = "<p>Wrong File Type</p>";
$continue = 0;
}
if($file['size'] > $maxSize) {
$errorMessage = "<p>File is too large.</p>";
$continue = 0;
}
//check for errors
if(($file['error'] > 0)) {
echo "<p>Error" . $file['error'] . "</p>";
}
elseif($continue == 0) {
echo $errorMessage;
}
//display info
elseif($continue == 1) {
echo "Name:" . $file['name'] . "<br />";
echo "Type:" . $file['type'] . "<br />";
if((file_exists($adr . $file['name']))) {
echo "<p>" . $file['name'] . "already exists!</p>";
}
else {
//upload large version
move_uploaded_file($file['tmp_name'], $adr . $file['name']);
//resize and copy image for thumbnail
$work = new ImgResizer($adr . $file['name']);
$work -> resize(180, $adr . "/thumb/" . $file['name']);
echo "<p>Upload Successful! View file <a href='" . $adr . $file['name'] . "'>here.</a></p>
<p><a href='" . $adr . "/thumb/" . $file['name'] . "'>Thumbnail</a></p>";
}
}
}
?>
My HTML:
<h1>Upload image</h1>
<p>
<span class="notification">Use shift+click or ctrl+click to select multiple images at once. <br /></span>
</p>
<p>Only <span class="emp">*.png, *.jpeg, *.jpg, and *.gif</span> filetypes are allowed.<br /></p>
<form action= "<?PHP echo "upload.php?adr=" . $_POST["formFeast"]; ?>" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="userfile[]" id="userfile" multiple="multiple" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<p><a href="admin-dashboard">Back to admin dashboard</a></p>
Most of the time it works great. However, when uploading lots of files at the same time, I get "Wrong File Type" as an output. and only 1 "Wrong file type", not multiple.
Does this have to do with GD?