示例#1
0
 /**
  * Upload new image (uses "imgupload" ckeditor plugin, with little modifications in file naming)
  */
 public function postUpload()
 {
     $imageDirectory = Config::get('admin.imagesUploadDirectory');
     $upload_dir = Config::get('admin.imagesDirectory') . '/' . $imageDirectory;
     $allowedExtensions = ['bmp', 'gif', 'jpg', 'jpeg', 'png'];
     $maxsize = 2000;
     $maxwidth = 9000;
     $maxheight = 8000;
     $minwidth = 10;
     $minheight = 10;
     $file = Input::file('upload');
     $errors = [];
     $extension = null;
     $width = 0;
     $height = 0;
     try {
         if (is_null($file)) {
             $errors[] = Lang::get('admin::lang.ckeditor.upload.error.common');
             throw new Exception();
         }
         $extension = $file->guessClientExtension();
         if (!in_array($extension, $allowedExtensions)) {
             $errors[] = Lang::get('admin::lang.ckeditor.upload.error.wrong_extension', ['file' => $file->getClientOriginalName()]);
             throw new Exception();
         }
         if ($file->getSize() > $maxsize * 1000) {
             $errors[] = Lang::get('admin::lang.ckeditor.upload.error.filesize_limit', ['size' => $maxsize]);
         }
         $image = App::make('image')->make($file);
         $width = $image->width();
         $height = $image->height();
         if ($width > $maxwidth || $height > $maxheight) {
             $errors[] = Lang::get('admin::lang.ckeditor.upload.error.imagesize_max_limit', ['width' => $width, 'height' => $height, 'maxwidth' => $maxwidth, 'maxheight' => $maxheight]);
         }
         if ($width < $minwidth || $height < $minheight) {
             $errors[] = Lang::get('admin::lang.ckeditor.upload.error.imagesize_min_limit', ['width' => $width, 'height' => $height, 'minwidth' => $minwidth, 'minheight' => $minheight]);
         }
     } catch (Exception $e) {
     }
     if (!empty($errors)) {
         return '<script>alert("' . implode('\\n', $errors) . '");</script>';
     }
     $finalFilename = RandomFilenamer::get($upload_dir, $extension);
     $file = $file->move($upload_dir, $finalFilename);
     $CKEditorFuncNum = Input::get('CKEditorFuncNum');
     $url = URL::route('imagecache', ['original', $imageDirectory . '/' . $finalFilename]);
     $message = Lang::get('admin::lang.ckeditor.upload.success', ['size' => number_format($file->getSize() / 1024, 3, '.', ''), 'width' => $width, 'height' => $height]);
     $result = "window.parent.CKEDITOR.tools.callFunction({$CKEditorFuncNum}, '{$url}', '{$message}')";
     return '<script>' . $result . ';</script>';
 }
示例#2
0
 /** @test */
 public function it_generates_name_with_extension()
 {
     $name = RandomFilenamer::get('', 'jpg');
     $this->assertStringEndsWith('.jpg', $name);
 }
 /**
  * @param \Closure|null $func
  * @param $field
  * @param UploadedFile $file
  * @return string
  */
 protected function getFilenameFromFile($func, $field, UploadedFile $file)
 {
     if (is_null($func)) {
         $func = function ($directory, $originalName, $extension) {
             return RandomFilenamer::get($directory, $extension);
         };
     }
     return $func($this->{$field}->getDirectoryFullPath(), $file->getClientOriginalName(), $file->guessClientExtension());
 }