I would suggest using Intervention Library as it makes handling images so much easier.
I use the following for my own website ->
<?php
// Include the configuration file and autoload file from the composer.
require_once __DIR__ . '/../config/clearwebconfig.php';
require_once "vendor/autoload.php";
use Intervention\Image\ImageManagerStatic as Image;
// Import the ErrorHandler and Database classes from the clearwebconcepts namespace.
use clearwebconcepts\{
ErrorHandler,
Database,
ImageContentManager,
LoginRepository as Login
};
$errorHandler = new ErrorHandler();
// Register the exception handler method
set_exception_handler([$errorHandler, 'handleException']);
$database = new Database();
$pdo = $database->createPDO();
$checkStatus = new Login($pdo);
// To check for either 'member' or 'sysop'
if ($checkStatus->check_security_level(['sysop'])) {
// Grant access
} else {
// Access denied
header('location: dashboard.php');
exit();
}
function is_ajax_request(): bool
{
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}
$save_result = false;
if (($_SERVER['REQUEST_METHOD'] === 'POST') && isset($_FILES['image'])) {
$data = $_POST['cms'];
$data['content'] = trim($data['content']);
$errors = array();
$exif_data = [];
$file_name = $_FILES['image']['name']; // Temporary file:
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$thumb_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
/*
* Set EXIF data info of image for database table that is
* if it contains the info otherwise set to null.
*/
if ($file_ext === 'jpeg' || $file_ext === 'jpg') {
$exif_data = exif_read_data($file_tmp);
if (array_key_exists('Make', $exif_data) && array_key_exists('Model', $exif_data)) {
$data['Model'] = $exif_data['Make'] . ' ' . $exif_data['Model'];
}
if (array_key_exists('ExposureTime', $exif_data)) {
$data['ExposureTime'] = $exif_data['ExposureTime'] . "s";
}
if (array_key_exists('ApertureFNumber', $exif_data['COMPUTED'])) {
$data['Aperture'] = $exif_data['COMPUTED']['ApertureFNumber'];
}
if (array_key_exists('ISOSpeedRatings', $exif_data)) {
$data['ISO'] = "ISO " . $exif_data['ISOSpeedRatings'];
}
if (array_key_exists('FocalLengthIn35mmFilm', $exif_data)) {
$data['FocalLength'] = $exif_data['FocalLengthIn35mmFilm'] . "mm";
}
} else {
$data['Model'] = null;
$data['ExposureTime'] = null;
$data['Aperture'] = null;
$data['ISO'] = null;
$data['FocalLength'] = null;
}
$data['content'] = trim($data['content']);
$extensions = array("jpeg", "jpg", "png");
if (in_array($file_ext, $extensions, true) === false) {
$errors[] = "extension not allowed, please choose a JPEG or PNG file.";
}
if ($file_size >= 58720256) {
$errors[] = 'File size must be less than or equal to 42 MB';
}
/*
* Create unique name for image.
*/
$image_random_string = bin2hex(random_bytes(16));
$image_path = 'assets/image_path/img-entry-' . $image_random_string . '-2048x1365' . '.' . $file_ext;
$thumb_path = 'assets/thumb_path/thumb-entry-' . $image_random_string . '-600x400' . '.' . $file_ext;
move_uploaded_file($file_tmp, $image_path);
move_uploaded_file($thumb_tmp, $thumb_path);
// Load the image
$image = Image::make($image_path);
// Resize the image
$image->resize(2048, 1365, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
// Save the new image
$image->save($image_path, 100);
// Load the image with Intervention Image
$image = Image::make($image_path);
// Resize the image while maintaining the aspect ratio
$image->resize(600, 400, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
});
// Save the thumbnail
$image->save($thumb_path, 100);
$data['image_path'] = $image_path;
$data['thumb_path'] = $thumb_path;
/*
* If no errors save ALL the information to the
* database table.
*/
if (empty($errors) === true) {
// Save to Database Table CMS
$timezone = new DateTimeZone('America/Detroit'); // Use your timezone here
$today = new DateTime('now', $timezone);
$data['date_updated'] = $data['date_added'] = $today->format("Y-m-d H:i:s");
$cms = new ImageContentManager($pdo, $data);
$result = $cms->create();
if ($result) {
header('Content-Type: application/json');
echo json_encode(['status' => 'success']);
exit();
}
} else {
if (is_ajax_request()) {
// Send a JSON response with errors for AJAX requests
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'errors' => $errors]);
}
}
}
I log my errors to a log file that I can only see and a person needs to be login to my website even to upload a file.
Maybe the above can you help you out a little. Just remember nothing is full proof, but you should make the code as tight as possible.