implemented image upload and cropping system, mostly work. it's too zoomed in and appears to be using the xy coords from the top left of the crop selector.
This commit is contained in:
+120
-76
@@ -29,35 +29,52 @@ function delete_self()
|
||||
return -1;
|
||||
}
|
||||
|
||||
function create_account($username, $email, $password, $verify_password, $role_id, $profile_picture)
|
||||
function cookies($user, $time)
|
||||
{
|
||||
if ($password != $verify_password) {
|
||||
$error_msg = '
|
||||
<div class="error">
|
||||
Passwords do not match!
|
||||
</div>
|
||||
';
|
||||
setcookie('user_id', $user['user_id'], $time);
|
||||
setcookie('username', $user['username'], $time);
|
||||
setcookie('email', $user['email'], $time);
|
||||
setcookie('role_id', $user['role_id'], $time);
|
||||
setcookie('profile_picture', $user['profile_picture'], $time);
|
||||
}
|
||||
|
||||
return $error_msg;
|
||||
function logout()
|
||||
{
|
||||
foreach (array_keys($_COOKIE) as $key) {
|
||||
echo 'unsetting ' . $key . ' now.';
|
||||
setcookie($key, '', time() - 3600, '/');
|
||||
unset($_COOKIE[$key]);
|
||||
}
|
||||
|
||||
$password_hash = hash('sha256', $password);
|
||||
header('Location: articles.php');
|
||||
}
|
||||
|
||||
$id = rand(1000, 999999999);
|
||||
while (user_id_exists($id))
|
||||
$id = rand(1000, 999999999);
|
||||
function login_form($error_msg = '')
|
||||
{
|
||||
$form = '
|
||||
<div class="form_card">
|
||||
<h3 class="underline">Log In</h3>
|
||||
<form method="post">
|
||||
<input type="hidden" name="form_id" value="login_form">
|
||||
|
||||
if (!empty($profile_picture)) {
|
||||
$filetype = explode('.', $profile_picture);
|
||||
$pic_path = $id . $filetype[count($filetype) - 1];
|
||||
}
|
||||
<label for="username">Username / Email</label>
|
||||
<input id="username" name="username" required>
|
||||
|
||||
$stmt = 'INSERT INTO user (user_id, username, email, password_hash, role_id' . (!empty($profile_picture) ? ', profile_picture' : '') . ') VALUES (' . $id . ', "' . $username . '", "' . $email . '", "' . $password_hash . '", ' . $role_id . (!empty($profile_picture) ? '", "' . $pic_path . '"' : '') . ');';
|
||||
exec_statement($stmt, 0);
|
||||
<label for="password">Password</label>
|
||||
<input id="password" name="password" type="password" required>
|
||||
|
||||
login($username, $password, false);
|
||||
<label class="form_checkbox_container">Stay Logged In?
|
||||
<input name="stay_logged_in" type="checkbox" value="true">
|
||||
<span class="checkmark"></span>
|
||||
</label>
|
||||
|
||||
return true;
|
||||
<input class="button" type="submit" value="Log In">
|
||||
' . $error_msg . '
|
||||
</form>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function login($username, $password, $stay_logged_in)
|
||||
@@ -86,58 +103,17 @@ function login($username, $password, $stay_logged_in)
|
||||
return true;
|
||||
}
|
||||
|
||||
function cookies($user, $time)
|
||||
{
|
||||
setcookie('user_id', $user['user_id'], $time);
|
||||
setcookie('username', $user['username'], $time);
|
||||
setcookie('email', $user['email'], $time);
|
||||
setcookie('role_id', $user['role_id'], $time);
|
||||
setcookie('profile_picture', $user['profile_picture'], $time);
|
||||
}
|
||||
|
||||
function logout()
|
||||
{
|
||||
foreach (array_keys($_COOKIE) as $key) {
|
||||
echo 'unsetting ' . $key . ' now.';
|
||||
setcookie($key, '', time() - 3600, '/');
|
||||
unset($_COOKIE[$key]);
|
||||
}
|
||||
|
||||
header('Location: articles.php');
|
||||
}
|
||||
|
||||
function login_form($error_msg = '')
|
||||
{
|
||||
$form = '
|
||||
<div class="form_card">
|
||||
<h3 class="underline">Log In</h3>
|
||||
<form method="post">
|
||||
<label for="username">Username / Email</label>
|
||||
<input id="username" name="username" required>
|
||||
|
||||
<label for="password">Password</label>
|
||||
<input id="password" name="password" type="password" required>
|
||||
|
||||
<label class="form_checkbox_container">Stay Logged In?
|
||||
<input name="stay_logged_in" type="checkbox" value="true">
|
||||
<span class="checkmark"></span>
|
||||
</label>
|
||||
|
||||
<input class="button" type="submit" value="Log In">
|
||||
' . $error_msg . '
|
||||
</form>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function signup_form($error_msg = '')
|
||||
{
|
||||
$form = '
|
||||
<div class="form_card">
|
||||
<h3 class="underline">Sign Up</h3>
|
||||
<form method="post">
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
<input type="hidden" name="form_id" value="signup_form">
|
||||
<input type="hidden" name="crop_x" value="" id="crop_x">
|
||||
<input type="hidden" name="crop_y" value="" id="crop_y">
|
||||
<input type="hidden" name="crop_width" value="" id="crop_width">
|
||||
|
||||
<label for="username">Username</label>
|
||||
<input id="username" name="username" required>
|
||||
|
||||
@@ -150,8 +126,9 @@ function signup_form($error_msg = '')
|
||||
<label for="verify_password">Verify Password</label>
|
||||
<input id="verify_password" name="verify_password" type="password" required>
|
||||
|
||||
<label for="profile_picture_upload" class="upload_button">Upload a Profile Picture</label>
|
||||
<input id="profile_picture_upload" name="profile_picture_upload" type="file" accept="image/png, image/jpeg">
|
||||
<label for="profile_picture" class="upload_button modal_button" id="profile_cropper_button">Upload a Profile Picture</label>
|
||||
<input id="profile_picture" class="file_input" name="profile_picture" type="file" accept="image/png, image/jpeg, image/jpg">
|
||||
<small class="file_input_feedback">No file selected.</small>
|
||||
|
||||
<label class="form_checkbox_container">Stay Logged In?
|
||||
<input name="stay_logged_in" type="checkbox" value="true">
|
||||
@@ -161,12 +138,80 @@ function signup_form($error_msg = '')
|
||||
<input class="button" type="submit" value="Sign Up">
|
||||
' . $error_msg . '
|
||||
</form>
|
||||
|
||||
<div id="user_actions_modal" class="modal">
|
||||
<div id="modal_content">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="display: none;">
|
||||
<div id="profile_cropper" class="modal_form">' . profile_cropper() . '</div>
|
||||
</div>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
function signup($username, $email, $password, $verify_password, $role_id, $upload = null, $x = null, $y = null, $crop_width = null)
|
||||
{
|
||||
if ($password != $verify_password) {
|
||||
$error_msg = '
|
||||
<div class="error">
|
||||
Passwords do not match!
|
||||
</div>
|
||||
';
|
||||
|
||||
return $error_msg;
|
||||
}
|
||||
|
||||
$password_hash = hash('sha256', $password);
|
||||
|
||||
$id = rand(1000, 999999999);
|
||||
while (user_id_exists($id))
|
||||
$id = rand(1000, 999999999);
|
||||
|
||||
if (!empty($upload)) {
|
||||
$orig_size = getimagesize($upload['profile_picture']['tmp_name']);
|
||||
$orig_width = $orig_size[0];
|
||||
$orig_height = $orig_size[1];
|
||||
|
||||
$crop_size = min($orig_width, $orig_height) * .56;
|
||||
|
||||
crop_image($upload['profile_picture']['tmp_name'], $x, $y, $crop_size, $crop_size);
|
||||
$target = upload($upload['profile_picture'], $_ENV['PROFILE_IMAGES_FQ_PATH'], $id);
|
||||
}
|
||||
|
||||
$stmt = 'INSERT INTO user (user_id, username, email, password_hash, role_id' . (!empty($target) ? ', profile_picture' : '') . ') VALUES (' . $id . ', "' . $username . '", "' . $email . '", "' . $password_hash . '", ' . $role_id . (!empty($target) ? ', "' . $target . '"' : '') . ');';
|
||||
exec_statement($stmt, 0);
|
||||
|
||||
login($username, $password, false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function profile_cropper()
|
||||
{
|
||||
$display = '
|
||||
<div class="form_card">
|
||||
<img src="IMG_UPLOAD_PATH" id="img_to_crop"/>
|
||||
<div id="img_crop_selection">
|
||||
<div class="corner" id="corner_nw"></div>
|
||||
<div class="corner" id="corner_ne"></div>
|
||||
<div class="corner" id="corner_sw"></div>
|
||||
<div class="corner" id="corner_se"></div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<button id="crop_button" class="button">Crop</button>
|
||||
<button id="crop_cancel" class="button">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
';
|
||||
|
||||
return $display;
|
||||
}
|
||||
|
||||
function reset_pw_form()
|
||||
{
|
||||
$form = '
|
||||
@@ -413,7 +458,7 @@ function user_view($user_id)
|
||||
<div class="row">
|
||||
<h1>' . $user['username'] . '</h1>
|
||||
<div class="card_pic_box">
|
||||
<img src="/profile_pictures/' . $user['profile_picture'] . '" class="profile_picture" />
|
||||
<img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $user['profile_picture'] . '" class="profile_picture" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="line"></div>
|
||||
@@ -425,15 +470,14 @@ function user_view($user_id)
|
||||
// <div id="request_role_form" class="modal_form">' . request_role_change_form() . '</div>
|
||||
$view .= '
|
||||
<div class="user_actions">
|
||||
<a class="modal_button underline">Reset Password</a>
|
||||
<a class="modal_button underline">Update Email</a>
|
||||
<a class="modal_button underline">Update Username</a>
|
||||
<a class="modal_button underline">Delete Account</a>
|
||||
<button id="reset_pw_form_button" class="modal_button underline">Reset Password</button>
|
||||
<button id="update_email_form_button" class="modal_button underline">Update Email</button>
|
||||
<button id="update_username_form_button" class="modal_button underline">Update Username</button>
|
||||
<button id="delete_account_form_button" class="modal_button underline">Delete Account</button>
|
||||
</div>
|
||||
|
||||
<div id="user_actions_modal" class="modal">
|
||||
<div class="modal_content">
|
||||
<div id="modal_content"></div>
|
||||
<div id="modal_content">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -46,12 +46,29 @@ function article_page_from_markdown($article_id)
|
||||
{
|
||||
$query = 'SELECT * FROM article WHERE article_id = ' . $article_id . ';';
|
||||
$article = query_one_result($query);
|
||||
|
||||
$query = 'SELECT user_id, username, profile_picture FROM user WHERE user_id = ' . $article['author_id'] . ';';
|
||||
$author = query_one_result($query);
|
||||
|
||||
$markdown = read_file_one_string($_ENV['ARTICLES_PATH'] . $article_id . '/article.md');
|
||||
|
||||
$parsedown = new Parsedown();
|
||||
|
||||
$html = '<div class="article">';
|
||||
$html .= '<h3>' . $article['title'] . '</h3>';
|
||||
$html = '
|
||||
<div class="article">
|
||||
<h3>' . $article['title'] . '</h3>
|
||||
<div class="row">
|
||||
<div class="col_right">
|
||||
<h5>Written by <a href="user.php?action=view&user_id=' . $author['user_id'] . '" style="font-size: 1.5vw;">' . $author['username'] . '</a></h5>
|
||||
<p>Published on ' . $article['published_at'] . '</p>
|
||||
</div>
|
||||
<div class="card_pic_box">
|
||||
<img src="/profile_pictures/' . $author['profile_picture'] . '" class="card_profile_picture" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="line" style="margin-bottom: 15px;"></div>
|
||||
';
|
||||
|
||||
$html .= $parsedown->text($markdown);
|
||||
$html .= '</div>';
|
||||
|
||||
@@ -91,7 +108,7 @@ function article_card($article_id)
|
||||
<small>Published: ' . $article['published_at'] . '</small>
|
||||
</div>
|
||||
<div class="card_pic_box">
|
||||
<img src="/profile_pictures/' . $author['profile_picture'] . '" class="card_profile_picture">
|
||||
<img src="' . $_ENV['PROFILE_IMAGES_PQ_PATH'] . $author['profile_picture'] . '" class="card_profile_picture">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+86
-1
@@ -1,5 +1,85 @@
|
||||
<?php
|
||||
|
||||
if (!empty($_FILES['upload'])) {
|
||||
require_once '../vendor/autoload.php';
|
||||
|
||||
// Load environment variables.
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
|
||||
$dotenv->safeLoad();
|
||||
upload($_FILES['upload'], $_ENV['UPLOAD_TMP_FQ_PATH'], isset($_POST['filename']) ? $_POST['filename'] : null);
|
||||
}
|
||||
|
||||
function upload($input, $dest_dir, $filename = null)
|
||||
{
|
||||
$filetype = strtolower(pathinfo($input['name'], PATHINFO_EXTENSION));
|
||||
|
||||
switch ($filetype) {
|
||||
case 'png':
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
if (check_image_upload($dest_dir, $filetype, $input)) {
|
||||
move_uploaded_file($input['tmp_name'], $dest_dir . ($filename ? $filename . '.' . $filetype : $input['name']));
|
||||
if ($filename)
|
||||
return basename($filename . '.' . $filetype);
|
||||
else
|
||||
return basename($input['name'] . '.' . $filetype);
|
||||
} else
|
||||
return '';
|
||||
break;
|
||||
default:
|
||||
# code...
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function crop_image($img_path, $x, $y, $height, $width)
|
||||
{
|
||||
//$dest = $_ENV['UPLOAD_TMP_FQ_PATH'];
|
||||
$dest = '/tmp/';
|
||||
// Get the image type
|
||||
$imageType = exif_imagetype($img_path);
|
||||
|
||||
// Create an image resource from the source image
|
||||
switch ($imageType) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$sourceImage = imagecreatefromjpeg($img_path);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$sourceImage = imagecreatefrompng($img_path);
|
||||
break;
|
||||
default:
|
||||
return false; // Unsupported image type
|
||||
}
|
||||
|
||||
if (!$sourceImage)
|
||||
return false; // Failed to create image resource
|
||||
|
||||
// Create a new image resource for the cropped image
|
||||
$croppedImage = imagecreatetruecolor($width, $height);
|
||||
|
||||
// Copy the cropped portion from the source image to the destination image
|
||||
imagecopy($croppedImage, $sourceImage, 0, 0, $x, $y, $width, $height);
|
||||
|
||||
// Save the cropped image to the destination path
|
||||
switch ($imageType) {
|
||||
case IMAGETYPE_JPEG:
|
||||
imagejpeg($croppedImage, $img_path);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
imagepng($croppedImage, $dest);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
imagegif($croppedImage, $dest);
|
||||
break;
|
||||
}
|
||||
|
||||
// Free up memory
|
||||
imagedestroy($sourceImage);
|
||||
imagedestroy($croppedImage);
|
||||
|
||||
return true; // Cropping successful
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads in file contents and stores as a single string.
|
||||
*/
|
||||
@@ -10,9 +90,14 @@ function read_file_one_string($file_path)
|
||||
return $string;
|
||||
}
|
||||
|
||||
function check_image_upload($target, $filetype, $image)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
function pretty_dump($arr)
|
||||
{
|
||||
return '
|
||||
echo '
|
||||
<pre>
|
||||
' . print_r($arr) . '
|
||||
</pre>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 MiB |
Reference in New Issue
Block a user