From e802d5204b96d645ec3d40b81b4a8bdc6e0ee675 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Mon, 4 Nov 2019 23:31:31 +0100 Subject: refactor: switch to psr-4 autoloading --- app/class/Media.php | 233 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 app/class/Media.php (limited to 'app/class/Media.php') diff --git a/app/class/Media.php b/app/class/Media.php new file mode 100644 index 0000000..b30b883 --- /dev/null +++ b/app/class/Media.php @@ -0,0 +1,233 @@ +hydrate($donnees); + } + + public function hydrate(array $donnees) + { + foreach ($donnees as $key => $value) { + $method = 'set' . $key; + + if (method_exists($this, $method)) { + $this->$method($value); + } + } + } + + + public function analyse() + { + $this->settype(); + + $filepath = $this->path . $this->id . '.' . $this->extension; + + $this->size = filesize($filepath); + + if ($this->type == 'image') { + list($width, $height, $type, $attr) = getimagesize($filepath); + $this->width = $width; + $this->height = $height; + } + + + } + + + public function getfullpath() + { + if(!empty(Config::basepath())) { + $base = '/' . Config::basepath(); + } else { + $base = ''; + } + $fullpath = $base . '/'. $this->path() . $this->id() . '.' . $this->extension(); + $fullpath = str_replace('\\', '/', $fullpath); + return $fullpath; + } + + public function getincludepath() + { + $includepath = $this->path() . $this->id() . '.' . $this->extension(); + $includepath = str_replace('\\', '/', $includepath); + $includepath = substr($includepath, 6); + return $includepath; + } + + public function getfulldir() + { + return $this->path . $this->id . '.' . $this->extension; + } + + /** + * Generate html code depending on media type + * + * @return string html code + */ + public function getcode() : string + { + switch ($this->type) { + case 'image': + $code = '![' . $this->id . '](' . $this->getincludepath() . ')'; + break; + + case 'other': + $code = '[' . $this->id . '](' . $this->getincludepath() . ')'; + break; + + case 'sound': + $code = '<audio controls src="' . $this->getincludepath() . '"></audio>'; + break; + + case 'video': + $code = '<video controls=""><source src="' . $this->getincludepath() . '" type="video/' . $this->extension . '"></video>'; + break; + + } + + return $code; + + } + + + +// _________________________________________________ G E T ____________________________________________________ + + public function id() + { + return $this->id; + } + + public function path() + { + return $this->path; + } + + public function extension() + { + return $this->extension; + } + + public function type() + { + return $this->type; + } + + public function size($display = 'binary') + { + if($display == 'hr') { + return readablesize($this->size); + } else { + return $this->size; + } + } + + public function width() + { + return $this->width; + } + + public function height() + { + return $this->height; + } + + public function length() + { + return $this->length; + } + +// ___________________________________________________ S E T __________________________________________________ + + public function setid($id) + { + if (is_string($id)) { + $this->id = $id; + } + } + + public function setpath($path) + { + if (strlen($path) < 40 and is_string($path)) { + $this->path = strip_tags(strtolower($path)); + } + } + + public function setextension($extension) + { + if (strlen($extension) < 7 and is_string($extension)) { + $this->extension = strip_tags(strtolower($extension)); + } + } + + public function settype() + { + if (isset($this->extension)) { + if (in_array($this->extension, $this::IMAGE)) { + $this->type = "image"; + } elseif (in_array($this->extension, $this::SOUND)) { + $this->type = "sound"; + } elseif (in_array($this->extension, $this::VIDEO)) { + $this->type = "video"; + } else { + $this->type = "other"; + } + } + } + + public function setsize($size) + { + if (40 and is_int($size)) { + $this->size = strip_tags(strtolower($size)); + } + } + + public function setwidth($width) + { + if (is_int($width)) { + $this->width = strip_tags(strtolower($width)); + } + } + + public function setheight($height) + { + if (is_int($height)) { + $this->height = strip_tags(strtolower($height)); + } + } + + public function setlength($length) + { + if ($this->type == 'sound') { + $this->length = $length; + } + } + + + + + + +} -- cgit v1.2.3