mike42/gfx-php
gfx-php is a PHP graphics helper library by mike42, aimed at generating and manipulating simple bitmap-style images and drawing primitives. Useful for lightweight image rendering tasks where full GD/Imagick workflows feel too heavy.
Installation Add the package via Composer (updated for v0.6 compatibility):
composer require mike42/gfx-php:^0.6
Laravel handles autoloading automatically.
First Use Case: Drawing a Simple Image Create a basic image with text or shapes (unchanged):
use Mike42\Gfx\Gfx;
$image = Gfx::create(200, 100);
$image->fill(255, 255, 255); // White background
$image->text('Hello, Laravel!', 10, 10, 0, 0, 0); // Black text
$image->save('output.png');
New Feature: Reading BMP Files Load existing BMP images for editing or analysis:
$bmpImage = Gfx::load('input.bmp');
$bmpImage->save('output_converted.png'); // Convert BMP to PNG
Where to Look First
docs/ folder for BMP format specifics.src/Mike42/Gfx/GfxImage.php for load() method updates.tests/ (e.g., BmpTest.php).Image Generation (Unchanged)
Gfx::create(width, height) for new canvases.fill(), rect(), or text().BMP File Handling (New)
Gfx::load('path/to/file.bmp') to read BMPs (supports 8/16/24/32-bit, RLE compression, etc.).
$bmp = Gfx::load('scan.bmp');
$bmp->save('scan_converted.png'); // Auto-converts to PNG
$bmp = Gfx::load('image.bmp');
$width = $bmp->getWidth();
$height = $bmp->getHeight();
$bitsPerPixel = $bmp->getBitsPerPixel(); // e.g., 24
foreach (Storage::files('bmp_uploads') as $path) {
$image = Gfx::load($path);
$image->save(str_replace('bmp', 'png', $path));
}
Dynamic Content (Unchanged)
Laravel Integration (Updated)
$bmp = Gfx::load($request->file('bmp')->path());
Storage::disk('public')->put('converted/' . $request->filename . '.png', $bmp->getImage());
$request->validate([
'bmp' => 'required|mimes:bmp',
]);
Font Handling (Unchanged)
GfxFont::load() for text rendering.BMP Format Quirks (New)
$image->getBitsPerPixel() and log unsupported formats:
if ($image->getBitsPerPixel() !== 24 && $image->getBitsPerPixel() !== 32) {
Log::warning("Unsupported BMP format: {$image->getBitsPerPixel()} bits");
}
save().imagealphablending() in PHP’s GD).Font Paths (Unchanged)
GfxFont::load() fails with incorrect paths.storage_path('fonts/arial.ttf')).Memory Limits (Updated)
memory_limit.$bmp = Gfx::load('large.bmp');
$bmp->resize(1920, 1080); // Downsample to HD
Output Formatting (Unchanged)
Content-Type headers for BMP outputs (though BMP is rarely served directly).return response($image->getImage(), 200, ['Content-Type' => 'image/png']);
Color Values (Unchanged)
$rgb = $bmp->getPixelColor(10, 10); // Returns RGB integer
BMP Metadata Dump BMP properties to diagnose issues:
$bmp = Gfx::load('problem.bmp');
dd([
'width' => $bmp->getWidth(),
'height' => $bmp->getHeight(),
'bits' => $bmp->getBitsPerPixel(),
'compression' => $bmp->getCompression(),
]);
Fallback for Unsupported BMPs Gracefully handle unsupported formats:
try {
$image = Gfx::load('file.bmp');
} catch (\Exception $e) {
return response()->view('errors.bmp_unsupported');
}
Performance Profiling Benchmark BMP loading for large files:
$start = microtime(true);
$bmp = Gfx::load('large.bmp');
Log::info("BMP load time: " . (microtime(true) - $start) . "s");
BMP-Specific Filters (New)
Extend GfxImage to add BMP-aware filters (e.g., RLE decompression):
class BmpFilter extends GfxImage {
public function decompressRLE() {
// Custom logic for RLE BMPs
}
}
Format Auto-Detection Create a helper to detect and load BMPs/PNGs/JPEGs dynamically:
function loadImage($path) {
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
return $ext === 'bmp'
? Gfx::load($path)
: imagecreatefromstring(file_get_contents($path));
}
Event Listeners (Unchanged)
Trigger events for BMP conversions (e.g., bmp.converted):
event(new BmpConverted($bmp, $outputPath));
Testing BMP Support Mock BMP loading in tests:
$mockBmp = Mockery::mock('Mike42\Gfx\GfxImage');
$mockBmp->shouldReceive('getWidth')->andReturn(800);
$mockBmp->shouldReceive('getImage')->andReturn(file_get_contents('test.png'));
How can I help you explore Laravel packages today?