Skip to content

Commit

Permalink
Use image v3 (#3451)
Browse files Browse the repository at this point in the history
* v11

* Fix styling

* wip

* Fix styling

* wip

* fix tests

* Fix styling

* fix tests

* remove unneeded test

* fix tests

* update test config

* Refactor code to set default extension to 'jpg'

* wip

* Fix styling

* skip

* Fix styling

* simplify service provider

* Fix styling

* update deps

* cs

* Fix styling

* rename events

* rename config file

* nitpick

* wip

* wip

* update deps

* Exclude PHP 8.3 and Laravel 9.* from the test matrix

* Update Laravel dependencies to version 10.0

* Update requirements for Laravel Media Library to PHP 8.2+ and Laravel 10+.

* Update laravel dependency version in run-tests.yml to 10.*.

* Update test workflow badge in README.md

* Refactor package to allow usage of any filesystem

* prep

---------

Co-authored-by: freekmurze <[email protected]>
  • Loading branch information
freekmurze and freekmurze authored Dec 14, 2023
1 parent c97c593 commit a1823c0
Show file tree
Hide file tree
Showing 102 changed files with 513 additions and 528 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ jobs:
with:
ref: ${{ github.head_ref }}

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga
with:
args: --config=.php_cs.dist.php --allow-risky=yes
- name: Fix styling issues
uses: aglipanci/[email protected]

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Fix styling
9 changes: 2 additions & 7 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.3, 8.2, 8.1, 8.0]
laravel: [10.*, 9.*]
php: [8.3, 8.2]
laravel: [10.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: ^8.0
- laravel: 9.*
testbench: ^7.0
exclude:
- laravel: 10.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }}

Expand Down
35 changes: 0 additions & 35 deletions .php_cs.dist.php

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Associate files with Eloquent models

