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:
2025-03-29 22:57:43 -06:00
parent 9a45075010
commit ff7e799b7f
14 changed files with 609 additions and 130 deletions
+86 -1
View File
@@ -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>