Issues (52)

Models/Image.php (1 issue)

1
<?php
2
3
namespace OkayBueno\Images\Models;
4
5
use Illuminate\Database\Eloquent\SoftDeletes;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Class Image
10
 * @package OkayBueno\Images\Models
11
 */
12
class Image extends Model
13
{
14
    use SoftDeletes;
15
16
    protected $fillable = [
17
        'path', 'filename', 'type', 'thumbnails', 'metadata', 'processed'
18
    ];
19
20
21
    /*******************************************************************************************************************
22
     ************************************************** HELPERS / DATA *************************************************
23
     *******************************************************************************************************************/
24
    /**
25
     * @param null $size
26
     * @param bool|FALSE $onlyPath
27
     * @return array|null|string
28
     */
29
    public function thumbnails( $size = NULL, $onlyPath = FALSE )
30
    {
31
        $sizesArray = [];
32
33
        if ( is_null( $size ) )
0 ignored issues
show
The condition is_null($size) is always true.
Loading history...
34
        {
35
            if ( is_array( $this->thumbnails ) )
36
            {
37
                foreach( $this->thumbnails as $sizeKey => $path )
38
                {
39
                    $sizesArray[ $sizeKey ] = $onlyPath ? $path : $this->url_to( $path );
40
                }
41
            }
42
43
        } else
44
        {
45
            if ( is_array( $this->thumbnails) )
46
            {
47
                foreach( $this->thumbnails as $sizeKey => $path )
48
                {
49
                    if( $size == $sizeKey ) return $onlyPath ? $path : $this->url_to( $path );
50
                }
51
            }
52
        }
53
54
        return $sizesArray;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function url()
61
    {
62
        return $this->url_to( $this->path );
63
    }
64
65
    /*******************************************************************************************************************
66
     **************************************************** INTERNAL *****************************************************
67
     *******************************************************************************************************************/
68
69
    /**
70
     * @param $value
71
     * @return mixed
72
     */
73
    public function getThumbnailsAttribute( $value )
74
    {
75
        return json_decode( $value, TRUE );
76
    }
77
78
    /**
79
     * @param $path
80
     * @return string
81
     */
82
    protected function url_to( $path )
83
    {
84
        $localUrl = config('images.local_disk_url');
85
        $remoteUrl = config('images.cloud_disk_url');
86
        if ( $this->processed ) $cdnBaseUrl = $remoteUrl ? $remoteUrl : $localUrl;
87
        else $cdnBaseUrl = $localUrl;
88
89
        $cdnUrlToImage = rtrim( $cdnBaseUrl, '/') . '/' . trim( $path, '/');
90
91
        return $cdnUrlToImage;
92
    }
93
94
95
}
96