Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Handles the image uploads
- * Author: Ayan Dey
- */
- error_reporting(E_WARNING | E_PARSE);
- require "dbClass.php";
- $db = new DB();
- date_default_timezone_set('America/Los_Angeles');
- $timeStamp = time();
- // Upload path
- $path = "../uploads/";
- if($_SERVER["REQUEST_METHOD"] === "POST") {
- $error = "";
- $message = "";
- $name = filter_var($_POST["heading"], FILTER_SANITIZE_STRING);
- if(empty($name)) {
- $error = "Please type your name..";
- }
- else {
- if($_FILES["fileUpload"]["error"] === 0) {
- $filename = $_FILES["fileUpload"]["name"];
- $fileExt = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
- if($fileExt === "jpg") {
- $tmpFile = $_FILES["fileUpload"]["tmp_name"];
- $newFile = preg_replace('/\s+/', '', $name) . "_" . $timeStamp . "." . $fileExt;
- if(move_uploaded_file($tmpFile, $path . $newFile)) {
- $db->query("INSERT INTO files(name, filename) VALUES(:name, :filename)", array("name"=> $name, "filename"=> $newFile));
- imageFixOrientation($path . $newFile);
- $message = "File Upload Successful";
- }
- else {
- $error = "Failed to upload the snap..";
- }
- }
- else {
- $error = "Wrong file type!";
- }
- }
- elseif ($_FILES["fileUpload"]["error"] === 4) {
- $error = "No file selected..";
- }
- else {
- $error = "Some error occured while uploading the file. Error Code: ".$_FILES["fileUpload"]["error"];
- }
- }
- if(empty($error)) {
- echo json_encode(array("MSG" => $message, "CLASS" => "success", "code" => 0));
- }
- else {
- echo json_encode(array("MSG" => $error, "CLASS" => "error", "code" => 1));
- }
- }
- function imageFixOrientation($filename) {
- if (function_exists('exif_read_data')) {
- $exif = exif_read_data($filename);
- if($exif && isset($exif['Orientation'])) {
- $orientation = $exif['Orientation'];
- if($orientation != 1){
- $img = imagecreatefromjpeg($filename);
- $deg = 0;
- switch ($orientation) {
- case 3:
- $deg = 180;
- break;
- case 6:
- $deg = 270;
- break;
- case 8:
- $deg = 90;
- break;
- }
- if ($deg) {
- $img = imagerotate($img, $deg, 0);
- }
- // then rewrite the rotated image back to the disk as $filename
- imagejpeg($img, $filename, 95);
- } // if there is some rotation necessary
- } // if have the exif orientation info
- } // if function exists
- }
Add Comment
Please, Sign In to add comment