2020-09-08 11:45:17 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-08 11:45:17 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Validation;
|
|
|
|
|
|
|
|
use CodeIgniter\Validation\FileRules as ValidationFileRules;
|
|
|
|
|
|
|
|
class FileRules extends ValidationFileRules
|
|
|
|
{
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Checks an uploaded file to verify that the dimensions are within a specified allowable dimension.
|
2020-09-08 11:45:17 +00:00
|
|
|
*/
|
|
|
|
public function min_dims(string $blank = null, string $params): bool
|
|
|
|
{
|
|
|
|
// Grab the file name off the top of the $params
|
|
|
|
// after we split it.
|
|
|
|
$params = explode(',', $params);
|
|
|
|
$name = array_shift($params);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($files = $this->request->getFileMultiple($name))) {
|
2020-09-08 11:45:17 +00:00
|
|
|
$files = [$this->request->getFile($name)];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2021-05-19 16:35:13 +00:00
|
|
|
if ($file === null) {
|
2020-09-08 11:45:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Parameter sizes
|
|
|
|
$minWidth = $params[0] ?? 0;
|
|
|
|
$minHeight = $params[1] ?? 0;
|
|
|
|
|
|
|
|
// Get uploaded image size
|
|
|
|
$info = getimagesize($file->getTempName());
|
|
|
|
$fileWidth = $info[0];
|
|
|
|
$fileHeight = $info[1];
|
|
|
|
|
|
|
|
if ($fileWidth < $minWidth || $fileHeight < $minHeight) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2020-09-08 11:45:17 +00:00
|
|
|
/**
|
|
|
|
* Checks an uploaded file to verify that the image ratio is of 1:1
|
|
|
|
*/
|
|
|
|
public function is_image_squared(string $blank = null, string $params): bool
|
|
|
|
{
|
|
|
|
// Grab the file name off the top of the $params
|
|
|
|
// after we split it.
|
|
|
|
$params = explode(',', $params);
|
|
|
|
$name = array_shift($params);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($files = $this->request->getFileMultiple($name))) {
|
2020-09-08 11:45:17 +00:00
|
|
|
$files = [$this->request->getFile($name)];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2021-05-19 16:35:13 +00:00
|
|
|
if ($file === null) {
|
2020-09-08 11:45:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get uploaded image size
|
|
|
|
$info = getimagesize($file->getTempName());
|
|
|
|
$fileWidth = $info[0];
|
|
|
|
$fileHeight = $info[1];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if ($fileWidth !== $fileHeight) {
|
2020-09-08 11:45:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
}
|