xref: /imagick/examples/captcha.php (revision 7599f2b8)
1<?php
2
3/*
4	A very primitive captcha implementation
5*/
6
7/* Create Imagick object */
8$Imagick = new Imagick();
9
10/* Create the ImagickPixel object (used to set the background color on image) */
11$bg = new ImagickPixel();
12
13/* Set the pixel color to white */
14$bg->setColor( 'white' );
15
16/* Create a drawing object and set the font size */
17$ImagickDraw = new ImagickDraw();
18
19/* Set font and font size. You can also specify /path/to/font.ttf */
20$ImagickDraw->setFont( 'Helvetica Regular' );
21$ImagickDraw->setFontSize( 20 );
22
23/* Create the text */
24$alphanum = 'ABXZRMHTL23456789';
25$string = substr( str_shuffle( $alphanum ), 2, 6 );
26
27/* Create new empty image */
28$Imagick->newImage( 85, 30, $bg );
29
30/* Write the text on the image */
31$Imagick->annotateImage( $ImagickDraw, 4, 20, 0, $string );
32
33/* Add some swirl */
34$Imagick->swirlImage( 20 );
35
36/* Create a few random lines */
37$ImagickDraw->line( rand( 0, 70 ), rand( 0, 30 ), rand( 0, 70 ), rand( 0, 30 ) );
38$ImagickDraw->line( rand( 0, 70 ), rand( 0, 30 ), rand( 0, 70 ), rand( 0, 30 ) );
39$ImagickDraw->line( rand( 0, 70 ), rand( 0, 30 ), rand( 0, 70 ), rand( 0, 30 ) );
40$ImagickDraw->line( rand( 0, 70 ), rand( 0, 30 ), rand( 0, 70 ), rand( 0, 30 ) );
41$ImagickDraw->line( rand( 0, 70 ), rand( 0, 30 ), rand( 0, 70 ), rand( 0, 30 ) );
42
43/* Draw the ImagickDraw object contents to the image. */
44$Imagick->drawImage( $ImagickDraw );
45
46/* Give the image a format */
47$Imagick->setImageFormat( 'png' );
48
49/* Send headers and output the image */
50header( "Content-Type: image/{$Imagick->getImageFormat()}" );
51echo $Imagick->getImageBlob( );
52
53?>