ptachoire/cssembed
Embed and inline images in CSS by converting them to data URIs. ptachoire/cssembed scans your stylesheets for url(...) references and replaces eligible assets with base64-encoded content, reducing HTTP requests and simplifying distribution of self-contained CSS.
Installation
composer require ptachoire/cssembed
Add the service provider to config/app.php under providers:
Ptachoire\CssEmbed\CssEmbedServiceProvider::class,
Basic Usage Embed CSS URLs in a Blade template:
use Ptachoire\CssEmbed\CssEmbed;
// In a controller or service
$cssEmbed = app(CssEmbed::class);
$embeddedCss = $cssEmbed->embed('path/to/your/styles.css');
echo $embeddedCss;
First Use Case Replace external CSS URLs in Blade views with embedded versions:
<link href="{{ $cssEmbed->embed('css/main.css') }}" rel="stylesheet">
Ensure the cssembed middleware is registered in app/Http/Kernel.php:
protected $middleware = [
// ...
\Ptachoire\CssEmbed\Middleware\CssEmbedMiddleware::class,
];
Embedding CSS in Controllers
public function show()
{
$cssEmbed = app(CssEmbed::class);
$embeddedStyles = $cssEmbed->embedMultiple([
'css/main.css',
'css/theme.css'
]);
return view('home', ['styles' => $embeddedStyles]);
}
Blade Directives Create a custom Blade directive for seamless embedding:
// In AppServiceProvider@boot()
Blade::directive('embedCss', function ($expression) {
return "<?php echo app(\Ptachoire\CssEmbed\CssEmbed::class)->embed({$expression}); ?>";
});
Usage:
<link href="{{ embedCss('css/main.css') }}" rel="stylesheet">
Middleware Integration Automatically embed CSS for specific routes:
Route::middleware(['cssembed'])->group(function () {
Route::get('/dashboard', 'DashboardController@index');
});
$embeddedCss = Cache::remember('embedded_css_main', now()->addDays(7), function () {
return $cssEmbed->embed('css/main.css');
});
$cssEmbed->embed("css/themes/{$theme}.css");
Relative Paths
Ensure CSS files referenced in @import or url() within your CSS use absolute paths (e.g., /images/logo.png) or configure the package to resolve them:
$cssEmbed->setBaseUrl('/public');
Large Files Embedding very large CSS files may increase response size. Consider:
embedMultiple with chunking.Middleware Order
Place CssEmbedMiddleware after ShareErrorsFromSession and StartSession in Kernel.php to avoid embedding issues with session-based paths.
\Log::debug('Embedded CSS:', ['content' => $embeddedCss]);
if (!file_exists(public_path('css/main.css'))) {
throw new \Exception('CSS file not found');
}
$cssEmbed->setStoragePath(storage_path('app/embedded_css'));
$cssEmbed->exclude(['css/vendor.css', 'css/print.css']);
Custom Embedders
Extend the CssEmbedder interface to support additional formats (e.g., SCSS):
class ScssEmbedder implements CssEmbedder {
public function embed(string $path): string {
// Custom SCSS processing logic
}
}
Register it in the service provider:
$this->app->bind(CssEmbedder::class, ScssEmbedder::class);
Pre/Post Processing Hook into the embedding process via events (if the package supports them) or wrap the embedder:
$embeddedCss = $cssEmbed->embed('css/main.css');
$processedCss = str_replace('/* old comment */', '', $embeddedCss);
How can I help you explore Laravel packages today?