diff options
author | n-peugnet <n.peugnet@free.fr> | 2019-11-04 23:31:31 +0100 |
---|---|---|
committer | n-peugnet <n.peugnet@free.fr> | 2019-11-05 19:06:40 +0100 |
commit | e802d5204b96d645ec3d40b81b4a8bdc6e0ee675 (patch) | |
tree | 8e6db5e36ad8f247b442583e1e9e5da2934f4b52 /app/class/Media.php | |
parent | f1f63f556c41c99d45cd610186b0982383eff375 (diff) | |
download | wcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.tar.gz wcms-e802d5204b96d645ec3d40b81b4a8bdc6e0ee675.zip |
refactor: switch to psr-4 autoloading
Diffstat (limited to 'app/class/Media.php')
-rw-r--r-- | app/class/Media.php | 233 |
1 files changed, 233 insertions, 0 deletions
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 @@ +<?php + +namespace Wcms; + +class Media +{ + private $id; + private $path; + private $extension; + private $type; + private $size; + private $width; + private $height; + private $length; + + const IMAGE = array('jpg', 'jpeg', 'gif', 'png'); + const SOUND = array('mp3', 'flac', 'wav', 'ogg'); + const VIDEO = array('mp4', 'mov', 'avi', 'mkv'); + + + +// _____________________________________________________ F U N ____________________________________________________ + + public function __construct(array $donnees) + { + $this->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 = ' . ')'; + 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; + } + } + + + + + + +} |