Exemplo n.º 1
0
 public function __construct($model = null)
 {
     $this->model = $model;
     $ctrl = $this;
     $this->beforeFilter(function ($route, $request) use($ctrl) {
         return $ctrl->_authFilter($route, $request);
     });
     parent::__construct();
 }
Exemplo n.º 2
0
 public function missingMethod($args)
 {
     if (method_exists($this, 'route')) {
         return call_user_func_array(array($this, 'route'), $args);
     }
     return parent::missingMethod($args);
 }
Exemplo n.º 3
0
 public function missingMethod($args)
 {
     if (count($args) < 3) {
         return parent::missingMethod($args);
     }
     if ($args[0] == 'force') {
         array_shift($args);
     }
     $modelKeyword = $args[0];
     $modelClass = \Devhook::getClassByKey($modelKeyword);
     $id = $args[1];
     $file = $args[2];
     $parts = explode('-', $file);
     if (count($parts) == 3) {
         list($field, $sizeKey, $file) = $parts;
         @(list($imageId, $ext) = explode('.', $file));
     } else {
         $imageId = false;
         @(list($field, $file) = $parts);
         @(list($sizeKey, $ext) = explode('.', $file));
     }
     if (!$field || !$sizeKey || $ext != 'jpg') {
         return parent::missingMethod($args);
     }
     $model = $modelClass::find($id);
     if (!$model || !$model->{$field}) {
         return parent::missingMethod($args);
     }
     // $fileExt = pathinfo($model->$field, PATHINFO_EXTENSION);
     $fields = (array) $model->getFields();
     $modelField = isset($fields[$field]) ? $fields[$field] : false;
     if (!$modelField || empty($modelField->type)) {
         return parent::missingMethod($args);
     }
     $imageTypes = array('image');
     $type = $modelField->type;
     $sizes = (array) $modelField->settings('sizes');
     $imageAdminThumb = Config::get('devhook.imageAdminThumb');
     if (!in_array($type, $imageTypes) || empty($sizes[$sizeKey])) {
         return parent::missingMethod($args);
     }
     $watermark = $modelField->settings('watermark');
     $size = $sizes[$sizeKey];
     $width = $size[0];
     $hegiht = $size[1];
     $crop = isset($size[2]) ? $size[2] : false;
     $quality = isset($size[3]) ? $size[3] : 95;
     if ($imageId) {
         $imageFile = \Image::where('imageable_type', $modelKeyword)->where('imageable_id', $id)->find($imageId);
         if (!$imageFile) {
             return parent::missingMethod($args);
         }
         $imageFile = $imageFile->path;
     } else {
         $imageFile = $model->{$field};
     }
     ini_set('memory_limit', '200M');
     $image = new IMG(public_path($imageFile));
     if ($crop) {
         $image->grab($width, $hegiht);
     } else {
         $image->resize($width, $hegiht, true);
     }
     if ($watermark) {
         if (!$watermark['sizes'] || in_array($sizeKey, $watermark['sizes'])) {
             $image->insert($watermark['image'], $watermark['offset'][0], $watermark['offset'][1], $watermark['position']);
         }
     }
     $tmpFile = public_path($model->{$field}) . '.tmp_' . uniqid();
     $newFile = public_path(Config::get('devhook.imageRoute') . '/' . implode('/', $args));
     $newPath = dirname($newFile);
     if (!File::exists($newPath)) {
         File::makeDirectory($newPath, 0777, true);
     }
     $image->save($newFile, $quality);
     $img = $image->encode($ext, $quality);
     $response = Response::make($img, 200);
     $response->header('Content-Type', 'image/jpeg');
     return $response;
 }