<?php
//error_reporting(E_ALL & ~E_NOTICE);
$db = new DB();
@ini_set('post_max_size', '64M');
@ini_set('upload_max_filesize', '64M');
$msg = "";
$smsg = "";
$emsg = "";
if (isset($_POST["main-gal-img"])) {
$valid_image_check = array("image/gif", "image/jpeg", "image/jpg", "image/png", "image/bmp");
if (count($_FILES["user_files"]) > 0) {
$folderName = "../images/gallery-uploads/";
for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {
if ($_FILES["user_files"]["name"][$i] <> "") {
$image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));
// if valid image type then upload
if (in_array($image_mime, $valid_image_check)) {
$ext = explode("/", strtolower($image_mime));
$ext = strtolower(end($ext));
$filename = rand(10000, 990000) . '_' . time() . '.' . $ext;
$filepath = $folderName . $filename;
if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) {
$emsg .= "Failed to upload <strong>" . $_FILES["user_files"]["name"][$i] . "</strong>. <br>";
$counter++;
} else {
$resizeObj = new resize($filepath);
$resizeObj->resizeImage(1400, 750, 'crop');
$resizeObj->saveImage($filepath, 80);
$resizeObj2 = new resize($filepath);
$resizeObj2->resizeImage(330, 220, 'crop');
$resizeObj2->saveImage('../images/gallery-uploads/thumb/' . $filename, 80);
//insert into database starts
try {
$query = "INSERT INTO `main-gallery` " .
"(image_name, caption) " .
"VALUES " .
"('" . mysql_real_escape_string($filename)
. "','" . mysql_real_escape_string($_POST['caption'][$i]) . "')";
$result = $db->readQuery($query);
if ($result > 0) {
// file uplaoded successfully.
} else {
// failed to insert into database.
}
} catch (Exception $ex) {
$emsg .= "<strong>" . $ex->getMessage() . "</strong>. <br>";
}
//insert into database ends
}
} else {
$emsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> not a valid image. <br>";
}
}
}
$msg .= (strlen($smsg) > 0) ? successMessage($smsg) : "";
$msg .= (strlen($emsg) > 0) ? errorMessage($emsg) : "";
} else {
$msg = errorMessage("You must upload atleast one file");
}
}
function errorMessage($str) {
return '<div style="width:50%; margin:0 auto; border:2px solid #F00;padding:2px; color:#000; margin-top:10px; text-align:center;">' . $str . '</div><br>';
}
function successMessage($str) {
return '<div style="width:50%; border:2px solid rgba(46, 128, 4, 0.28); padding:2px; color:#00B710; margin-top:10px;">' . $str . '</div><br>';
}
function getMainGalleryById($id) {
$db = new DB();
$sql = "SELECT * FROM `main-gallery` WHERE `id` = '$id' ";
$result = $db->readQuery($sql);
return mysql_fetch_assoc($result);
}
function getMainGallery() {
$db = new DB();
$sql = "SELECT * FROM `main-gallery` ORDER BY sort ASC";
$result = $db->readQuery($sql);
$array_res = array();
while ($row = mysql_fetch_array($result)) {
$property = array(
'id' => $row['id'],
'image_name' => $row['image_name'],
'caption' => $row['caption'],
'type' => $row['type'],
);
array_push($array_res, $property);
}
return $array_res;
}
|