Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"illuminate/support": "^13.0",
"illuminate/validation": "^13.0",
"illuminate/view": "^13.0",
"intervention/image": "^3.2",
"intervention/image": "^4.0",
"jenssegers/agent": "^2.6",
"laminas/laminas-diactoros": "^3.0",
"laminas/laminas-httphandlerrunner": "^2.6",
Expand Down
2 changes: 1 addition & 1 deletion framework/core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"illuminate/support": "^13.0",
"illuminate/validation": "^13.0",
"illuminate/view": "^13.0",
"intervention/image": "^3.2",
"intervention/image": "^4.0",
"jenssegers/agent": "^2.6.4",
"laminas/laminas-diactoros": "^3.0",
"laminas/laminas-httphandlerrunner": "^2.6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
return $file->getStream();
}

$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))
->scale(64, 64)
->toPng();

$this->fileExtension = 'png';

$encodedImage = $this->imageManager->decode($file->getStream()->getMetadata('uri'))
->scale(64, 64)
->encodeUsingFileExtension($this->fileExtension);

return $encodedImage;
}
}
12 changes: 3 additions & 9 deletions framework/core/src/Api/Controller/UploadLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ class UploadLogoController extends UploadImageController

protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
{
$image = $this->imageManager->read($file->getStream()->getMetadata('uri'))
$image = $this->imageManager->decode($file->getStream()->getMetadata('uri'))
->scale(height: 60);

if ($image->isAnimated()) {
$this->resolvedExtension = 'gif';
$this->resolvedExtension = $image->isAnimated() ? 'gif' : 'webp';

return $image->toGif();
}

$this->resolvedExtension = 'webp';

return $image->toWebp();
return $image->encodeUsingFileExtension($this->resolvedExtension);
}

protected function fileExtension(ServerRequestInterface $request, UploadedFileInterface $file): string
Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Api/Resource/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private function uploadAvatarFromUrl(User $user, string $url): void
$urlContents = $this->retrieveAvatarFromUrl($url);

if ($urlContents !== null) {
$image = $this->imageManager->read($urlContents);
$image = $this->imageManager->decodeBinary($urlContents);

$this->avatarUploader->upload($user, $image);
}
Expand Down
4 changes: 2 additions & 2 deletions framework/core/src/User/AvatarUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function upload(User $user, ImageInterface $image): void
$avatarPath = Str::random();

if ($image->isAnimated()) {
$encodedImage = $image->toGif();
$avatarPath .= '.gif';
} else {
$encodedImage = $image->toWebp();
$avatarPath .= '.webp';
}

$encodedImage = $image->encodeUsingPath($avatarPath);

$this->removeFileAfterSave($user);
$user->changeAvatarPath($avatarPath);

Expand Down
7 changes: 3 additions & 4 deletions framework/core/src/User/AvatarValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
use Flarum\Foundation\ValidationException;
use Flarum\Locale\TranslatorInterface;
use Illuminate\Validation\Factory;
use Intervention\Gif\Exceptions\DecoderException as GifDecoderException;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\ImageException;
use Intervention\Image\ImageManager;
use Psr\Http\Message\UploadedFileInterface;
use Symfony\Component\Mime\MimeTypes;
Expand Down Expand Up @@ -74,8 +73,8 @@ protected function assertFileMimes(UploadedFileInterface $file): void
}

try {
$this->imageManager->read($file->getStream()->getMetadata('uri'));
} catch (DecoderException|GifDecoderException) {
$this->imageManager->decode($file->getStream()->getMetadata('uri'));
} catch (ImageException) {
$this->raise('image');
}
}
Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/User/Command/UploadAvatarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle(UploadAvatar $command): User

$this->validator->assertValid(['avatar' => $command->file]);

$image = $this->imageManager->read($command->file->getStream()->getMetadata('uri'));
$image = $this->imageManager->decode($command->file->getStream()->getMetadata('uri'));

$this->events->dispatch(
new AvatarSaving($user, $actor, $image)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ protected function fire(): int

try {
$contents = $this->uploadDir->get($oldPath);
$webpContents = $this->imageManager->read($contents)->toWebp();

$newPath = pathinfo($oldPath, PATHINFO_FILENAME).'.webp';

$webpContents = $this->imageManager->decodeBinary($contents)->encodeUsingPath($newPath);

$this->uploadDir->put($newPath, $webpContents);

User::where('id', $user->id)->update(['avatar_url' => $newPath]);
Expand Down
5 changes: 3 additions & 2 deletions framework/core/tests/unit/User/AvatarUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Eloquent\Model;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\ImageManager;
use Mockery as m;

Expand Down Expand Up @@ -93,7 +94,7 @@ public function test_changing_avatar_removes_file()
$user->changeAvatarPath('ABCDEFGHabcdefgh.png');
$user->syncOriginal();

$this->uploader->upload($user, ImageManager::gd()->create(50, 50));
$this->uploader->upload($user, ImageManager::usingDriver(Driver::class)->createImage(50, 50));

// Simulate saving
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
Expand All @@ -111,7 +112,7 @@ public function test_upload_stores_webp_for_static_images()

$user = new User();

$this->uploader->upload($user, ImageManager::gd()->create(50, 50));
$this->uploader->upload($user, ImageManager::usingDriver(Driver::class)->createImage(50, 50));

// Simulate saving
foreach ($user->releaseAfterSaveCallbacks() as $callback) {
Expand Down