Okay here's our code of this captcha image:
Code:
<?php
header('Content-type: image/jpg');
$im = imagecreatefromjpeg ("image.jpg");
$rand1 = rand(1,255);
$rand2 = rand(1,255);
$rand3 = rand(1,255);
$color = imagecolorallocate($im, $rand1, $rand2, $rand3);
$text = rand(1000, 9999);
$font = 'text-font.otf';
$size = 30;
imagettftext($im, $size, 0, 70, 45, $color, $font, $text);
imagejpeg($im);
imagedestroy($im);
?>
But if we want to use this image on our sites we must add:
Code:
$_SESSION['captcha'] = $text;
on the bottom of our captcha file. So.. now let's make our form secure.
First of all we must start the session
Code:
<?php
session_start();
session_start(); - Starts the session
Now I'm using a form for checking that is the security code correct
Code:
if(isset($_POST['submit'])){
$ses = $_SESSION['captcha'];
if($ses == $_POST['code']){
echo "Code is correct!";
}else{
echo "Code is incorrect!";
}
if(isset($_POST['submit']) && !empty($_POST['code'])){ - If someone has clicked the submit button let's continue with our script
$ses = $_SESSION['captcha']; - Let's get the captcha code from the session
if($ses == $_POST['code']){ - If captcha's code is same as the code you posted
echo "Code is correct!"; - let's say that the code was correct
}else{ - but if they are not same
echo "Code is incorrect!"; - let's say that the code is not correct
} - This ends the if and else statement
Code:
}else{
echo "<img src='captcha.php'><br>";
echo "<form action='' method='post'>"
."<input type='text' name='code'>"
."<input type='submit' name='submit' value='Enter'>"
."</form>";
}
}else{ - If anyone hasnt clicked the submit button let's display our form
echo "<img src='captcha.php'><br>"; - Shows our captcha image
echo "<form action='' method='post'>"
."<input type='text' name='code'>"
."<input type='submit' name='submit' value='Enter'>"
."</form>"; - Our form
} - This ends the if and else statement

And we are done.. that was so simple 
You can customize this script for your forms 
Here's the full script:
captcha.php
Code:
<?php
session_start();
$im = imagecreatefromjpeg ("image.jpg");
$rand1 = rand(1,255);
$rand2 = rand(1,255);
$rand3 = rand(1,255);
$color = imagecolorallocate($im, $rand1, $rand2, $rand3);
$text = rand(1000, 9999);
$font = 'text-font.otf';
$size = 30;
imagettftext($im, $size, 0, 70, 45, $color, $font, $text);
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im);
$_SESSION['captcha'] = $text;
?>
index.php
Code:
<?php
session_start();
if(isset($_POST['submit']) && !empty($_POST['code'])){
$ses = $_SESSION['captcha'];
if($ses == $_POST['code']){
echo "Code is correct!";
}else{
echo "Code is incorrect!";
}
}else{
echo "<img src='captcha.php'><br>";
echo "<form action='' method='post'>"
."<input type='text' name='code'>"
."<input type='submit' name='submit' value='Enter'>"
."</form>";
}
?>
I attached the files also 
Enjoy!