Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
<?php
define ("MAXSIZE", 400, false); //this is the max size in pixels that the images will be scaled to
define ("IMAGEPATH", "./images/", false); // the relative path (or absolute) to save albums
define ("ALLOWZIP", true); //defines whether bulk uploads are allowed in zip format. Must support unix ZIP commands.
define ("INIT", true); // change this to false to stop the file initialisation
define ("MAXFILESIZE", ini_get("upload_max_filesize")); //maximum file upload size;
define ("LOCALTMP", "./localtmp", false); //temporary storage for uploaded zip files
switchboard(); //start the machine
function switchboard(){
if (INIT) init(); //create the directories etc
$action = isset($_POST['action']) ? $_POST['action'] : '';
switch ($action) {
case "uploadfile":
processUploadedFile();
break;
case "Upload":
displayUploadForm();
break;
break;
displayUploadForm();
}
}
function init(){
$u = umask(0);
if (!file_exists(IMAGEPATH)){
mkdir(IMAGEPATH, 0777);
}
if (!file_exists(LOCALTMP)){
mkdir (LOCALTMP, 0777);
}
umask ($u);
}
function getTempDir(){
umask(0);
$dir = "./".uniqid("tempzip_", true) .'/';
mkdir ($dir);
return $dir;
}
function displayUploadForm($msg=NULL){
$maxsize = MAXFILESIZE;
$msg = (empty($msg)) ? NULL: '<div class="errorMessage">'.$msg.'</div>';
$shortdescription = (isset($_POST['shortdescripton'])) ? $_POST['shortdescripton'] : '';
$contentObject = <<<HTML
$msg
<form method="post" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" >
<fieldset>
<legend>Upload images</legend>
<p>You can upload gif, png or jpegs either singly or in bulk as a zip file</p>
Choose file (max $maxsize): <input type="file" name="upload" /><br/>
Short description: <input type="text" maxlength="255" name="shortdescription" value="$shortdescription" /><br/>
Which album: $select
<input type="submit" name="submit" value="Upload" />
<input type="hidden" name="action" value="uploadfile" />
</fieldset>
</form>
HTML;
echo $contentObject;
}
function processUploadedFile(){
//check for errors
$msg = "";
if (isset($_FILES['upload'])){
switch ($_FILES['upload']['error']){
case 0:
break;
case 1:
case 2:
$msg = "The uploaded file is too large";
break;
case 3:
$msg = "The uploaded file was only partially uploaded";
break;
case 4:
$msg = "No file was uploaded";
break;
case 6:
case 7:
case 8;
$msg = "There was a problem saving ths uploaded file.";
break;
}
}
//if we have a problem, tell the user
if (!empty($msg)){
displayUploadForm($msg);
exit();
}
//check the file type.
//simple check against the file extension. not ideal...
if (!empty($_POST['upload']['type'])){
$type = $_POST['upload']['type'];
}else {
$type = getFileType($_FILES['upload']['name']);
}
switch ($type){
case "application/x-zip":
if (ALLOWZIP){
$tmpdir = getTempDir();
if (!move_uploaded_file($_FILES['upload']['tmp_name'], $tmpdir."tmpzipfile.zip")){
displayUploadForm("Problem processing zip file");
exit();
}
processUploadedZipFile($tmpdir, "tmpzipfile.zip", $albumName);
} else {
displayUploadForm("Sorry we do not allow Zip files");
}
break;
case "unsupported":
displayUploadForm("Only zip files and jpegs are allowed to be uploaded");
exit();
break;
default:
$extension = getExtensionFromType($type);
$uid = uniqid("photo_", true) .".$extension";
$newName = IMAGEPATH.$albumName.'/'.$uid;
if (!move_uploaded_file($_FILES['upload']['tmp_name'], $newName )){
displayUploadForm("Problem saving this file");
exit();
} else {
processUploadedPhoto($uid, $type, $albumName);
}
}
displayUploadForm("all Done");
}
function processUploadedPhoto($file, $type){
//where should the resultant file go
$resizedfile = reSizePhoto(IMAGEPATH.$file, $type);
if (!$resizedfile){
displayUploadForm("Problem resizing the photo");
unlink(IMAGEPATH.$file);
exit();
}
//save the metadata for the file
$shortdescription = addslashes (trim($_POST['shortdescription']));
$orientation = $GLOBALS['orientation'];
$fileName = $file;
saveToDatabase (array($file, $shortdesription, $orientation, $type));
}
function saveToDatabase($data){
array_walk($data, 'cleanse');
$query = "insert into ". TABLENAME ." (imageID, imagePath, imageDescription, imageOrientation, imageType) values (null, '%s', '%s', '%s', '%s')";
$result = mysql_query( vsprintf($query, $data));
if (!$result){
echo mysql_error();
exit;
} else {
return true;
}
}
function cleanse ($data){
return mysql_real_escape_string(trim($data));
}
function processUploadedZipFile($dir, $file, $albumName){
$command = "unzip $file";
//unzip the file to the new directory
exec (escapeshellcmd($command));
//iterate over the directory
$fh = opendir($dir);
while ( false !== ($file = readdir($fh))){
if (!is_file($file)){
//do nothing
}
//really need a function to test the filetype naturally here ...
$type = getfiletype($dir.$file);
$extension = getExtensionFromType($type);
if ( ($type !== "unsupported") && ($type !== "application/x-zip") ){
processUploadedPhoto($dir.$file, $type, $albumName);
} else {
//do nothing
}
}
removeDirectory($dir, false);
}
function getfiletype($file){
if (function_exists("mime_content_type")){
return(mime_content_type($file));
}else{
$ext = getExtension($file);
switch ($ext){
case "jpeg":
case "jpg":
return "image/jpeg";
break;
case "gif":
return "image/gif";
break;
case "png":
return "image/png";
break;
case "zip":
return "application/x-zip";
break;
default:
return "unsupported";
}
}
}
function getExtension($file){
$parts = pathinfo($file);
return ($parts['extension']);
}
function getExtensionFromType($type){
switch ($type){
case "image/jpeg":
return "jpeg";
break;
case "image/gif":
return "gif";
break;
case "image/png":
return "png";
break;
}
}
function reSizePhoto($file, $type){
$newsize = MAXSIZE;
//we know it is a jpeg because this is a limited applet
//but allow for future expansion!
set_time_limit(20);
switch ($type) {
case 'image/jpeg':
$src_img = imagecreatefromjpeg($file);
$func = 'imagejpeg';
$ext = '.jpeg';
break;
case 'image/gif':
$src_img = imagecreatefromgif($file);
$func = 'imagegif';
$ext = '.gif';
break;
case 'image/png':
$src_img = imagecreatefrompng($file);
$func = 'imagepng';
$ext = '.png';
break;
default:
displayUploadForm("error - file is not of a supported image type");
exit();
}
if ($src_img == '') {
displayUploadForm("error - file is not a $type file");
exit();
}
//get current image sizes
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y){
$GLOBALS['orientation'] = "hr";
if ($old_x >= $newsize){
$thumb_w=$newsize;
$thumb_h=$old_y*($newsize/$old_x);
} else {
return $file;
}
} elseif ($old_x < $old_y) {
$GLOBALS['orientation'] = "vt";
if ($old_y >= $newsize){
$thumb_w=$old_x*($newsize/$old_y);
$thumb_h=$newsize;
} else {
return $file;
}
} else {
$GLOBALS['orientation'] = "hr";
if ($old_y >= $newsize){
$thumb_w= $thumb_h = $newsize;
} else {
return $file;
}
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
$func($dst_img,$file);
imagedestroy($dst_img);
imagedestroy($src_img);
return $file;
}
function removeDirectory($dirname,$only_empty=false) {
//taken from php.net user notes on rmdir()
if (!is_dir($dirname))
return false;
$dscan = array(realpath($dirname));
$darr = array();
while (!empty($dscan)) {
$dcur = array_pop($dscan);
$darr[] = $dcur;
if ($d=opendir($dcur)) {
while ($f=readdir($d)) {
if ($f=='.' || $f=='..')
continue;
$f=$dcur.'/'.$f;
if (is_dir($f))
$dscan[] = $f;
else
unlink($f);
}
closedir($d);
}
}
$i_until = ($only_empty)? 1 : 0;
for ($i=count($darr)-1; $i>=$i_until; $i--) {
@rmdir($darr[$i]);
}
return (($only_empty)? (count(scandir)<=2) : (!is_dir($dirname)));
}
?>
function displayUploadForm($msg=NULL){
$maxsize = MAXFILESIZE;
$msg = (empty($msg)) ? NULL: '<div class="errorMessage">'.$msg.'</div>';
$shortdescription = (isset($_POST['shortdescripton'])) ? $_POST['shortdescripton'] : '';
$contentObject = <<<HTML
$msg
<form method="post" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" >
<fieldset>
<legend>Upload images</legend>
<p>You can upload gif, png or jpegs either singly or in bulk as a zip file</p>
Choose file (max $maxsize): <input type="file" name="upload" /><br/>
Short description: <input type="text" maxlength="255" name="shortdescription" value="$shortdescription" /><br/>
[red][s]Which album: $select[/s][/red]
<input type="submit" name="submit" value="Upload" />
<input type="hidden" name="action" value="uploadfile" />
</fieldset>
</form>
HTML;
echo $contentObject;
}