generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Spatie Image remove bkwld/croppa * plug Spatie image to getCroppaUrl * fix image url with Spatie Image * add config for images thumbnails * add delete thumbnails * Fix styling * add controller, route for rendering images on the fly --------- Co-authored-by: webplusmultimedia <[email protected]>
- Loading branch information
1 parent
94c19f9
commit 9667987
Showing
9 changed files
with
320 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use GalleryJsonMedia\JsonMedia\Controllers\JsonImageController; | ||
use GalleryJsonMedia\JsonMedia\UrlParser; | ||
use Illuminate\Support\Facades\Route; | ||
|
||
Route::get('{path}', [JsonImageController::class, 'handle']) | ||
->where('path', UrlParser::make()->routePattern()); |
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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* Created by PhpStorm. | ||
* | ||
* @category Category | ||
* | ||
* @author daniel | ||
* | ||
* @link http://webplusm.net | ||
* Date: 16/04/2024 10:09 | ||
*/ | ||
|
||
namespace GalleryJsonMedia\JsonMedia\Controllers; | ||
|
||
use GalleryJsonMedia\JsonMedia\ImageManipulation\Croppa; | ||
use GalleryJsonMedia\JsonMedia\UrlParser; | ||
use Illuminate\Filesystem\FilesystemAdapter; | ||
use Illuminate\Routing\Controller; | ||
use Illuminate\Support\Facades\Storage; | ||
use League\Flysystem\Local\LocalFilesystemAdapter; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class JsonImageController extends Controller | ||
{ | ||
public function __construct(protected UrlParser $urlParser) | ||
{ | ||
} | ||
|
||
public function handle(string $requestPath) | ||
{ | ||
// Validate the signing token | ||
$token = $this->urlParser->signingToken($requestPath); | ||
|
||
if ($token !== request('_token')) { | ||
throw new NotFoundHttpException('Token mismatch'); | ||
} | ||
|
||
$url = $this->urlParser->parse($requestPath); | ||
|
||
/** @var FilesystemAdapter $storage */ | ||
$storage = Storage::disk(config('gallery-json-media.disk')); | ||
|
||
/**@todo : for non local file Soon */ | ||
// Create the image file | ||
$croppa = (new Croppa(filesystem: $storage, filePath: $url['path'], width: $url['width'], height: $url['height'])); | ||
$croppa->render(); | ||
if ($storage->getAdapter() instanceof LocalFilesystemAdapter) { | ||
return redirect($requestPath, 301); | ||
} | ||
|
||
$absolutePath = $storage->path($requestPath); | ||
|
||
return new BinaryFileResponse($absolutePath, 200, [ | ||
'Content-Type' => $storage->mimeType($requestPath), | ||
]); | ||
|
||
} | ||
} |
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GalleryJsonMedia\JsonMedia\ImageManipulation; | ||
|
||
use GalleryJsonMedia\JsonMedia\UrlParser; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use Spatie\Image\Exceptions\InvalidImageDriver; | ||
use Spatie\Image\Exceptions\InvalidManipulation; | ||
use Spatie\Image\Image; | ||
use Spatie\Image\Manipulations; | ||
|
||
final class Croppa | ||
{ | ||
public function __construct(protected Filesystem $filesystem, private string $filePath, private ?int $width = null, private ?int $height = null) | ||
{ | ||
} | ||
|
||
public function url(): string | ||
{ | ||
/*if (! $this->filesystem->exists($this->getPathNameForThumbs())) { | ||
$this->save(); | ||
}*/ | ||
$url = $this->filesystem->url($this->getPathNameForThumbs()); | ||
$url .= '?_token=' . UrlParser::make()->signingToken($url); | ||
|
||
return $url; | ||
} | ||
|
||
/** | ||
* @throws InvalidManipulation | ||
* @throws InvalidImageDriver | ||
*/ | ||
public function render(): void | ||
{ | ||
$image = Image::load($this->filesystem->path($this->filePath)) | ||
->useImageDriver(config('gallery-json-media.images.driver')); | ||
|
||
$manipulations = new Manipulations(); | ||
$manipulations->quality(config('gallery-json-media.images.quality')); | ||
|
||
if ($this->width and $this->height) { | ||
$manipulations->crop( | ||
cropMethod: config('gallery-json-media.images.thumbnails-crop-method'), | ||
width: $this->width, | ||
height: $this->height | ||
); | ||
} else { | ||
if ($this->width) { | ||
$manipulations->width($this->width); | ||
} | ||
if ($this->height) { | ||
$manipulations->height($this->height); | ||
} | ||
} | ||
$image->manipulate($manipulations) | ||
->save($this->filesystem->path($this->getPathNameForThumbs())); | ||
} | ||
|
||
protected function getPathNameForThumbs(): string | ||
{ | ||
return $this->getBaseNameForTumbs() . $this->getSuffix() . '.' . $this->getFileInfo()['extension']; | ||
} | ||
|
||
protected function getBaseNameForTumbs() | ||
{ | ||
$basePath = str($this->filePath)->beforeLast('/'); | ||
|
||
return $basePath . '/' . $this->getFileInfo()['filename']; | ||
} | ||
|
||
protected function getSuffix(): string | ||
{ | ||
$suffix = '-'; | ||
if ($this->width === null && $this->height === null) { | ||
return ''; | ||
} | ||
$width = $this->width ?? '_'; | ||
$height = $this->height ?? '_'; | ||
$suffix .= $width . 'x' . $height; | ||
|
||
return $suffix; | ||
} | ||
|
||
/** | ||
* @return array{dirname : string,basename:string,extension : string,filename : string} | ||
*/ | ||
protected function getFileInfo(): array | ||
{ | ||
return pathinfo($this->filesystem->path($this->filePath)); | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$search = $this->filesystem->path($this->getBaseNameForTumbs() . '-*.*'); | ||
foreach (glob($search) as $file) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
public function delete(): void | ||
{ | ||
$this->reset(); | ||
$this->filesystem->delete($this->filePath); | ||
} | ||
} |
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
Oops, something went wrong.