lavibi /
popoya
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lavibi\Popoya; |
||
| 4 | |||
| 5 | use Psr\Http\Message\StreamInterface; |
||
| 6 | use Psr\Http\Message\UploadedFileInterface; |
||
| 7 | |||
| 8 | class Image extends AbstractValidator |
||
| 9 | { |
||
| 10 | const MAX_WIDTH = 'max_width'; |
||
| 11 | const MAX_HEIGHT = 'max_height'; |
||
| 12 | const NOT_VALID_IMAGE = 'not_valid_image'; |
||
| 13 | |||
| 14 | 1 | public function hasMaxWidth($width) |
|
| 15 | { |
||
| 16 | 1 | $this->options['size']['width'] = (int) $width; |
|
| 17 | |||
| 18 | 1 | return $this; |
|
| 19 | } |
||
| 20 | |||
| 21 | 1 | public function hasMaxHeight($height) |
|
| 22 | { |
||
| 23 | 1 | $this->options['size']['height'] = (int) $height; |
|
| 24 | |||
| 25 | 1 | return $this; |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array $value |
||
| 30 | * |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | 7 | public function isValid($value) |
|
| 34 | { |
||
| 35 | 7 | $this->value = $value; |
|
| 36 | |||
| 37 | 7 | $image = $this->isImage(); |
|
| 38 | |||
| 39 | 7 | if (!$image) { |
|
| 40 | 3 | return false; |
|
| 41 | } |
||
| 42 | |||
| 43 | 4 | if (!$this->isValidImageMaxSize($image['width'], $image['height'])) { |
|
| 44 | 1 | return false; |
|
| 45 | } |
||
| 46 | |||
| 47 | 4 | $this->standardValue['image'] = $image; |
|
| 48 | |||
| 49 | 4 | return true; |
|
| 50 | } |
||
| 51 | |||
| 52 | 7 | protected function isImage() |
|
| 53 | { |
||
| 54 | 7 | $file = $this->getFile(); |
|
| 55 | |||
| 56 | 7 | if ($file === false) { |
|
| 57 | 2 | $this->setError(static::NOT_VALID_IMAGE); |
|
| 58 | 2 | return false; |
|
| 59 | } |
||
| 60 | |||
| 61 | 6 | $size = @getimagesize($file); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 62 | |||
| 63 | 6 | if ($size === false) { |
|
| 64 | 2 | $this->setError(static::NOT_VALID_IMAGE); |
|
| 65 | 2 | return false; |
|
| 66 | } |
||
| 67 | |||
| 68 | return [ |
||
| 69 | 4 | 'width' => $size[0], |
|
| 70 | 4 | 'height' => $size[1], |
|
| 71 | ]; |
||
| 72 | } |
||
| 73 | |||
| 74 | 7 | protected function getFile() |
|
| 75 | { |
||
| 76 | 7 | $file = $this->value; |
|
| 77 | |||
| 78 | 7 | if ($this->value instanceof UploadedFileInterface) { |
|
| 79 | 2 | $file = $this->value->getStream(); |
|
| 80 | } |
||
| 81 | |||
| 82 | 7 | if (is_array($this->value) && isset($this->value['tmp_name'])) { |
|
| 83 | 4 | $file = $this->value['tmp_name']; |
|
| 84 | } |
||
| 85 | |||
| 86 | 7 | if (is_string($file)) { |
|
| 87 | 4 | return $file; |
|
| 88 | } |
||
| 89 | |||
| 90 | 4 | if ($file instanceof StreamInterface) { |
|
| 91 | 2 | return $file->getMetadata('uri') ?? false; |
|
| 92 | } |
||
| 93 | |||
| 94 | 2 | return false; |
|
| 95 | } |
||
| 96 | |||
| 97 | 4 | protected function isValidImageMaxSize($width, $height) |
|
| 98 | { |
||
| 99 | 4 | if ($this->options['size']['width'] !== 0 && $width > $this->options['size']['width']) { |
|
| 100 | 1 | $this->setError(self::MAX_WIDTH); |
|
| 101 | 1 | return false; |
|
| 102 | } |
||
| 103 | |||
| 104 | 4 | if ($this->options['size']['height'] !== 0 && $height > $this->options['size']['height']) { |
|
| 105 | 1 | $this->setError(self::MAX_HEIGHT); |
|
| 106 | 1 | return false; |
|
| 107 | } |
||
| 108 | |||
| 109 | 4 | return true; |
|
| 110 | } |
||
| 111 | |||
| 112 | 7 | protected function init() |
|
| 113 | { |
||
| 114 | 7 | parent::init(); |
|
| 115 | |||
| 116 | 7 | $this->defaultOptions['size'] = [ |
|
| 117 | 'width' => 0, |
||
| 118 | 'height' => 0, |
||
| 119 | ]; |
||
| 120 | |||
| 121 | 7 | $this->messages[static::NOT_VALID_IMAGE] = 'Given data was not image type.'; |
|
| 122 | 7 | $this->messages[static::MAX_WIDTH] = 'Image width is too large.'; |
|
| 123 | 7 | $this->messages[static::MAX_HEIGHT] = 'Image height is too large.'; |
|
| 124 | 7 | } |
|
| 125 | } |
||
| 126 |