[![Latest Version](https://img.shields.io/github/release/spatie/laravel-medialibrary.svg?style=flat-square)](https://github.com/spatie/laravel-medialibrary/releases)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/spatie/laravel-medialibrary/run-tests.yml?branch=master&style=flat-square&label=tests)
[![run-tests](https://github.com/spatie/laravel-medialibrary/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-medialibrary/actions/workflows/run-tests.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-medialibrary.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-medialibrary)

This package can associate all sorts of files with Eloquent models. It provides a
Expand All @@ -30,7 +30,7 @@ $newsItem->addMedia($bigFile)->toMediaCollection('downloads', 's3');
```

The storage of the files is handled by [Laravel's Filesystem](https://laravel.com/docs/filesystem),
so you can use any filesystem you like. Additionally the package can create image manipulations
so you can use any filesystem you like. Additionally, the package can create image manipulations
on images and pdfs that have been added in the media library.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
Expand Down
6 changes: 6 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not cover. We accept PRs to improve this guide.

## From v10 to v11

- Image v3 is now used. Make sure to update your image conversions to the new syntax. See [the image docs](https://spatie.be/docs/image/v3) for more info.
- All event names have gained the `Event` suffix. For example `Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAdded` is now `Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAddedEvent`.


## From v9 to v10

Upgrading from v9 to v10 is straightforward. The biggest change is that we dropped support for PHP 7, and are using PHP 8 features.
Expand Down
48 changes: 24 additions & 24 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,38 @@
],
"homepage": "https://github.com/spatie/laravel-medialibrary",
"require": {
"php": "^8.0",
"php": "^8.2",
"ext-exif": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"illuminate/bus": "^9.18|^10.0",
"illuminate/conditionable": "^9.18|^10.0",
"illuminate/console": "^9.18|^10.0",
"illuminate/database": "^9.18|^10.0",
"illuminate/pipeline": "^9.18|^10.0",
"illuminate/support": "^9.18|^10.0",
"maennchen/zipstream-php": "^2.0|^3.0",
"spatie/image": "^2.2.7",
"spatie/temporary-directory": "^2.0",
"symfony/console": "^6.0"
"illuminate/bus": "^10.0",
"illuminate/conditionable": "^10.0",
"illuminate/console": "^10.0",
"illuminate/database": "^10.0",
"illuminate/pipeline": "^10.0",
"illuminate/support": "^10.0",
"maennchen/zipstream-php": "^3.1",
"spatie/image": "^3.0",
"spatie/laravel-package-tools": "^1.16.1",
"spatie/temporary-directory": "^2.2",
"symfony/console": "^6.4.1|^7.0"
},
"require-dev": {
"ext-imagick": "*",
"ext-pdo_sqlite": "*",
"ext-zip": "*",
"aws/aws-sdk-php": "^3.133.11",
"doctrine/dbal": "^2.13",
"guzzlehttp/guzzle": "^7.4",
"league/flysystem-aws-s3-v3": "^3.0",
"mockery/mockery": "^1.4",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"phpstan/extension-installer": "^1.1",
"spatie/laravel-ray": "^1.28",
"spatie/pdf-to-image": "^2.1",
"spatie/phpunit-snapshot-assertions": "^4.2"
"aws/aws-sdk-php": "^3.293.10",
"doctrine/dbal": "^2.13.9",
"guzzlehttp/guzzle": "^7.8.1",
"league/flysystem-aws-s3-v3": "^3.22",
"mockery/mockery": "^1.6.7",
"larastan/larastan": "^2.7",
"orchestra/testbench": "^7.0|^8.17",
"pestphp/pest": "^2.28",
"phpstan/extension-installer": "^1.3.1",
"spatie/laravel-ray": "^1.33",
"spatie/pdf-to-image": "^2.2",
"spatie/pest-plugin-snapshots": "^2.1"
},
"conflict": {
"php-ffmpeg/php-ffmpeg": "<0.6.1"
Expand Down Expand Up @@ -91,7 +92,6 @@
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"baseline": "vendor/bin/phpstan analyse --generate-baseline",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"test": "vendor/bin/pest"
}
}
2 changes: 1 addition & 1 deletion config/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/*
* When enabled, media collections will be serialised using the default
* laravel model serialization behaviour.
*
*
* Keep this option disabled if using Media Library Pro components (https://medialibrary.pro)
*/
'use_default_collection_serialization' => false,
Expand Down
2 changes: 1 addition & 1 deletion docs/_index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: v10
title: v11
slogan: Associate files with Eloquent models.
githubUrl: https://github.com/spatie/laravel-medialibrary
branch: main
Expand Down
16 changes: 8 additions & 8 deletions docs/advanced-usage/consuming-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ weight: 8

The media library will fire the following events that your handlers can listen for:

### MediaHasBeenAdded
### MediaHasBeenAddedEvent
This event is fired after a file has been saved to disk.

The event has a property `media` that holds the `\Spatie\MediaLibrary\Models\Media`-object of which the file has been stored.

### ConversionWillStart
### ConversionWillStartEvent
This event is fired right before a conversion will start.

The event has two public properties:

- `media`: the `\Spatie\MediaLibrary\Models\Media`-object of which a conversion will be started
- `conversion`: the conversion (an instance of `\Spatie\MediaLibrary\Conversion\Conversion`) that will start

### ConversionHasBeenCompleted
### ConversionHasBeenCompletedEvent
This event is fired when a conversion has been completed.

The event has two public properties:

- `media`: the `\Spatie\MediaLibrary\Models\Media`-object of which a conversion has been completed
- `conversion`: the conversion (an instance of `\Spatie\MediaLibrary\Conversion\Conversion`) that has just been completed

### CollectionHasBeenCleared
### CollectionHasBeenClearedEvent
This event will be fired after a collection has been cleared.

The event has two public properties:
Expand All @@ -42,11 +42,11 @@ First you must create a listener class. Here's one that will log the paths of ad
namespace App\Listeners;

use Log;
use Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAdded;
use Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAddedEvent;

class MediaLogger
{
public function handle(MediaHasBeenAdded $event)
public function handle(MediaHasBeenAddedEvent $event)
{
$media = $event->media;
$path = $media->getPath();
Expand All @@ -59,8 +59,8 @@ Hook it up in `app/Providers/EventServiceProvider.php` to let Laravel know that

```php
protected $listen = [
'Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAdded' => [
'App\Listeners\MediaLogger'
Spatie\MediaLibrary\MediaCollections\Events\MediaHasBeenAddedEvent::class => [
App\Listeners\MediaLogger::class
],
];
```
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ blade view that will be used to display all form elements on a row in the compon
In this example we're going to add a custom property form field called `extra_field`.

```html
@include('media-library::livewire.partials.collection.fields')
@include('medialibrary::livewire.partials.collection.fields')

<div class="media-library-field">
<label class="media-library-label">Extra field</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ blade view that will be used to display all form elements on a row in the compon
In this example we're going to add a custom property form field called `extra_field`.

```html
@include('media-library::livewire.partials.collection.fields')
@include('medialibrary::livewire.partials.collection.fields')

<div class="media-library-field">
<label class="media-library-label">Extra field</label>
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Requirements
weight: 5
---

Laravel Media Library requires **PHP 8+** and **Laravel 9+**.
Laravel Media Library requires **PHP 8.2+** and **Laravel 10+**.

The [exif extension](http://php.net/manual/en/exif.installation.php) is required (on most systems it will be installed by default).
To create derived images **[GD](http://php.net/manual/en/book.image.php)** needs to be installed on your server.
Expand Down
17 changes: 9 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Spatie Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<php>
<env name="MAIL_MAILER" value="log"/>
</php>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
8 changes: 4 additions & 4 deletions src/Conversions/Actions/PerformConversionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Spatie\MediaLibrary\Conversions\Actions;

use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\Conversions\Events\ConversionHasBeenCompleted;
use Spatie\MediaLibrary\Conversions\Events\ConversionWillStart;
use Spatie\MediaLibrary\Conversions\Events\ConversionHasBeenCompletedEvent;
use Spatie\MediaLibrary\Conversions\Events\ConversionWillStartEvent;
use Spatie\MediaLibrary\Conversions\ImageGenerators\ImageGeneratorFactory;
use Spatie\MediaLibrary\MediaCollections\Filesystem;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
Expand All @@ -25,7 +25,7 @@ public function execute(
return;
}

event(new ConversionWillStart($media, $conversion, $copiedOriginalFile));
event(new ConversionWillStartEvent($media, $conversion, $copiedOriginalFile));

$manipulationResult = (new PerformManipulationsAction())->execute($media, $conversion, $copiedOriginalFile);

Expand All @@ -48,7 +48,7 @@ public function execute(

$media->markAsConversionGenerated($conversion->getName());

event(new ConversionHasBeenCompleted($media, $conversion));
event(new ConversionHasBeenCompletedEvent($media, $conversion));
}

protected function renameInLocalDirectory(
Expand Down
25 changes: 18 additions & 7 deletions src/Conversions/Actions/PerformManipulationsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Spatie\Image\Image;
use Spatie\MediaLibrary\Conversions\Conversion;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\ImageFactory;

class PerformManipulationsAction
{
public function execute(
Media $media,
Conversion $conversion,
string $imageFile
string $imageFile,
): string {

if ($conversion->getManipulations()->isEmpty()) {
return $imageFile;
}
Expand All @@ -28,21 +29,31 @@ public function execute(
$conversion->format($media->extension);
}

ImageFactory::load($conversionTempFile)
->manipulate($conversion->getManipulations())
->save();
$image = Image::useImageDriver(config('media-library.image_driver'))
->loadFile($conversionTempFile)
->format('jpg');

$conversion->getManipulations()->apply($image);

$image->save();

return $conversionTempFile;
}

protected function getConversionTempFileName(
Media $media,
Conversion $conversion,
string $imageFile
string $imageFile,
): string {
$directory = pathinfo($imageFile, PATHINFO_DIRNAME);

$fileName = Str::random(32)."{$conversion->getName()}.{$media->extension}";
$extension = $media->extension;

if ($extension === '') {
$extension = 'jpg';
}

$fileName = Str::random(32)."{$conversion->getName()}.{$extension}";

return "{$directory}/{$fileName}";
}
Expand Down
2 changes: 1 addition & 1 deletion src/Conversions/Commands/RegenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function getMediaToBeRegenerated(): Collection
// Get this arg first as it can also be passed to the greater-than-id branch
$modelType = $this->argument('modelType');

$startingFromId = (int)$this->option('starting-from-id');
$startingFromId = (int) $this->option('starting-from-id');
if ($startingFromId !== 0) {
$excludeStartingId = (bool) $this->option('exclude-starting-id') ?: false;

Expand Down
Loading

0 comments on commit a1823c0

Please sign in to comment.