We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
With this midification, all images found in your /public directory and all recursive directories will be encoded to base64 on the fly.
to create a new middleware file.
add this code to the newly created middleware file.
namespace App\Http\Middleware;
use Closure;
class EncodeImagePaths { public function handle($request, Closure $next) { $response = $next($request);
if ($response instanceof \Illuminate\Http\Response) { $content = $response->getContent(); // Regex to find image paths more generally $pattern = '/src="([^"]+\.(jpg|jpeg|png|gif))"/i'; $callback = function ($matches) { $imagePath = public_path($matches[1]); // Get the absolute path of the image if (file_exists($imagePath)) { $type = pathinfo($imagePath, PATHINFO_EXTENSION); $data = file_get_contents($imagePath); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); return 'src="' . $base64 . '"'; } else { return $matches[0]; // No change if file doesn't exist } }; // Replace content $newContent = preg_replace_callback($pattern, $callback, $content); $response->setContent($newContent); } return $response; }
}
protected $middleware = [ // other middleware \App\Http\Middleware\EncodeImagePaths::class, ];
The text was updated successfully, but these errors were encountered:
how do I check if I did everything correctly? thanks for the code btw
Sorry, something went wrong.
No branches or pull requests
With this midification, all images found in your /public directory and all recursive directories will be encoded to base64 on the fly.
php artisan make:middleware EncodeImagePaths
to create a new middleware file.
add this code to the newly created middleware file.
namespace App\Http\Middleware;
use Closure;
class EncodeImagePaths
{
public function handle($request, Closure $next)
{
$response = $next($request);
}
protected $middleware = [
// other middleware
\App\Http\Middleware\EncodeImagePaths::class,
];
The text was updated successfully, but these errors were encountered: