-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support a custom file namer for responsive images * Fix styling * apply code review suggestions * let conversions use a new file namer class * add documentation * cleanup last conversion file namer parts * apply code review suggestions * split up conversion & responsive file namer * remove get prefix on FileNamer methods * Update naming-generated-files.md * Update naming-generated-files.md Co-authored-by: Nielsvanpach <[email protected]> Co-authored-by: Freek Van der Herten <[email protected]>
- Loading branch information
1 parent
9ac9125
commit f2c5a56
Showing
19 changed files
with
243 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: Naming generated files | ||
weight: 11 | ||
--- | ||
|
||
### Naming conversion files | ||
|
||
By default, all conversion files will be named in this format: | ||
|
||
``` | ||
{original-file-name-without-extension}-{name-of-the-conversion}.{extension} | ||
``` | ||
|
||
Should you want to name your conversion file using another format, | ||
then you can specify the class name of your own `FileNamer` in the `file_namer` key | ||
of the `media-library.php` config file. | ||
|
||
The only requirements is that your class extends `Spatie\MediaLibrary\Support\FileNamer`. | ||
In your class you should implement 2 methods: | ||
1. `conversionFileName` should return the media file name combined with the conversion name | ||
2. `responsiveFileName` should return the media file name | ||
|
||
Here is the implementation of `Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer` | ||
|
||
```php | ||
namespace Spatie\MediaLibrary\Support\FileNamer; | ||
|
||
use Spatie\MediaLibrary\Conversions\Conversion; | ||
|
||
class DefaultFileNamer extends FileNamer | ||
{ | ||
public function conversionFileName(string $fileName, Conversion $conversion): string | ||
{ | ||
$strippedFileName = pathinfo($fileName, PATHINFO_FILENAME); | ||
|
||
return "{$strippedFileName}-{$conversion->getName()}"; | ||
} | ||
|
||
public function responsiveFileName(string $fileName): string | ||
{ | ||
return pathinfo($fileName, PATHINFO_FILENAME); | ||
} | ||
} | ||
``` | ||
|
||
### Naming responsive image files | ||
|
||
By default, all responsive image files will be named in this format: | ||
|
||
``` | ||
{original-file-name-without-extension}___{name-of-the-conversion}_{width}_{height}.{extension} | ||
``` | ||
|
||
Just like the conversion file names, you can use another format for naming your files | ||
by using your own `FileNamer` class. It is only possible to prefix the name, because other parts are needed in processing responsive images. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Support\FileNamer; | ||
|
||
use Spatie\MediaLibrary\Conversions\Conversion; | ||
|
||
class DefaultFileNamer extends FileNamer | ||
{ | ||
public function conversionFileName(string $fileName, Conversion $conversion): string | ||
{ | ||
$strippedFileName = pathinfo($fileName, PATHINFO_FILENAME); | ||
|
||
return "{$strippedFileName}-{$conversion->getName()}"; | ||
} | ||
|
||
public function responsiveFileName(string $fileName): string | ||
{ | ||
return pathinfo($fileName, PATHINFO_FILENAME); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Support\FileNamer; | ||
|
||
use Spatie\MediaLibrary\Conversions\Conversion; | ||
use Spatie\MediaLibrary\MediaCollections\Models\Media; | ||
|
||
abstract class FileNamer | ||
{ | ||
abstract public function conversionFileName(string $fileName, Conversion $conversion): string; | ||
|
||
abstract public function responsiveFileName(string $fileName): string; | ||
|
||
public function temporaryFileName(Media $media, string $extension): string | ||
{ | ||
return "{$this->responsiveFileName($media->file_name)}.{$extension}"; | ||
} | ||
|
||
public function extensionFromBaseImage(string $baseImage): string | ||
{ | ||
return pathinfo($baseImage, PATHINFO_EXTENSION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Tests\ResponsiveImages; | ||
|
||
use Spatie\MediaLibrary\Tests\TestSupport\TestFileNamer; | ||
|
||
class ResponsiveImageFileNamerTest extends ResponsiveImageTest | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config()->set("media-library.file_namer", TestFileNamer::class); | ||
|
||
$this->fileName = "prefix_test_suffix"; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/ResponsiveImages/ResponsiveImageGeneratorFileNamerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Spatie\MediaLibrary\Tests\ResponsiveImages; | ||
|
||
use Spatie\MediaLibrary\Tests\TestSupport\TestFileNamer; | ||
|
||
class ResponsiveImageGeneratorFileNamerTest extends ResponsiveImageGeneratorTest | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config()->set("media-library.file_namer", TestFileNamer::class); | ||
|
||
$this->fileName = "prefix_test_suffix"; | ||
} | ||
} |
Oops, something went wrong.