File size: 5,893 Bytes
52d9fb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | <?php
namespace App\Lib;
use App\Constants\FileInfo;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
class FileManager
{
/*
|--------------------------------------------------------------------------
| File Manager
|--------------------------------------------------------------------------
|
| FileManager class is using to manage edit, update, remove files. Developer
| can manage any kind of files from here. But some limitations is here for image.
| This class using a trait to manage the file paths and sizes. Developer can also
| use this class as a helper function.
|
*/
/**
* The file which will be uploaded
*
*
* @var object
*/
protected $file;
/**
* The path where will be uploaded
*
* @var string
*/
public $path;
/**
* The size, if the file is image
*
* @var string
*/
public $size;
/**
* Check the file is image or not
*
* @var boolean
*/
protected $isImage;
/**
* Thumbnail version size, if required
* and if the file is image
*
* @var string
*/
public $thumb;
/**
* Old filename, which will be removed
*
* @var string
*/
public $old;
/**
* Current filename, which is uploading
*
* @var string
*/
public $filename;
/**
* Set the file and file type to properties if exist
*
* @param $file
* @return void
*/
public function __construct($file = null){
$this->file = $file;
if ($file) {
$imageExtensions = ['jpg','jpeg','png','JPG','JPEG','PNG'];
if (in_array($file->getClientOriginalExtension(), $imageExtensions)) {
$this->isImage = true;
}else{
$this->isImage = false;
}
}
}
/**
* File upload process
*
* @return void
*/
public function upload(){
//create the directory if doesn't exists
$path = $this->makeDirectory();
if (!$path) throw new \Exception('File could not been created.');
//remove the old file if exist
if ($this->old) {
$this->removeFile();
}
//get the filename
if(!$this->filename){
$this->filename = $this->getFileName();
}
//upload file or image
if ($this->isImage == true) {
$this->uploadImage();
}else{
$this->uploadFile();
}
}
/**
* Upload the file if this is image
*
* @return void
*/
protected function uploadImage(){
$manager = new ImageManager(new Driver());
$image = $manager->read($this->file);
//resize the
if ($this->size) {
$size = explode('x', strtolower($this->size));
$image->resize($size[0], $size[1]);
}
//save the image
$image->save($this->path . '/' . $this->filename);
//save the image as thumbnail version
if ($this->thumb) {
if ($this->old) {
$this->removeFile($this->path . '/thumb_' . $this->old);
}
$thumb = explode('x', $this->thumb);
$manager->read($this->file)->resize($thumb[0], $thumb[1])->save($this->path . '/thumb_' . $this->filename);
}
}
/**
* Upload the file if this is not a image
*
* @return void
*/
protected function uploadFile(){
$this->file->move($this->path,$this->filename);
}
/**
* Make directory doesn't exists
* Developer can also call this method statically
*
* @param $location
* @return string
*/
public function makeDirectory($location = null){
if (!$location) $location = $this->path;
if (file_exists($location)) return true;
return mkdir($location, 0755, true);
}
/**
* Remove all directory inside the location
* Developer can also call this method statically
*
* @param $location
* @return void
*/
public function removeDirectory($location = null){
if (!$location) $location = $this->path;
if (! is_dir($location)) {
throw new \InvalidArgumentException("$location must be a directory");
}
if (substr($location, strlen($location) - 1, 1) != '/') {
$location .= '/';
}
$files = glob($location . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
static::removeDirectory($file);
} else {
unlink($file);
}
}
rmdir($location);
}
/**
* Remove the file if exists
* Developer can also call this method statically
*
* @param $path
* @return void
*/
public function removeFile($path = null)
{
if (!$path) $path = $this->path . '/' . $this->old;
file_exists($path) && is_file($path) ? @unlink($path) : false;
if ($this->thumb) {
if (!$path) $path = $this->path . '/thumb_' . $this->old;
file_exists($path) && is_file($path) ? @unlink($path) : false;
}
}
/**
* Generating the filename which is uploading
*
* @return string
*/
protected function getFileName(){
return uniqid() . time() . '.' . $this->file->getClientOriginalExtension();
}
/**
* Get access of array from fileInfo method as non-static method.
* Also get some others method
*
* @return string|void
*/
public function __call($method,$args){
$fileInfo = new FileInfo;
$filePaths = $fileInfo->fileInfo();
if (array_key_exists($method, $filePaths)) {
$path = json_decode(json_encode($filePaths[$method]));
return $path;
}else{
if (method_exists($this,$method)) {
$this->$method(...$args);
}else{
throw new \Exception('File key or method doesn\'t exists.');
}
}
}
/**
* Get access some non-static method as static method
*
* @return void
*/
public static function __callStatic($method,$args){
$selfClass = new FileManager;
$selfClass->$method(...$args);
}
}
|