* @author Bharat Mediratta * @version $Revision: 17580 $ */ class CaptchaImageView extends GalleryView { /** * @see GalleryView::isImmediate */ function isImmediate() { return true; } /** * @see GalleryView::isAllowedInMaintenance */ function isAllowedInMaintenance() { return true; } /** * @see GalleryView::renderImmediate */ function renderImmediate($status, $error) { global $gallery; $session =& $gallery->getSession(); $code = $session->get('captcha.key'); if (empty($code)) { GalleryCoreApi::requireOnce('modules/captcha/classes/CaptchaHelper.class'); CaptchaHelper::generateCaptchaSessionKey(); $code = $session->get('captcha.key'); if (!$session->getId()) { $session->start(); } } header('Content-type: image/jpeg'); header('Pragma: No-cache'); /* Load the default background, black&white static */ $image = @ImageCreateFromJPEG(dirname(__FILE__) . '/data/code_bg.jpg'); $width = imagesx($image); $height = imagesy($image); /* Draw some colored ellipses at random to perturb the background */ for ($i = 0; $i < 40; $i++) { /* Pick a random 50-75% lightness color for this ellipse */ $color = ImageColorAllocate($image, rand(128, 192), rand(128, 192), rand(128, 192)); ImageEllipse($image, rand(-15, $width - 5), rand(-15, $height - 5), rand(20, $width), rand(20, $height), $color); } /* With characters 16 pixels apart, figure out the edge padding to center the whole lot */ $textX = ($width - 16 * strlen($code)) / 2; $textY = ($height - 12 * strlen($code)) / 2 - 3; /* Draw the code into the image, diagonally with some random displacement */ for ($i = 0; $i < strlen($code); $i++) { /* Pick a random 25-50% lightness color for this letter */ $color = ImageColorAllocate($image, rand(60, 120), rand(60, 120), rand(60, 120)); ImageString($image, 5, $textX + $i*16 + rand(-2, 2), $textY + $i*12 + rand(-5, 5), substr($code, $i, 1), $color); } /* Output the image and reclaim the memory it used */ /* Use low quality jpeg compression to make the image less OCR-able */ ImageJPEG($image, '', 50); ImageDestroy($image); return null; } } ?>