Skip to content

Commit ae92c27

Browse files
author
Rias
committed
Add v7 docs
1 parent 6ffb8a4 commit ae92c27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2136
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
build
2-
docs
32
vendor
43
tests/Support/temp
54
tests/temp

docs/_index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: v7
3+
slogan: Associate files with Eloquent models.
4+
githubUrl: https://github.com/spatie/laravel-medialibrary
5+
branch: v7
6+
---

docs/about-us.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: About us
3+
---
4+
5+
[Spatie](https://spatie.be) is a webdesign agency based in Antwerp, Belgium.
6+
7+
Open source software is used in all projects we deliver. Laravel, Nginx, Ubuntu are just a few
8+
of the free pieces of software we use every single day. For this, we are very grateful.
9+
When we feel we have solved a problem in a way that can help other developers,
10+
we release our code as open source software [on GitHub](https://spatie.be/opensource).

docs/advanced-usage/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Advanced usage
3+
weight: 7
4+
---
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: Consuming events
3+
weight: 8
4+
---
5+
6+
The medialibrary will fire the following events that your handlers can listen for:
7+
8+
### MediaHasBeenAdded
9+
This event is fired after the a file has been saved to disk.
10+
11+
The event has a property `media` that holds the `\Spatie\MediaLibrary\Models\Media`-object of which the file has been stored.
12+
13+
### ConversionWillStart
14+
This event is fired right before a conversion will start.
15+
16+
The event has two public properties:
17+
18+
- `media`: the `\Spatie\MediaLibrary\Models\Media`-object of which a conversion will be started
19+
- `conversion`: the conversion (an instance of `\Spatie\MediaLibrary\Conversion\Conversion`) that will start
20+
21+
### ConversionHasBeenCompleted
22+
This event is fired when a conversion has been completed.
23+
24+
The event has two public properties:
25+
26+
- `media`: the `\Spatie\MediaLibrary\Models\Media`-object of which a conversion has been completed
27+
- `conversion`: the conversion (an instance of `\Spatie\MediaLibrary\Conversion\Conversion`) that has just been completed
28+
29+
### CollectionHasBeenCleared
30+
This event will be fired after a collection has been cleared.
31+
32+
The event has two public properties:
33+
34+
- `model`: the object that conforms to `\Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia` of which a collection has just been cleared.
35+
- `collectionName`: the name of the collection that has just been cleared
36+
37+
## Sample usage
38+
39+
First you must created a listener class. Here's one that will log the paths of added media.
40+
41+
```php
42+
namespace App\Listeners;
43+
44+
use Log;
45+
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;
46+
47+
class MediaLogger
48+
{
49+
public function handle(MediaHasBeenAdded $event)
50+
{
51+
$media = $event->media;
52+
$path = $media->getPath();
53+
Log::info("file {$path} has been saved for media {$media->id}");
54+
}
55+
}
56+
```
57+
58+
Hook it up in `app/Providers/EventServiceProvider.php` to let Laravel know that your handler should be called when the event is fired:
59+
60+
```php
61+
protected $listen = [
62+
'Spatie\MediaLibrary\Events\MediaHasBeenAdded' => [
63+
'App\Listeners\MediaLogger'
64+
],
65+
];
66+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Generating custom urls
3+
weight: 9
4+
---
5+
6+
When `getUrl` is called, the task of generating that url is passed to an implementation of `Spatie\MediaLibrary\UrlGenerator`.
7+
8+
The package contains a `LocalUrlGenerator` that can generate urls for a media library that is stored inside the public path. An `S3UrlGenerator` is also included for when you're using S3 to store your files.
9+
10+
If you are storing your media files in a private directory or are using a different filesystem, you can write your own `UrlGenerator`. Your generator must adhere to the `Spatie\MediaLibrary\UrlGenerator` interface. If you'd extend `Spatie\MediaLibrary\UrlGenerator\BaseUrlGenerator` you only need to implement the methods: `getUrl`, `getTemporaryUrl` and `getResponsiveImagesDirectoryUrl`. You can call `getPathRelativeToRoot` to get the relative path to the root of your disk.
11+
12+
The code of the included `S3UrlGenerator` should help make things more clear:
13+
14+
```php
15+
namespace Spatie\MediaLibrary\UrlGenerator;
16+
17+
class S3UrlGenerator extends BaseUrlGenerator
18+
{
19+
/**
20+
* Get the url for the profile of a media item.
21+
*
22+
* @return string
23+
*/
24+
public function getUrl() : string
25+
{
26+
return config('medialibrary.s3.domain').'/'.$this->getPathRelativeToRoot();
27+
}
28+
29+
/**
30+
* Get the temporary url for a media item.
31+
*
32+
* @param \DateTimeInterface $expiration
33+
* @param array $options
34+
*
35+
* @return string
36+
*/
37+
public function getTemporaryUrl(DateTimeInterface $expiration, array $options = []): string
38+
{
39+
return $this
40+
->filesystemManager
41+
->disk($this->media->disk)
42+
->temporaryUrl($this->getPath(), $expiration, $options);
43+
}
44+
45+
/**
46+
* Get the url to the directory containing responsive images.
47+
*
48+
* @return string
49+
*/
50+
public function getResponsiveImagesDirectoryUrl(): string
51+
{
52+
return config('medialibrary.s3.domain').'/'.$this->pathGenerator->getPathForResponsiveImages($this->media);
53+
}
54+
}
55+
```

docs/advanced-usage/moving-media.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Moving media
3+
weight: 7
4+
---
5+
6+
You can move media from one model to another with the `move` method.
7+
8+
```php
9+
$mediaItem = $model->getMedia()->first();
10+
11+
$movedMediaItem = $mediaItem->move($anotherModel);
12+
```
13+
14+
Any conversions defined on `$anotherModel` will be performed. The `name` and the `custom_properties` will be transferred as well.

docs/advanced-usage/ordering-media.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Ordering media
3+
weight: 6
4+
---
5+
6+
Laravel medialibrary has a built in feature to help you order the media in your project. By default all inserted media items are ordered by their creation order (from the oldest to the newest) using the `order_column` column of the `media` table.
7+
8+
You can easily reorder a list of media by calling ̀Media::setNewOrder`:
9+
10+
```php
11+
/**
12+
* This function reorders the records: the record with the first id in the array
13+
* will get order 1, the record with the second id will get order 2, ...
14+
*
15+
* A starting order number can be optionally supplied (defaults to 1).
16+
*
17+
* @param array $ids
18+
* @param int $startOrder
19+
*/
20+
Media::setNewOrder([11, 2, 26]);
21+
```
22+
23+
Of course you can also manually change the value of the `order_column`.
24+
25+
```php
26+
$media->order_column = 10;
27+
$media->save();
28+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Overriding default filesystem behavior
3+
weight: 10
4+
---
5+
6+
The `Spatie\MediaLibrary\Filesystem\Filesystem` class contains the behavior for actions like adding files, renaming files and deleting files. It applies these actions to the disks (local, S3, etc) that you configured.
7+
8+
If you want to override the default behavior you can create your own implementation by extending `Spatie\MediaLibrary\Filesystem\Filesystem`. You then bind your own class to the service container in the `AppServiceProvider`:
9+
10+
```php
11+
use App\CustomFilesystem;
12+
use Spatie\MediaLibrary\Filesystem\Filesystem;
13+
14+
class AppServiceProvider extends ServiceProvider
15+
{
16+
...
17+
public function register()
18+
{
19+
$this->app->bind(Filesystem::class, CustomFilesystem::class);
20+
}
21+
}
22+
```
23+
24+
Generally speaking you do not want to mess with this class, so only override it if you know what you're doing.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Storing media specific manipulations
3+
weight: 3
4+
---
5+
6+
Imagine you need to apply a 90 degree rotation to a single image. So the rotation should be applied to one specific `Media` and not to all media linked to the given `$newsItem`.
7+
8+
When adding an image to the medialibrary, you can use `withManipulations` to set any media specific manipulations.
9+
10+
Here's a quick example:
11+
12+
```php
13+
$newsItem
14+
->addMedia($pathToFile)
15+
->withManipulations([
16+
'thumb' => ['orientation' => '90'],
17+
]);
18+
```
19+
20+
The package will regenerate all files (conversions) using the saved manipulation as the first manipulation when creating each derived image.
21+
22+
You can also apply media specific manipulations to an existing `Media` instance.
23+
24+
```php
25+
$mediaItems = $newsItem->getMedia('images');
26+
$mediaItems[0]->manipulations = [
27+
'thumb' => ['orientation' => '90'],
28+
];
29+
30+
// This will cause the thumb conversions to be regenerated.
31+
$mediaItems[0]->save();
32+
```
33+
34+
First the rotation will be applied for this specific `$mediaItem`, then the other manipulations you specified in the `thumb` conversion.
35+
36+
Of course you can also set media specific manipulations for multiple conversions in one go:
37+
38+
```php
39+
$newsItem
40+
->addMedia($pathToFile)
41+
->withManipulations([
42+
'thumb' => ['orientation' => '90'],
43+
'otherConversion' => ['orientation' => '90'],
44+
]);
45+
```
46+
47+
Lets take the example again of this one image `$mediaItem` that needs to be rotated and was linked to `$newsItem`. Imagine we have a lot of conversions for all the media: `thumb`, `small` for web, `cmyk` for print in full resolution.
48+
Having to add all these manipulation keys with `orientation 90` would be a pain.
49+
50+
You can avoid this pain by using a wildcard. Manipulations of the wildcard will be added to each conversion of the media.
51+
52+
Here's an example:
53+
54+
```php
55+
$newsItem
56+
->addMedia($pathToFile)
57+
->withManipulations([
58+
'*' => ['orientation' => '90'],
59+
]);
60+
```
61+
62+
You can also combine wildcard manipulations with one for a specific collection. The wildcard manipulations will always be performed before the collection specific ones.
63+
64+
```php
65+
$newsItem
66+
->addMedia($pathToFile)
67+
->withManipulations([
68+
'*' => ['orientation' => '90'],
69+
'thumb' => ['filter' => 'greyscale'],
70+
]);
71+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Using a custom directory structure
3+
weight: 5
4+
---
5+
6+
By default, files will be stored inside a directory that uses the `id` of its `Media`-object as a name. Converted images will be stored inside a directory named `conversions`.
7+
8+
```
9+
media
10+
---- 1
11+
------ file.jpg
12+
------ conversions
13+
--------- small.jpg
14+
--------- medium.jpg
15+
--------- big.jpg
16+
---- 2
17+
------ file.jpg
18+
------ conversions
19+
--------- small.jpg
20+
--------- medium.jpg
21+
--------- big.jpg
22+
...
23+
```
24+
25+
Putting files inside their own folders guarantees that files with the same name can be added without any problems.
26+
27+
To override this default folder structure, a class that conforms to the `PathGenerator`-interface can be specified as the `path_generator` in the config file.
28+
29+
Let's take a look at the interface:
30+
31+
```php
32+
namespace Spatie\MediaLibrary\PathGenerator;
33+
34+
use Spatie\MediaLibrary\Models\Media;
35+
36+
interface PathGenerator
37+
{
38+
/**
39+
* Get the path for the given media, relative to the root storage path.
40+
*
41+
* @param \Spatie\MediaLibrary\Models\Media $media
42+
*
43+
* @return string
44+
*/
45+
public function getPath(Media $media): string;
46+
47+
/**
48+
* Get the path for conversions of the given media, relative to the root storage path.
49+
*
50+
* @param \Spatie\MediaLibrary\Models\Media $media
51+
*
52+
* @return string
53+
*/
54+
public function getPathForConversions(Media $media): string;
55+
56+
/*
57+
* Get the path for responsive images of the given media, relative to the root storage path.
58+
*
59+
* @param \Spatie\MediaLibrary\Models\Media $media
60+
*
61+
* @return string
62+
*/
63+
public function getPathForResponsiveImages(Media $media): string;
64+
}
65+
```
66+
67+
[This example from the tests](https://github.com/spatie/laravel-medialibrary/blob/7.0.0/tests/Unit/PathGenerator/CustomPathGenerator.php) uses
68+
the md5 value of media-id to name directories. The directories where conversions are stored will be named `c` instead of the default `conversions`.
69+
70+
There aren't any restrictions on how the directories can be named. When a `Media`-object gets deleted the package will delete its entire associated directory. To avoid tears or worse, make sure that every media gets stored its own unique directory.

0 commit comments

Comments
 (0)