123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace App\Exceptions\Form;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- use Dcat\Admin\Form\Field\Image;
- use Dcat\Admin\Form\Field\File;
- use Dcat\Admin\Form\Field\ImageField;
- class CutImage extends File
- {
- use ImageField;
- protected $rules = ['nullable', 'image'];
- protected $view = 'admin.form_custom.cut-image';
- public function __construct($column, $arguments = [])
- {
- parent::__construct($column, $arguments);
- $this->setupImage();
- }
-
- public function aspectRatio(float $value = 1)
- {
- $this->options['aspectRatio'] = $value;
- return $this;
- }
- protected function setupImage()
- {
- if (! isset($this->options['accept'])) {
- $this->options['accept'] = [];
- }
- $this->options['accept']['mimeTypes'] = 'image/*';
- $this->options['isImage'] = true;
- }
-
- public function dimensions(array $options)
- {
- if (! $options) {
- return $this;
- }
- $this->mergeOptions(['dimensions' => $options]);
- foreach ($options as $k => &$v) {
- $v = "$k=$v";
- }
- return $this->rules('dimensions:'.implode(',', $options));
- }
-
- public function ratio($ratio)
- {
- if ($ratio <= 0) {
- return $this;
- }
- return $this->dimensions(['ratio' => $ratio]);
- }
-
- protected function prepareFile(UploadedFile $file)
- {
- $this->callInterventionMethods($file->getRealPath(), $file->getMimeType());
- $this->uploadAndDeleteOriginalThumbnail($file);
- }
- }
|