2022-08-10 13:05:15 +00:00
< ? php
/*
|-------------------------------------------------------------
| Uploading Images
|-------------------------------------------------------------
| gwa gwa
|-------------------------------------------------------------
*/
session_start ();
// Include server connection
include " ../server/conn.php " ;
if ( isset ( $_POST [ 'submit' ])) {
2022-08-10 14:55:15 +00:00
if ( isset ( $_SESSION [ 'id' ])) {
// Root paths
$dir = " ../../images/ " ;
$thumb_dir = $dir . " thumbnails/ " ;
2022-08-10 13:05:15 +00:00
2022-09-07 14:29:53 +00:00
// File name updating
$file_type = pathinfo ( $dir . $_FILES [ 'image' ][ 'name' ], PATHINFO_EXTENSION );
$image_newname = " IMG_ " . $_SESSION [ " username " ] . " _ " . round ( microtime ( true )) . " . " . $file_type ;
$image_path = $dir . $image_newname ;
2022-08-10 13:05:15 +00:00
2022-08-10 14:55:15 +00:00
// Allowed file types
$allowed_types = array ( 'jpg' , 'jpeg' , 'png' , 'webp' );
if ( in_array ( $file_type , $allowed_types )) {
// Move file to server
if ( move_uploaded_file ( $_FILES [ 'image' ][ 'tmp_name' ], $image_path )) {
// Attempt making a thumbnail
try {
$image_thumbnail = new Imagick ( $image_path );
$image_thumbnail -> resizeImage ( 300 , null , null , 1 , null );
2022-09-07 14:29:53 +00:00
$image_thumbnail -> writeImage ( $thumb_dir . $image_newname );
2022-08-10 14:55:15 +00:00
} catch ( Exception $e ) {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 14:55:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( 'Gwha!' , 'We hit a small roadbump during making of the thumbail. We will continue anyway!' , 'var(--black)' , 'assets/icons/bug.svg' );
2022-08-10 14:55:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 14:55:15 +00:00
}
2022-08-10 13:05:15 +00:00
2022-08-10 14:55:15 +00:00
// Prepare sql for destruction and filtering the sus
$sql = " INSERT INTO swag_table (imagename, alt, author) VALUES (?, ?, ?) " ;
2022-08-10 13:05:15 +00:00
2022-08-10 14:55:15 +00:00
if ( $stmt = mysqli_prepare ( $conn , $sql )) {
// Bind the smelly smelly
mysqli_stmt_bind_param ( $stmt , " sss " , $param_image_name , $param_alt_text , $param_user_id );
2022-08-10 13:05:15 +00:00
2022-08-10 14:55:15 +00:00
// Setting up parameters
2022-09-07 14:29:53 +00:00
$param_image_name = $image_newname ;
2022-08-10 14:55:15 +00:00
$param_alt_text = $_POST [ 'alt' ];
$param_user_id = $_SESSION [ 'id' ];
2022-08-10 13:05:15 +00:00
2022-08-10 14:55:15 +00:00
// Attempt to execute the prepared statement
if ( mysqli_stmt_execute ( $stmt )) {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 14:55:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( ':3' , 'Your Image uploaded successfully!' , 'var(--green)' , 'assets/icons/check.svg' );
2022-08-10 14:55:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 14:55:15 +00:00
} else {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 14:55:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( ':c' , 'Something went fuckywucky, please try later' , 'var(--red)' , 'assets/icons/cross.svg' );
2022-08-10 14:55:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 14:55:15 +00:00
}
2022-08-10 13:05:15 +00:00
}
2022-08-10 14:55:15 +00:00
} else {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 14:55:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( 'Hmmff' , 'Something happened when moving the file to the server. This may just been a 1-off so try again' , 'var(--red)' , 'assets/icons/bug.svg' );
2022-08-10 14:55:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 13:05:15 +00:00
}
} else {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 13:05:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( 'Woopsie' , 'The file type you are trying to upload is not supported. Supported files include: JPEG, JPG, PNG and WEBP' , 'var(--red)' , 'assets/icons/cross.svg' );
2022-08-10 13:05:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 13:05:15 +00:00
}
} else {
2022-08-14 21:27:10 +00:00
?>
2022-08-10 13:05:15 +00:00
< script >
2022-09-07 14:29:53 +00:00
sniffleAdd ( 'Denied!!!' , 'As you are not loggedin, your upload has been stopped, L' , 'var(--red)' , 'assets/icons/cross.svg' );
2022-08-10 13:05:15 +00:00
</ script >
2022-08-14 21:27:10 +00:00
< ? php
2022-08-10 13:05:15 +00:00
}
}