Animierte CAPTCHAs und GIFs selbst erstellen
Da ich noch nie mit PHP ein animiertes GIF erstellt habe wollte ich das mal testen. Um auch etwas halbwegs sinnvolles zu tun habe ich eine Klasse erstellt die animierte CAPTCHAs erstellen kann.
Bei animierten GIFs und PHP gibt es im Prinzip 2 Lösungen: Man nutzt Imagick, oder man baut das GIF-Format binär nach. Für letzteres gibt es bereits die Klasse GIFEncoder, die ich auch hier nutze. Die GD-Funktionen von PHP bieten leider keine Möglichkeit (mehr), animierte GIFs zu erstellen.
Das Projekt AnimatedCaptcha liegt auf Github. Man übergibt der Klasse eine Zeichenfolge, stellt ein paar Parameter ein und erhält als Ergebnis ein animiertes GIF. Ich habe ein Plugin-System eingebaut, sodass weitere Plugins schnell erstellt werden können. Ich habe bereits 3 angelegt:
MovingRectangle:
Snow:
RollingBall:
Diese 3 Plugins haben jeweils weniger als 40 Zeilen Code. Hier 2 Beispiele der Benutzung:
<?php // Example for the MovingRectangle Plugin $pluginOptions = array( 'text' => 'PHPGangsta', 'textSize' => 3, 'textColor' => '000000', 'backgroundColor' => 'ffffff', 'foregroundColor' => '000000', 'windowWidth' => 30, 'pixelPerFrame' => 2, ); require_once 'AnimatedCaptcha.php'; $animatedCaptcha = new AnimatedCaptcha(); $animatedCaptcha->setMillisecondsBetweenFrames(10) ->setPluginName('MovingRectangle') ->setPluginOptions($pluginOptions) ->render() ->outputRenderedImage();
Und Nummer 2:
<?php // Example for the Snow Plugin $pluginOptions = array( 'text' => 'PHPGangsta', 'textSize' => 4, 'textColor' => '000000', 'backgroundColor' => 'ffffff', 'foregroundColor' => '000000', 'strength' => 20, ); require_once 'AnimatedCaptcha.php'; $animatedCaptcha = new AnimatedCaptcha(); $animatedCaptcha->setMillisecondsBetweenFrames(10) ->setPluginName('Snow') ->setPluginOptions($pluginOptions) ->render() ->outputRenderedImage();
Aktuell habe ich mindestens 3 Dinge auf der Liste die man noch erweitern könnte:
- TTF-Schriftarten wären nett um unterschiedlichere Ausgaben zu bekommen
- Eventuell als Alternative Imagick, das könnte schneller/besser sein als die GIFEncoder Lösung, muß man ausprobieren
- mehr Plugins!