एक फ़ाइल कॉल Captcha.class.php
बनाएं और इस डाल:
class Captcha {
private $font = '/path/to/font/yourfont.ttf'; // get any font you like and dont forget to update this.
private function generateCode($characters) {
$possible = '23456789bcdfghjkmnpqrstvwxyz'; // why not 1 and i, because they look similar and its hard to read sometimes
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;
}
function getImage($width, $height, $characters) {
$code = $this->generateCode($characters);
$fontSize = $height * 0.75;
$image = imagecreate($width, $height);
if(!$image) {
return FALSE;
}
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 66, 42, 32);
$noiseColor = imagecolorallocate($image, 150, 150, 150);
for($i=0; $i<($width*$height)/3; $i++) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noiseColor);
}
for($i=0; $i<($width*$height)/150; $i++) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noiseColor);
}
$textbox = imagettfbbox($fontSize, 0, $this->font, $code);
if(!$textbox) {
return FALSE;
}
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $fontSize, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['captcha'] = $code;
}
}
फिर अपने पेज में आप कर सकते हैं:
<img src="/captcha.php" />
में फिर
/captcha.php
आप रखा जाएगा:
session_start();
require('Captcha.class.php');
$Captcha = new Captcha();
$Captcha->getImage(120,40,6);
आप पैराम्स को आपके रूप में बदल सकते हैं अलग कैप्चा भी दिखाना चाहते हैं।
इस तरह आप फ्लाई पर उत्पन्न करेंगे। यदि आप चाहें तो डिस्क को हमेशा डिस्क पर सहेज सकते हैं।
आप जीडी का उपयोग क्यों नहीं करते? –
यदि आपके पास कुल 5000 से अधिक संपर्क या न्यूजलेटर हैं, तो आप बहुत लोकप्रिय हैं :) यह संसाधनों की अक्षम और अपशिष्ट लगता है। आवश्यकता होने पर केवल छवि उत्पन्न करें। – Bakudan
अच्छी तरह से मैं यह सुनिश्चित करने के लिए 5000 उत्पन्न करता हूं कि जब मैं यादृच्छिक करता हूं तो मुझे दो बार नहीं मिलता है। – Owan