Installation:
composer require appventus/pixel-art-bundle
Ensure the bundle is enabled in config/bundles.php (Symfony 4.4+ auto-discovers it).
First Use Case: Add the Twig filter to a template:
{{ "Hello"|pixelart|raw }}
This renders "Hello" in a default pixel-art font. The |raw ensures the HTML output is not escaped.
Where to Look First:
block, small, speed).Resources/views/ for default templates (if extended).Dynamic PixelArt Rendering: Use the filter with optional font parameter:
{% for name in ["Laravel", "PHP"] %}
{{ name|pixelart("speed")|raw }}
{% endfor %}
$twig->render('{{ "Pixel"|pixelart("block")|raw }}');
Customizing Output:
<div> with CSS classes for alignment/scaling:
<div class="pixel-art-container">
{{ "Laravel"|pixelart("small")|raw }}
</div>
$fonts = $this->get('pixel_art.font_manager')->getAvailableFonts();
Reusable Components: Create a Twig macro for consistency:
{% macro pixelText(text, font="default") %}
<span class="pixel-font">{{ text|pixelart(font)|raw }}</span>
{% endmacro %}
Usage:
{{ _self.pixelText("Dev", "speed") }}
API Responses: Return pixel-art as part of JSON responses (base64-encoded SVG):
return response()->json([
'message' => 'Pixelated!',
'pixelArt' => base64_encode($twig->render('{{ "API"|pixelart("block")|raw }}'))
]);
Font Availability:
"custom") is not found, the filter silently falls back to default. Validate fonts dynamically:
if (!$this->get('pixel_art.font_manager')->hasFont('custom')) {
throw new \InvalidArgumentException("Font 'custom' not available.");
}
Performance:
$cacheKey = "pixel_art_{$text}_{$font}";
return Cache::remember($cacheKey, now()->addHours(1), function() use ($text, $font) {
return $twig->render('{{ "'.$text.'"|pixelart("'.$font.'")|raw }}');
});
Twig Auto-escaping:
Always use |raw to prevent HTML escaping from breaking the pixel-art SVG.
Bundle Compatibility:
PixelArt class and use it directly).Resources/public/fonts/ directory is symlinked or copied to public/:
php bin/console assets:install
viewBox attributes in SVG.fonts.md).Add Custom Fonts:
.ttf/.otf files in Resources/public/fonts/ and update the font manager:
# config/packages/pixel_art.yaml
pixel_art:
fonts:
- { name: "custom", path: "%kernel.project_dir%/public/fonts/custom.ttf" }
Modify Rendering Logic:
PixelArt class (e.g., in app/PixelArt/PixelArt.php) to customize:
getColor() method).Laravel-Specific Adaptations:
// app/Facades/PixelArt.php
public static function make($text, $font = 'default') {
return app('twig')->render('{{ "'.$text.'"|pixelart("'.$font.'")|raw }}');
}
public function register() {
$this->app->register(new \AppVentus\PixelArtBundle\AppVentusPixelArtBundle());
}
.pixel-art-container svg {
--pixel-color: #ff0000;
}
.pixel-art-container {
transform: scale(1.5);
transform-origin: 0 0;
}
<div class="pixel-art-container" aria-label="Pixel art text: Laravel">
{{ "Laravel"|pixelart("speed")|raw }}
</div>
How can I help you explore Laravel packages today?