/** * Clean path handler * * @param $path * @param array $params * @return mixed * @throws Exception */ public static function cleanPath($path, $params = ['checkDir' => false, 'checkFile' => false]) { $params = Arr::mergeWithDefaultParams($params); if ($params['checkDir']) { if (!is_dir($path)) { throw new Exception('Path is not a dir'); } } if ($params['checkFile']) { if (!is_file($path)) { throw new Exception('Path is not a file'); } } return str_replace(array('//'), array('/'), $path); }
/** * Upload method helper * * @param null $name * @param null $file * @param array $params * @return array * @throws Exception */ public function upload($name = null, $file = null, $params = ['afterUpload' => null, 'beforeUpload' => null, 'coords' => null, 'width' => null, 'height' => null, 'beforeUploadImage' => null]) { Arr::mergeWithDefaultParams($params); $data = []; if ($file) { $data['file'] = $file; } elseif (Input::hasFile('file')) { $data['file'] = Input::file('file'); } else { trigger_error("File doesn't exist."); } $data['name'] = $name; $data['params'] = $params; $data['response'] = false; $data['path'] = $this->relPath(); $data['fullname'] = $data['file']->getClientOriginalName(); $data['extension'] = $data['file']->getExtension(); $data['mimeType'] = $data['file']->getMimeType(); if (in_array($data['mimeType'], ['image/gif', 'image/jpeg', 'image/png'])) { $data['type'] = "image"; } else { $data['type'] = "file"; } if (!$data['name']) { $data['name'] = $data['file']->getClientOriginalName(); } elseif (!preg_match('/\\.' . $data['extension'] . '$/i', $data['name'])) { $data['convert'] = true; } elseif (!preg_match('/\\./i', $data['name'])) { $data['name'] .= $data['name'] . '.' . $data['extension']; } if ($params['beforeUpload']) { $data = $params['beforeUpload']($data); } if ($data['type'] == 'image') { $img = Image::load($data['file']->getRealPath()); if ($data['params']['coords']) { $coords = $data['params']['coords']; if (!is_array($coords)) { $coords = json_decode(stripslashes($coords), true); } $img->crop($coords['x'], $coords['y'], $coords['x'] + $coords['w'], $coords['y'] + $coords['h']); } elseif ($data['params']['width'] && $data['params']['height']) { $img->resize($data['params']['width'], $data['params']['height']); } elseif ($data['params']['width']) { $img->fit_to_width($data['params']['width']); } elseif ($data['params']['height']) { $img->fit_to_width($data['params']['height']); } if ($params['beforeUploadImage']) { $img = $params['beforeUploadImage']($img); } @mkdir($data['path'], null, true); $data['response'] = $img->save(public_path($data['path'] . $data['name'])); } else { $data['response'] = File::move($data['file']->getRealPath(), $data['path'] . $data['name']); } if ($params['afterUpload']) { $data = $params['afterUpload']($data); } return $data; }
/** * Add a bootstrap btn helper * * @param $href * @param $content * @param array $attr * @param array $params * @return string * @throws \Exception */ public static function btn($href, $content, $attr = ['class' => 'btn btn-default'], $params = array('tag' => 'a', 'attrHref' => 'href')) { $params = Arr::mergeWithDefaultParams($params); if (preg_match('/^fa-/', $content)) { $content = "<i class=\"fa " . $content . '"></i>'; } elseif (preg_match('/^glyphicon-/', $content)) { $content = "<i class=\"glyphicon " . $content . '"></i>'; } return '<' . $params['tag'] . ' ' . $params['attrHref'] . '="' . $href . '" ' . Html::attributes($attr) . '>' . $content . '</' . $params['tag'] . '>'; }