174 lines
4.5 KiB
PHP
174 lines
4.5 KiB
PHP
|
<?php
|
|||
|
|
|||
|
/**
|
|||
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|||
|
*
|
|||
|
* @version 1
|
|||
|
* @author Ivor Barhansky <me@lopar.space>
|
|||
|
*/
|
|||
|
|
|||
|
class Uploader
|
|||
|
{
|
|||
|
private $width = ['min' => 0, 'max' => 0];
|
|||
|
private $height = ['min' => 0, 'max' => 0];
|
|||
|
private $maxFileSizeMb;
|
|||
|
private $savePath;
|
|||
|
private $extensions = 'jpg|png|jpeg|gif';
|
|||
|
private $extMatches = [];
|
|||
|
private $FILE;
|
|||
|
private $cnm = null;
|
|||
|
public static $error;
|
|||
|
|
|||
|
public function __construct($name, $cnm = null)
|
|||
|
{
|
|||
|
$this->FILE = $_FILES;
|
|||
|
if (!$this->FILE[$name]) {
|
|||
|
return;
|
|||
|
}
|
|||
|
$this->cnm = $cnm;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param int $max
|
|||
|
* @param int $min
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setWidth($max, $min = null)
|
|||
|
{
|
|||
|
$this->width['min'] = is_null($min) ? $max : $min;
|
|||
|
$this->width['max'] = $max;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param int $max
|
|||
|
* @param int $min
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setHeight($max, $min = null)
|
|||
|
{
|
|||
|
$this->height['min'] = is_null($min) ? $max : $min;
|
|||
|
$this->height['max'] = $max;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param $megabytes
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setMaxFileSize($megabytes)
|
|||
|
{
|
|||
|
$this->maxFileSizeMb = $megabytes * (1024 * 1024);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param string $path <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setSavePath($path)
|
|||
|
{
|
|||
|
$this->savePath = __DIR__ . '/' . $path;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param string|array $ext
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setExtentions($ext)
|
|||
|
{
|
|||
|
if (is_array($ext)) {
|
|||
|
$arr = $ext;
|
|||
|
} else {
|
|||
|
$arr[] = $ext;
|
|||
|
}
|
|||
|
if (!$arr) {
|
|||
|
return;
|
|||
|
}
|
|||
|
$this->extensions = implode('|', $arr);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @param int $width
|
|||
|
* @param int $height
|
|||
|
* @return void
|
|||
|
*/
|
|||
|
public function setDimensions($width, $height)
|
|||
|
{
|
|||
|
$this->setWidth($width);
|
|||
|
$this->setHeight($height);
|
|||
|
}
|
|||
|
|
|||
|
private function hasNormalDimensions()
|
|||
|
{
|
|||
|
list($width, $height) = getimagesize($this->FILE['tmp_name']);
|
|||
|
if (
|
|||
|
$width < $this->width['min'] ||
|
|||
|
$width > $this->width['max'] ||
|
|||
|
$height < $this->height['min'] ||
|
|||
|
$height > $this->height['max']
|
|||
|
) {
|
|||
|
self::$error = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [{$this->width['max']}x{$this->height['max']}] <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.";
|
|||
|
if ($this->width['min'] !== $this->width['max'] || $this->height['min'] !== $this->height['max']) {
|
|||
|
self::$error .= " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [{$this->width['min']}x{$this->height['min']}].";
|
|||
|
}
|
|||
|
self::$error .= " <span style='color:red;'>[{$width}x$height]</span>";
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private function hasNormalFileSize()
|
|||
|
{
|
|||
|
if (!$this->maxFileSizeMb) {
|
|||
|
$this->setMaxFileSize(2);
|
|||
|
}
|
|||
|
if ($this->FILE['size'] > $this->maxFileSizeMb || $this->FILE['size'] <=0) {
|
|||
|
self::$error = '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> ' . $this->maxFileSizeMb . ' <20><>';
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private function hasNormalType()
|
|||
|
{
|
|||
|
if (
|
|||
|
!preg_match('/\.(' . $this->extensions . ')$/i', $this->FILE['name'], $this->extMatches) ||
|
|||
|
!preg_match('/image/i', $this->FILE['type'])
|
|||
|
) {
|
|||
|
self::$error = '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> : ' . $this->extensions;
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private function hasNormalFilePath()
|
|||
|
{
|
|||
|
if (!$this->savePath || !is_dir($this->savePath)) {
|
|||
|
self::$error = '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>: <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>.';
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private function upload()
|
|||
|
{
|
|||
|
$this->extMatches[1] = strtolower($this->extMatches[1]);
|
|||
|
$fn = uniqid('f_', true) . '.' . $this->extMatches[1];
|
|||
|
$fn2 = uniqid('f_', true) . '.gif';
|
|||
|
if ($this->cnm) {
|
|||
|
$fn = $this->cnm;
|
|||
|
$fn2 = $this->cnm;
|
|||
|
}
|
|||
|
if (!move_uploaded_file($this->FILE['tmp_name'], $this->savePath . $fn)) {
|
|||
|
self::$error = '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>';
|
|||
|
return false;
|
|||
|
}
|
|||
|
return [$fn2, $fn, $this->savePath . $fn];
|
|||
|
}
|
|||
|
|
|||
|
function saveimg()
|
|||
|
{
|
|||
|
return $this->hasNormalFilePath() &&
|
|||
|
$this->hasNormalDimensions() &&
|
|||
|
$this->hasNormalFileSize() &&
|
|||
|
$this->hasNormalType() ? $this->upload() : false;
|
|||
|
}
|
|||
|
}
|