Skip to content

Commit

Permalink
intervention image update, deps update
Browse files Browse the repository at this point in the history
  • Loading branch information
k2so-dev committed Dec 15, 2023
1 parent 5a699cc commit c5dc150
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
37 changes: 37 additions & 0 deletions app/Helpers/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Helpers;

use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver as GdDriver;

class Image
{
/**
* Convert image to webp, jpeg or png format and resize it
*/
public static function convert(string $source, string $target, int $width = null, int $height = null, string $extension = 'webp', int $quality = 90): void
{
$manager = new ImageManager(new GdDriver);

$image = $manager->read($source);

$maxSize = 1920;

if ($width && $height) {
$image->cover($width, $height);
} elseif ($width || $image->width() > $maxSize) {
$image->scale(width: $width ?? $maxSize);
} elseif ($height || $image->height() > $maxSize) {
$image->scale(height: $height ?? $maxSize);
}

if ($extension === 'webp') {
$image->toWebp($quality)->save($target);
} else if ($extension === 'jpeg') {
$image->toJpeg($quality)->save($target);
} else if ($extension === 'png') {
$image->toPng($quality)->save($target);
}
}
}
24 changes: 3 additions & 21 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace App\Providers;

use App\Helpers\Image;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -27,29 +27,11 @@ public function register(): void
public function boot(): void
{
/**
* Convert uploaded image to any format and resize it
* Convert uploaded image to webp, jpeg or png format and resize it
*/
UploadedFile::macro('convert', function (int $width = null, int $height = null, string $extension = 'webp', int $quality = 90): UploadedFile {
return tap($this, function (UploadedFile $file) use ($width, $height, $extension, $quality) {
$image = Image::make($file->path());
$image->orientate();

$maxSize = 1920;

if ($width && $height) {
$image->fit($width, $height);
} elseif ($width || $image->width() > $maxSize) {
$image->resize($width ?? $maxSize, null, function ($constraint) {
$constraint->aspectRatio();
});
} elseif ($height || $image->height() > $maxSize) {
$image->resize(null, $height ?? $maxSize, function ($constraint) {
$constraint->aspectRatio();
});
}

$image->save($file->path(), $quality, $extension);
$image->destroy();
Image::convert($file->path(), $file->path(), $width, $height, $extension, $quality);
});
});

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"require": {
"php": "^8.2",
"guzzlehttp/guzzle": "^7.2",
"intervention/image": "^2.7",
"intervention/image": "^3.0",
"laravel/framework": "^10.34",
"laravel/octane": "^2.1",
"laravel/sanctum": "^3.3",
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
"api": "php artisan octane:start --watch --port=8000 --host=127.0.0.1"
},
"devDependencies": {
"@iconify-json/heroicons": "^1.1.15",
"@iconify-json/heroicons": "^1.1.16",
"@iconify/vue": "^4.1.1",
"@nuxt/devtools": "^1.0.4",
"@nuxt/devtools": "^1.0.6",
"@nuxt/image": "^1.1.0",
"@nuxt/ui": "^2.11.0",
"@nuxt/ui": "^2.11.1",
"@pinia/nuxt": "^0.5.1",
"chokidar": "^3.5.3",
"cross-env": "^7.0.3",
"dayjs-nuxt": "^2.1.9",
"nuxt": "^3.8.2",
"nuxt-security": "^1.0.0-rc.4",
"vue": "^3.3.7",
"nuxt-security": "^1.0.0",
"vue": "^3.3.11",
"vue-router": "^4.2.5"
}
}

0 comments on commit c5dc150

Please sign in to comment.