Image Test:
<?php if(isset($_POST['submit'])){
################################################ PROCESSOR #################################################################################
// filename: upload.processor.php
// first let’s set some variables
// make a note of the current working directory, relative to root.
$directory_self = str_replace(basename($_SERVER[‘PHP_SELF’]), ‘’, $_SERVER[‘PHP_SELF’]);
// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = $_SERVER[‘DOCUMENT_ROOT’] . $directory_self . ‘test/’;
// make a note of the location of the upload form in case we need it
$uploadForm = ‘http://’ . $_SERVER[‘HTTP_HOST’] . $directory_self . ‘multiple.upload.form.php’;
// make a note of the location of the success page
$uploadSuccess = ‘http://’ . $_SERVER[‘HTTP_HOST’] . $directory_self . ‘multiple.upload.success.php’;
// name of the fieldname used for the file in the HTML form
$fieldname = ‘file’;
//echo’
’;print_r($_FILES);exit;
// Now let’s deal with the uploaded files
// possible PHP upload errors
$errors = array(1 => ‘php.ini max file size exceeded’,
2 => ‘image file max size exceeded’,
3 => ‘file upload was only partial’,
4 => ‘no file was attached’);
// check the upload form was actually submitted else print form
isset($_POST[‘submit’])
or error(‘the upload form is neaded’, $uploadForm);
// check if any files were uploaded and if
// so store the active $_FILES array keys
$active_keys = array();
foreach($_FILES[$fieldname][‘name’] as $key => $filename)
{
if(!empty($filename))
{
$active_keys[] = $key;
}
}
// check at least one file was uploaded
//count($active_keys)
// or error(‘No files were uploaded’, $uploadForm);
// check for standard uploading errors
foreach($active_keys as $key)
{
($_FILES[$fieldname][‘error’][$key] == 0)
or error($_FILES[$fieldname][‘tmp_name’][$key].’: '.$errors[$_FILES[$fieldname][‘error’][$key]], $uploadForm);
}
// check that the file we are working on really was an HTTP upload
foreach($active_keys as $key)
{
@is_uploaded_file($_FILES[$fieldname][‘tmp_name’][$key])
or error($_FILES[$fieldname][‘tmp_name’][$key].’ not an HTTP upload’, $uploadForm);
}
// validation… since this is an image upload script we
// should run a check to make sure the upload is an image
foreach($active_keys as $key)
{
@getimagesize($_FILES[$fieldname][‘tmp_name’][$key])
or error($_FILES[$fieldname][‘tmp_name’][$key].’ not an image’, $uploadForm);
}
// make a unique filename for the uploaded file and check it is
// not taken… if it is keep trying until we find a vacant one
foreach($active_keys as $key)
{
$now = time();
while(file_exists($uploadFilename[$key] = $now.’-’.$_FILES[$fieldname][‘name’][$key]))
{
$now++;
}
}
foreach($active_keys as $key)
{
$nameparts = pathinfo($uploadFilename[$key]);
$extension = $nameparts[‘extension’];
$uploadFilename[$key] = substr($uploadFilename[$key],"", -4);
$uploadFilename[$key] = preg_replace(’/[^a-zA-Z0-9]/’, ‘-’, $uploadFilename[$key]);
$uploadFilename[$key] = $uploadFilename[$key].".".$extension;
}
// now let’s move the file to its final and allocate it with the new filename
foreach($active_keys as $key)
{
//$uploadFilename = preg_replace(’/[^a-zA-Z0-9]/’, ‘_’, $uploadFilename);
move_uploaded_file($_FILES[$fieldname][‘tmp_name’][$key], $_SERVER[‘DOCUMENT_ROOT’] . $directory_self . ‘test/’ . $uploadFilename[$key]) or error(‘receiving directory insuffiecient permission’, $uploadForm);
$imgfile = “test/”.$uploadFilename[$key];
list($width,$height)=getimagesize($imgfile);
/* The width and the height of the image also the getimagesize retrieve other information as well */
echo "Width: ".$width."<br>";
echo "Height: ".$height."";
if ($width > $height) {$thumbsize = $width;} else {$thumbsize = $height;}
$imgratio=$width/$height;
/*
To compress the image we will calculate the ration
For eg. if the image width=700 and the height = 921 then the ration is 0.77...
if means the image must be compression from its height and the width is based on its height
so the newheight = thumbsize and the newwidth is thumbsize*0.77...
*/
if($imgratio>1) {
$newwidth=$thumbsize;
$newheight=$thumbsize/$imgratio;
} else {
$newheight=$thumbsize;
$newwidth=$thumbsize*$imgratio;
}
$thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image
$source=imagecreatefromjpeg("test/".$uploadFilename[$key]); // Now it will create a new image from the source
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height); // Copy and resize the image
imagejpeg($thumb,"test2/".$uploadFilename[$key],25);
imagedestroy($thumb);
imagedestroy($source);
echo "<span class='pass'>Original Image:</span><br>".$uploadFilename[$key]."<br>
<img src='test/".$uploadFilename[$key]."' />";
echo "<br><br><span class='pass'>New Image:</span><br>".$uploadFilename[$key]."<br>
<img src='test2/".$uploadFilename[$key]."' />";
}
// If you got this far, everything has worked and the file has been successfully saved.
// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
echo ‘’."\n\n".
’’."\n".
’ ‘."\n".
’ ‘."\n\n".
’ ‘."\n\n".
’ Upload error’."\n\n".
’ ‘."\n\n".
’ ‘."\n\n".
’
‘."\n\n".
’
Upload failure
’."\n\n".
’
An error has occured ‘."\n\n".
’ ’ . $error . ‘…’."\n\n".
’ Please press Back Button to retrieve form data and try again.
’."\n\n".
’
‘."\n\n".
’’;
exit;
} // end error handler
######################################################################################################################################################
} ?>
</center>
<div class="form">
<form action="" method="post" name="add" id="add" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<table width="90%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF" class="form">
<tr>
<td>Images: (MAX UPLOAD 1MB)</td>
</tr>
<tr>
<td>Image 1:
<input id="image1" type="file" name="file[]" /></td>
</tr>
<tr>
<td>Image 2:
<input id="image2" type="file" name="file[]" /></td>
</tr>
<tr>
<td>Image 3:
<input id="image3" type="file" name="file[]" /></td>
</tr>
<tr>
<td>Image 4:
<input id="image4" type="file" name="file[]" /></td>
</tr>
<tr>
<td>Image 5:
<input id="image5" type="file" name="file[]" />
<br />
<br />
<br /></td>
</tr>
<tr>
<td><center>
<input type="submit" name="submit" value="submit" />
</center></td>
</tr>
</table>
</form>
</div>