|
5 | 5 | trait HasImage
|
6 | 6 | {
|
7 | 7 |
|
| 8 | + public function attachImage($image) |
| 9 | + { |
| 10 | + $this->image = basename($image); |
| 11 | + } |
| 12 | + |
| 13 | + public function getImageDirectory() |
| 14 | + { |
| 15 | + |
| 16 | + return empty($this->imageDir) ? 'storage/images/' . $this->getTable() : rtrim($this->imageDir, DIRECTORY_SEPARATOR); |
| 17 | + } |
| 18 | + |
| 19 | + public function getThumbnailDirectory() |
| 20 | + { |
| 21 | + return $this->getImageDirectory() . DIRECTORY_SEPARATOR . 'thumbs'; |
| 22 | + } |
| 23 | + |
8 | 24 | public function hasImage()
|
9 | 25 | {
|
10 | 26 | return (isset($this->image) && !empty($this->image));
|
11 | 27 | }
|
12 | 28 |
|
| 29 | + public function imageFileExists() |
| 30 | + { |
| 31 | + return $this->hasImage() && file_exists($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image); |
| 32 | + } |
| 33 | + |
| 34 | + public function thumbnailFileExists() |
| 35 | + { |
| 36 | + return $this->hasImage() && file_exists($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image); |
| 37 | + } |
| 38 | + |
13 | 39 | public function getThumb()
|
14 | 40 | {
|
15 | 41 |
|
16 |
| - if ($this->hasImage() && file_exists(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . "/thumbs/" . $this->image)) { |
17 |
| - return url(rtrim($this->imageDir, DIRECTORY_SEPARATOR). "/thumbs/" . $this->image); |
| 42 | + if ($this->thumbnailFileExists()) { |
| 43 | + return url($this->getThumbnailDirectory() . DIRECTORY_SEPARATOR . $this->image); |
18 | 44 | } else {
|
19 | 45 | return $this->getImage();
|
20 | 46 | }
|
21 | 47 | }
|
22 | 48 |
|
23 | 49 | public function getImage()
|
24 | 50 | {
|
25 |
| - if ($this->hasImage() && file_exists(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->image)) { |
26 |
| - return url(rtrim($this->imageDir, DIRECTORY_SEPARATOR) . "/" . $this->image); |
| 51 | + |
| 52 | + if ($this->imageFileExists()) { |
| 53 | + return url($this->getImageDirectory() . DIRECTORY_SEPARATOR . $this->image); |
27 | 54 | } else {
|
28 | 55 | return url($this->getDefaultImage());
|
29 | 56 | }
|
|
0 commit comments