Sunday, July 26, 2009

PHP Trim Functions

Dear friends i would like to discuss the features of PHP trim functions.

1.trim

We all are familiar with trim. Normally trim is using for removing whitespce from starting or ending of a string, but do you know we can remove words or characters from starting and ending of a string using trim?

Try this Example

$str = "Hello Welcomes";
echo trim($str,'Hels');

2.ltrim

ltrim is using from stripping whitespaces or characters or word from begining of a string.

Try this Example

$str = "Hello Welcomes";
echo ltrim($str, "Hels");

3.rtrim

rtrim is using from stripping whitespaces or characters or word from begining of a string. chop function is a alias of rtrim.

Try this Example

$str = "Hello Welcomes";
echo rtrim($str,'ems');

Hope all you like this article.

Thursday, July 16, 2009

Creating a CAPTCHA with PHP

We all are familiar with CAPTCHA. In this article am sharing how we can create a CAPTCHA image using PHP. You can visit this link http://en.wikipedia.org/wiki/Captcha to know more details about captcha.

PHP having many Image Functions to create and modify images. Before we creating a CAPTCHA image we want to generate a random text. You can prefer number or text or combination of both as captcha text.

So we need possible characters. Here i have given combination of text and number as captcha text.

$PossibleChar = '123456789bcdfghjkmnpqrstvwxyz';
$Code = '';
$i = 0;
$Limit = 6;//Captcha Text Limit
while ($i < $Limit)
{
$Code .= substr($PossibleChar, mt_rand(0,
strlen($PossibleChar)-1), 1);
$i++;
}

Above code will generate radom text for captcha. mt_tand function will generate a better result between 0 and total length of Possible Characters and substr function will cut the $PossibleChar according to that.

Now we want to create an Captcha image. Following code give you an idea about how we can create an Captcha image.


$Height = 40;//Image Height
$Width = 150;//Image Width
$Font = 'monofont.ttf';//Font Path
$FontSize = $Height * 0.75;//Font size will be 75% of the image Height

//Create a new palette based image
$Image = @imagecreate($Width, $Height) or die('Impossible to create an
image');
//Setting the Background Color
$BackgroundColor = imagecolorallocate($Image, 255, 255, 255);
//Setting the Text Color
$TextColor = imagecolorallocate($Image, 20, 40, 100);
//Setting the Line Color
$LineColor = imagecolorallocate($Image, 100, 120, 180);


//Generate random dots in background
//imagefilledellipse function Draws an ellipse centered at the specified
//coordinate on the given image
for( $i=0; $i<2000; $i++ )
{
imagefilledellipse($Image, mt_rand(0,$Width),
mt_rand(0,$Height), 1, 1, $LineColor);
}

//Generate random lines in background
//imageline function draws a line between the two given points
for( $i=0; $i<300; $i++ )
{
imageline($Image, mt_rand(0,$Width), mt_rand(0,$Height),
mt_rand(0,$Width), mt_rand(0,$Height), $LineColor);
}

//Create textbox and add text
//imagettfbbox function calculates and returns the bounding box in
//pixels for a TrueType text.
$TextBox = imagettfbbox($FontSize, 0, $Font, $Code) or die('Error in
image textbox Creation');

//X and Y for image text
$x = ($Width - $TextBox[4])/2;
$y = ($Height - $TextBox[5])/2;

//Write text to the image
imagettftext($Image, $FontSize, 0, $x, $y, $TextColor, $Font , $Code) or
die('Error in image text Creation');

//Header specification for image
header('Content-Type: image/jpeg');
//Output image to browser
imagejpeg($Image);
//Frees any memory associated with image image.
imagedestroy($Image);

Hope you understand what i narrate.