|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace UniSharp\LaravelFilemanager\Controllers; |
| 4 | + |
| 5 | +use Intervention\Image\Facades\Image; |
| 6 | +use UniSharp\LaravelFilemanager\Events\ImageIsCropping; |
| 7 | +use UniSharp\LaravelFilemanager\Events\ImageWasCropped; |
| 8 | + |
| 9 | +/** |
| 10 | + * Class CropController. |
| 11 | + */ |
| 12 | +class CropController extends LfmController |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Show crop page. |
| 16 | + * |
| 17 | + * @return mixed |
| 18 | + */ |
| 19 | + public function getCrop() |
| 20 | + { |
| 21 | + $working_dir = request('working_dir'); |
| 22 | + $img = parent::objectPresenter(parent::getCurrentPath(request('img'))); |
| 23 | + |
| 24 | + return view('laravel-filemanager::crop') |
| 25 | + ->with(compact('working_dir', 'img')); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Crop the image (called via ajax). |
| 30 | + */ |
| 31 | + public function getCropimage($overWrite = true) |
| 32 | + { |
| 33 | + $dataX = request('dataX'); |
| 34 | + $dataY = request('dataY'); |
| 35 | + $dataHeight = request('dataHeight'); |
| 36 | + $dataWidth = request('dataWidth'); |
| 37 | + $image_path = parent::getCurrentPath(request('img')); |
| 38 | + $crop_path = $image_path; |
| 39 | + |
| 40 | + if (! $overWrite) { |
| 41 | + $fileParts = explode('.', request('img')); |
| 42 | + $fileParts[count($fileParts) - 2] = $fileParts[count($fileParts) - 2] . '_cropped_' . time(); |
| 43 | + $crop_path = parent::getCurrentPath(implode('.', $fileParts)); |
| 44 | + } |
| 45 | + |
| 46 | + event(new ImageIsCropping($image_path)); |
| 47 | + // crop image |
| 48 | + Image::make($image_path) |
| 49 | + ->crop($dataWidth, $dataHeight, $dataX, $dataY) |
| 50 | + ->save($crop_path); |
| 51 | + |
| 52 | + if (config('lfm.should_create_thumbnails', true)) { |
| 53 | + // create thumb folder |
| 54 | + parent::createFolderByPath(parent::getThumbPath()); |
| 55 | + |
| 56 | + // make new thumbnail |
| 57 | + Image::make($crop_path) |
| 58 | + ->fit(config('lfm.thumb_img_width', 200), config('lfm.thumb_img_height', 200)) |
| 59 | + ->save(parent::getThumbPath(parent::getName($crop_path))); |
| 60 | + } |
| 61 | + |
| 62 | + event(new ImageWasCropped($image_path)); |
| 63 | + } |
| 64 | + |
| 65 | + public function getNewCropimage() |
| 66 | + { |
| 67 | + $this->getCropimage(false); |
| 68 | + } |
| 69 | +} |
0 commit comments