From 02608271bb2a9f3a65ed536984d306ee14b48e34 Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Sun, 12 Jul 2026 15:51:52 +0300 Subject: initialize repository; add docker config; migrate configuration to new specs --- public/upload.php | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 public/upload.php (limited to 'public/upload.php') diff --git a/public/upload.php b/public/upload.php new file mode 100644 index 0000000..1ed278d --- /dev/null +++ b/public/upload.php @@ -0,0 +1,193 @@ + 1048576) { // limit filesize to 1MB + $uploadOk = -2; + echo '{"success": false, "err" : "SIZE ERROR"}'; die(); + } + + // Allow certain file formats (only jpeg) + // --------------------------------------------------------------------------- + if ($ext != "jpg" && $ext != "jpeg" ) { // file extension shall be [ jpg | jpeg ] + $uploadOk = -3; + echo '{"success": false, "err" : "JPEG ERROR"}'; die(); + } + // Check if $uploadOk is set to 0 by an error + // (though this would have ended the script already) ------------------------- + if ($uploadOk < 0) { + echo '{"success": false, "err" : "IMG-ERR:'. (-$uploadOk) .'"}'; die(); + + } + // if everything is ok, try to upload the file info folder + // --------------------------------------------------------------------------- + else { + + if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $path_file)) { + // image uploaded; + + // delete old image + old thumbnail + ////////////////////////////////////////////////////////////////////// + if ($delete_old) { + unlink(UPLOAD_DIR. $sub_dir . $del); // delete old image + unlink(UPLOAD_DIR. $sub_dir .'pi'. $del); // delete old thumbnail + } + + // Create thumbnail + ////////////////////////////////////////////////////////////////////// + + // load image and get image size + $img = imagecreatefromjpeg( $path_file ); + $width = imagesx( $img ); + $height = imagesy( $img ); + + // calculate thumbnail size + $new_width = THUMBWIDTH; + $new_height = floor( $height * ( THUMBWIDTH / $width ) ); + + // create a new temporary image + $tmp_img = imagecreatetruecolor( $new_width, $new_height ); + + // copy and resize old image into new image + imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); + + // save thumbnail into a file + imagejpeg( $tmp_img, $path_thumb ); + + + // Update item record on database + ////////////////////////////////////////////////////////////////////// + + // init database connection + $_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS); + if ($_dbc->connect_errno) { + echo "Database connection failed; Please try in a few minutes."; + if (TESTING) echo $_dbc->connect_error; + $_dbc->close(); die(); + } + $_dbc->set_charset("utf8"); + + if (IS_2ND_IMG) + $_upd = $_dbc->prepare("UPDATE items SET img2 = ? WHERE id = ?"); + else + $_upd = $_dbc->prepare("UPDATE items SET img = ? WHERE id = ?"); + $_upd->bind_param("sd", $localFN, $id); + $_upd->execute(); + $_upd->close(); + $_dbc->close(); + + + + // send image thumbnail for preview + // ------------------------------------------------------------------- + + echo '{ "success" : true, "img" : "'. $localFN .'", "sec" : "'. (IS_2ND_IMG ? 'true' : 'false') .'", "thumbnail" : "'. $path_thumb .'" }'; + die(); // everything done! + + } + else { + echo '{"success": false, "err" : "UPLOAD ERR"}'; die(); + } + } -- cgit v1.2.3