1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-09 06:36:52 +02:00

fix(media): fix error - Function validateImage not found. #477

This commit is contained in:
Awilum
2020-10-16 19:07:13 +03:00
parent e3c7d88afb
commit 297f099e1e

View File

@@ -103,40 +103,7 @@ class MediaFiles
) {
// Validation rule to test if an upload is an image and, optionally, is the correct size.
if (in_array(mime_content_type($file['tmp_name']), ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) {
function validateImage($file, $max_image_width, $max_image_height, $exact)
{
try {
// Get the width and height from the uploaded image
[$width, $height] = getimagesize($file['tmp_name']);
} catch (\ErrorException $e) {
// Ignore read errors
}
if (empty($width) or empty($height)) {
// Cannot get image size, cannot validate
return false;
}
if (! $max_image_width) {
// No limit, use the image width
$max_image_width = $width;
}
if (! $max_image_height) {
// No limit, use the image height
$max_image_height = $height;
}
if ($exact) {
// Check if dimensions match exactly
return $width === $max_image_width and $height === $max_image_height;
}
// Check if size is within maximum dimensions
return $width <= $max_image_width and $height <= $max_image_height;
}
if (validateImage($file, $max_image_width, $max_image_height, $exact) === false) {
if ($this->validateImage($file, $max_image_width, $max_image_height, $exact) === false) {
return false;
}
}
@@ -408,4 +375,40 @@ class MediaFiles
{
return PATH['project'] . '/uploads/' . $id;
}
/**
* Validate Image
*/
protected function validateImage($file, $max_image_width, $max_image_height, $exact)
{
try {
// Get the width and height from the uploaded image
[$width, $height] = getimagesize($file['tmp_name']);
} catch (\ErrorException $e) {
// Ignore read errors
}
if (empty($width) or empty($height)) {
// Cannot get image size, cannot validate
return false;
}
if (! $max_image_width) {
// No limit, use the image width
$max_image_width = $width;
}
if (! $max_image_height) {
// No limit, use the image height
$max_image_height = $height;
}
if ($exact) {
// Check if dimensions match exactly
return $width === $max_image_width and $height === $max_image_height;
}
// Check if size is within maximum dimensions
return $width <= $max_image_width and $height <= $max_image_height;
}
}