docs_dir = $_dir; foreach ($_data as $property => $value) { $this->$property = $value; } $dot_pos = strrpos($this->name, '.'); if ($dot_pos !== false) { $this->ext = substr($_name, $dot_pos + 1); $this->name = substr($this->name, 0, $dot_pos); } } public function __destruct() { if (file_exists($this->tmp_name)) unlink($this->tmp_name); } protected function fix_dir($_dir) { $dir = preg_replace('[(/|\{1,2})$]', '', $_dir); return $dir; } public function copy($_dir) { $dir = $this->fix_dir($_dir); $filename = $this->name; if (!empty($this->ext)) $filename .= '.' . $this->ext; //move_uploaded_file($this->tmp_name, $this->docs_dir . $dir . '/' . $filename); copy($this->tmp_name, $this->docs_dir . $dir . '/' . $filename); } } class Image extends File { private $avail_ext = array(1 => 'gif', 2 => 'jpg', 3 => 'png'); private $avail_mimes = array(1 => 'image/gif', 2 => 'image/jpeg', 3 => 'image/png'); public $width, $height; public function __construct($_data) { parent::__construct($_data); list($width, $height, $type) = getimagesize($this->tmp_name); $this->width = $width; $this->height = $height; if (!in_array($type, array_keys($this->avail_ext))) $this->error = true; $this->ext = $this->avail_ext[$type]; $this->type = $this->avail_mimes[$type]; if ($this->error) return false; } private function ratio($_width, $_height) { /*if ($this->width < $_width && $this->height < $_height) { return array($this->width, $this->height); }*/ $ratiow = $this->width / $_width; $ratioh = $this->height / $_height; $ratio = max(array($ratiow, $ratioh)); $size = array(round($this->width / $ratio), round($this->height / $ratio)); return $size; } public function resample($_width, $_height, $_dir, $_ratio = true) { if ($_ratio) { list($_width, $_height) = $this->ratio($_width, $_height); } $image_p = imagecreatetruecolor($_width, $_height); switch ($this->type) { case 'image/jpeg' : $image = imagecreatefromjpeg($this->tmp_name); break; case 'image/gif' : $image = imagecreatefromgif($this->tmp_name); break; case 'image/png' : $image = imagecreatefrompng($this->tmp_name); break; } imagecopyresampled($image_p, $image, 0, 0, 0, 0, $_width, $_height, $this->width, $this->height); $dir = parent::fix_dir($_dir); $output = "{$dir}/{$this->name}.{$this->ext}"; if (file_exists($this->docs_dir . $output)) unlink($this->docs_dir . $output); switch ($this->type) { case 'image/jpeg' : imagejpeg($image_p, $this->docs_dir . $output, 80); break; case 'image/gif' : imagegif($image_p, $this->docs_dir . $output); break; case 'image/png' : imagepng($image_p, $this->docs_dir . $output); break; } } } class Uploader { private $docs_dir; private $files; public function __construct($_types = 'File', $_docs_dir = null) { $types = (!is_array($_types)) ? array($_types) : $_types; if (defined('DOCS_DIR')) { $this->docs_dir = DOCS_DIR; } foreach ($_FILES as $input_name => $data) { $type = (isset($types[$input_name])) ? $types[$input_name] : $types[0]; if (is_array($data['name'])) { $objects = array(); $count = count($data['name']); for($i = 0; $i < $count; $i++) { $props = array(); foreach ($data as $k => $v) $props[$k] = $data[$k][$i]; if ($props['error']) break; $objects[$i] = new $type($props); if ($objects[$i]->error) unset($objects[$i]); } if (!empty($objects)) $this->files[$input_name] = $objects; } else { if (!$data['error']) { $this->files[$input_name] = new $type($data); if ($this->files[$input_name]->error) unset($this->files[$input_name]); } } } } public function __get($_name) { $result = (isset($this->files[$_name])) ? $this->files[$_name] : false; return $result; } public function __isset($_name) { $result = (isset($this->files[$_name])) ? true : false; return $result; } } ?>