Skip to content

Instantly share code, notes, and snippets.

@Alphabetus
Last active Jan 8, 2019
Embed
What would you like to do?
// PHOTO PLANT ################################################################################################################
function photoPlant($pID){
// db
include "includes/dbConfig.php";
// init
$out = null;
// gen hash
$varA = microtime();
$varB = time();
$varC = $varA . $varB;
$hash = md5($varC);
// prepare upload
$currentDir = getcwd();
$uploadDirectory = "/gallery/";
$errors = []; // Store all foreseen and unforseen errors here
$fileExtensions = ['jpeg','jpg','png', '']; // Get all the file extensions, including empty for mobile
// reformat empty file extension
if ($fileExtension === ""){
$fileExtension = "jpg";
}
$fileName = $_FILES['photo']['name'];
$fileTmpName = $_FILES['photo']['tmp_name'];
$fileSize = $_FILES['photo']['size'];
$fileType = $_FILES['photo']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
// reformat filename
$fileName = $hash . "." . $fileExtension;
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
if (! in_array($fileExtension,$fileExtensions)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 8000000) {
$errors[] = "This file is more than 8MB. Sorry, it has to be less than or equal to 8MB";
}
// rotate img to exif
$errors = array_filter($errors);
if (empty($errors)){
$img = new Imagick($fileTmpName);
$orient = $img->getImageOrientation();
if($orient === 6){
// we need to rotate it 90deg
$img->rotateImage("rgba(255, 255, 255, 0.0)", 90);
}
if ($orient === 3){
// we need to rotate it 180deg
$img->rotateImage("rgba(255, 255, 255, 0.0)", 180);
}
$img->writeImage("gallery/" . $fileName);
}
else{
$out .= "Errors on upload";
}
// store img on db
// prepare data
$timeStamp = time();
// query
$query = mysqli_query($con, "INSERT INTO photo_table (photo_parent_id, photo_name, photo_timestamp) VALUES ($pID, '$fileName', $timeStamp)");
// run query
if (!$query){
$out .= mysqli_error($con);
}
else{
$out .= "ok";
// $out .= $orient;
}
// return
return $out;
}
// action photo plant
if (isset($_POST["plantPhotoID"])){
if (photoPlant($_POST["plantPhotoID"]) === "ok"){
header("Location: ".$_SERVER["PHP_SELF"]."?view=grow&growID=".$_GET["growID"]."");
}
else{
// for debug reasons ill give feedback on this one
print photoPlant($_POST["plantPhotoID"]);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment