-
Notifications
You must be signed in to change notification settings - Fork 554
/
Media.php
executable file
·68 lines (56 loc) · 1.69 KB
/
Media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace A17\Twill\Models;
use DB;
use ImageService;
class Media extends Model
{
public $timestamps = true;
public $table = 'medias';
protected $fillable = [
'uuid',
'filename',
'alt_text',
'caption',
'width',
'height',
];
public function getDimensionsAttribute()
{
return $this->width . 'x' . $this->height;
}
public function altTextFrom($filename)
{
$filename = pathinfo($filename, PATHINFO_FILENAME);
if (ends_with($filename, '@2x')) {
$filename = substr($filename, 0, -2);
}
return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename)));
}
public function canDeleteSafely()
{
return DB::table('mediables')->where('media_id', $this->id)->count() === 0;
}
public function toCmsArray()
{
return [
'id' => $this->id,
'name' => $this->filename,
'thumbnail' => ImageService::getCmsUrl($this->uuid, ["h" => "256"]),
'original' => ImageService::getRawUrl($this->uuid),
'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]),
'width' => $this->width,
'height' => $this->height,
];
}
public function getMetadata($name, $fallback = null)
{
$metadatas = (object) json_decode($this->pivot->metadatas);
$language = app()->getLocale();
$fallback = $fallback ? $this->$fallback : $this->name;
return $metadatas->$name->$language ?? (
is_object($metadatas->$name ?? null)
? ($fallback ?? '')
: ($metadatas->$name ?? $fallback)
);
}
}