'integer', 'width' => 'integer', 'height' => 'integer', 'focus_x' => 'float', 'focus_y' => 'float']; } public function uploader(): BelongsTo { return $this->belongsTo(config('manager.user_model'), 'uploaded_by'); } public function isImage(): bool { return str_starts_with($this->mime_type, 'image/'); } /** * The start of this file's address. It holds the id, so it stays the same when the file is * renamed, and is what content is searched for to see where a file is used. */ public function urlPrefix(): string { return '/media-files/'.$this->id.'/'; } /** * The address visitors load the file from. It ends in /view on purpose: web servers tend to * answer anything that ends in .jpg themselves, as a file on disk, and never ask the app. */ public function url(): string { return $this->urlPrefix().rawurlencode($this->filename).'/view'; } /** * The focus point of the image behind an address from the library, as a CSS `object-position` * value ("30% 65%"). Null for any other address, or when no focus point was set. */ public static function focusFor(?string $url): ?string { if ($url === null || preg_match('#^/media-files/(\d+)/#', $url, $match) !== 1) { return null; } $media = static::query()->whereKey((int) $match[1])->whereNotNull('focus_x')->whereNotNull('focus_y')->first(['id', 'focus_x', 'focus_y']); return $media instanceof self ? round((float) $media->focus_x * 100, 2).'% '.round((float) $media->focus_y * 100, 2).'%' : null; } /** What the manager shows. Never includes where the file is stored. */ public function toLibraryItem(int $usageCount = 0): array { return [ 'id' => $this->id, 'name' => $this->name, 'filename' => $this->filename, 'url' => $this->url(), 'mime_type' => $this->mime_type, 'is_image' => $this->isImage(), 'size' => $this->size, 'width' => $this->width, 'height' => $this->height, 'alt' => $this->alt, 'focus_x' => $this->focus_x, 'focus_y' => $this->focus_y, 'uploaded_by' => $this->uploader?->name, 'created_at' => $this->created_at?->toIso8601String(), 'usage_count' => $usageCount, ]; } }