-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-image.php
More file actions
163 lines (119 loc) · 4.79 KB
/
new-image.php
File metadata and controls
163 lines (119 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-sessions.php");
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-db-connection.php");
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-functions.php");
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-header.php");
?>
<main>
<?php $user = getUsersDetails($member); ?>
<?php
if (!$user || $user['member_is_admin'] != "yes") {
stderr("<strong>Protected</strong> page.");
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-footer.php");
die();
}
?>
<div class="row">
<div class="col-md-3">
<div class="card">
<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-dashboard.php"); ?>
</div>
<div class="card">
<?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-dashboard-extended.php"); ?>
</div>
</div>
<div class="col-md-9">
<div class="card">
<div class="card-header"><i class="fas fa-image"></i> New Image</div>
<div class="card-body">
<?php
$errors = [];
if (isset($_POST['submitImage'])) {
$filename = preg_replace("/[^a-zA-Z0-9.]/", "_", $_FILES['post_image']['name']);
// Determine if the image is a header based on the form input
$isHeader = isset($_POST['post_image_header']) && $_POST['post_image_header'] == 'yes';
if (!empty($_FILES['post_image']['name']) && empty($_POST['post_image_header'])) {
$imageName = uploadImage(
strtolower($filename),
$_FILES['post_image']['tmp_name'],
strtolower($_POST['post_image_alt_text']),
!$isHeader,
$isHeader
);
if (!$imageName) {
stderr("<strong>Error:</strong> Failed to upload image for non-header.");
return;
}
$i = DB::getInstance()->insert(
'images',
[
'image_name' => $imageName,
'image_alt_text' => $_POST['post_image_alt_text'],
'image_is_header' => "no",
'image_date' => date('Y-m-d H:i:s')
]
);
if (!$i) {
stderr("<strong>Error:</strong> Failed to insert non-header image into database.");
return;
}
stdmsg("Your new <strong>image</strong> has been <strong>uploaded</strong>.");
}
if (!empty($_FILES['post_image']['name']) && !empty($_POST['post_image_header'])) {
$imageName = uploadImage(
strtolower($filename),
$_FILES['post_image']['tmp_name'],
strtolower($_POST['post_image_alt_text']),
!$isHeader,
$isHeader
);
if (!$imageName) {
stderr("<strong>Error:</strong> Failed to upload image for header.");
return;
}
$resizedImage = resizeImage("uploads/" . $imageName, "uploads/" . $imageName, [100, 100]);
if (!$resizedImage) {
stderr("<strong>Error:</strong> Failed to resize header image.");
return;
}
$i = DB::getInstance()->insert(
'images',
[
'image_name' => $imageName,
'image_alt_text' => $_POST['post_image_alt_text'],
'image_is_header' => "yes",
'image_date' => date('Y-m-d H:i:s')
]
);
if (!$i) {
stderr("<strong>Error:</strong> Failed to insert header image into database.");
return;
}
stdmsg("Your new <strong>image</strong> has been <strong>uploaded</strong> and <strong>resized</strong>.");
}
}
?>
<form action="new-image.php" method="post" enctype="multipart/form-data">
<div class="mb-3">
<label for="post_image" class="form-label"><strong>Image:</strong></label>
<input class="form-control" type="file" id="post_image" name="post_image" required>
</div>
<div class="mb-3">
<label for="post_image_alt_text" class="form-label"><strong><span class="text-success">Image ALT Text:</span></strong></label>
<input type="text" class="form-control" id="post_image_alt_text" name="post_image_alt_text" required>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="post_image_header" name="post_image_header">
<label class="form-check-label" for="flexSwitchCheckChecked"><small>Use this as a header image. will be resized to <strong>100</strong> width x <strong>100</strong> height.</small></label>
</div>
<hr />
<button type="submit" name="submitImage" class="btn btn-success float-end"><i class="fas fa-plus"></i> New Image</button>
</form>
</div>
</div>
</div>
</div>
</main>
<?php
include($_SERVER['DOCUMENT_ROOT'] . "/includes/inc-footer.php");
?>