Skip to content

Instantly share code, notes, and snippets.

@abdullahessam
Created April 16, 2022 16:08
Show Gist options
  • Save abdullahessam/ce1b05bf450f1d2b48c9bb02bbce7b2d to your computer and use it in GitHub Desktop.
Save abdullahessam/ce1b05bf450f1d2b48c9bb02bbce7b2d to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Spatie\Image\Image;
use Spatie\Image\Manipulations;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class AddWatermarkMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
collect($request->allFiles())
->flatten()
->filter(function (UploadedFile $file) {
if (app()->environment('testing')) {
return true;
}
return $file->isValid();
})
->each(function (UploadedFile $file) {
Image::load($file->getPathname())
->watermark('_site/assets/img/logo.png')
->watermarkOpacity(50)
->watermarkPosition(Manipulations::POSITION_CENTER)
->save();
});
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment