Friday, November 6, 2009

Watermark in an Image with PHP

This is an example to write water mark in a image. Try this

$rim = 'image.JPG'; // URL TO IMAGE eg. image.jpg

$water = 'Hi Friends'; // WATERMAKR TEXT

$angle = '0'; // WATERMARK TEXT ANGLE

$ttf = 'times.ttf'; // WATERMARK TEXT FONT URL

$det = getimagesize($rim);
$mime = $det['mime'];

if ($mime == 'image/gif') {
$im = ImageCreateFromGIF($rim);
}else if ($mime == 'image/png') {
$im = ImageCreateFromPNG($rim);
}else if ($mime == 'image/jpeg') {
// echo "hi :".$mime;
$im = ImageCreateFromJpeg($rim);
}

$colorR = imagecolorallocatealpha ($im, 255, 255, 255, 50); // HEX COLOR CODE AND TRANSPARENCY

$count = strlen($water);
if($det[0] > $det[1]){
$size = $det[0]/$count;
}else{
$size = $det[1]/$count;
}

$bb = imagettfbbox($size, $angle, $ttf, $water);
$px = ($det[0]/2) - (($bb[2]-$bb[0])/2 - ($bb[2]-$bb[4])/2);
$py = ($det[1]/2) - (($bb[3]-$bb[1])/2 + ($bb[7]-$bb[1])/2);
imagettftext($im, $size, $angle, $px, $py, $colorR, $ttf, $water);

header("Content-type: image/jpeg");
imagejpeg($im,'',100);
imagedestroy($im);

Friday, October 23, 2009

PHP User Defined Functions Specialties

Each and every language has its on specialties. Here I listing the some useful features of PHP user defined functions.

Conditional Functions

We can define a function with in a condition. Function will only execute if condition satisfies.

E.g.:

$flag = true;

if($flag)
{
function insideCondition()
{
echo “
am inside“;
}
}

insideCondition();

if flag is true you will get following output

am inside

else

Fatal error: Call to undefined function insideCondition()

Function inside a Function

PHP allows you to declare a function inside a function. You can execute the inner function after calling the container function.

E.g.:

function parentFun()
{
echo "From Parent
";
function childFun()
{
echo "From Child
";
}
}

parentFun();

childFun();

Output will be

From Parent
From Child

If you call childFun() before parentFun(), you will get function undefined error.

Variable Function

PHP supports us to assign a function to a variable. After that we can use that variable as function, it allows us to pass function arguments also.

E.g.:

function newFun($str)
{
echo $str;
}

$varFun = ‘newFun’;

$var = "Hi Friends";

$varFun($var);

Wednesday, September 16, 2009

Array Sorting with PHP

We all are familiar with arrays. But PHP provides lot of array functions.
Those functions help us to reduce code length and save time. Here am
trying to point out array sorting functions.

sort

To sort an array
Syntax : sort($array,$flags)
sort functiom has four flags.They are :
SORT_REGULAR - compare items normally
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale
eg :
$arr = array( 35, 40, 20, 14, 36);
sort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[0] = 14
arr[1] = 20
arr[2] = 35
arr[3] = 36
arr[4] = 40

rsort

rsort function sorts an array in reverse order. This function is just opposite of sort function.

eg :
$arr = array( 35, 40, 20, 14, 36);
rsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[0] = 40
arr[1] = 36
arr[2] = 35
arr[3] = 20
arr[4] = 14

asort

asort function is similar to sort function. The only difference is it will not
change the array index.
eg :
$arr = array( 35, 40, 20, 14, 36);
asort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[3] = 14
arr[2] = 20
arr[0] = 35
arr[4] = 36
arr[1] = 40

arsort

arsort function is similar to rsort function. The only difference is it will not
change the array index.

eg :
$arr = array( 35, 40, 20, 14, 36);
arsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[1] = 40
arr[4] = 36
arr[0] = 35
arr[2] = 20
arr[3] = 14

ksort

ksort function will sort an array by key.

eg :
$arr = array( 35, 40, 20, 14, 36);
ksort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[0] = 35 arr[1] = 40 arr[2] = 20 arr[3] = 14 arr[4] = 36

krsort

krsort function will sort an array by key in reverse order.

eg :
$arr = array( 35, 40, 20, 14, 36);
krsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[4] = 36 arr[3] = 14 arr[2] = 20 arr[1] = 40 arr[0] = 35

Thursday, August 6, 2009

Sending HTML Content in an Email from PHP

PHP provides mail function to send mail. Syntax is:
bool mail ( string $to, string $subject, string $message [,
string $additional_headers [,
string $additional_parameters]] )

We can send mail n number of mail using this function. The great features of mail function is it support additional headers, that helps us to send the Sender’s details including server name and php version, HTML mail etc…

Here am listing a example, that give you a idea about sending HTML mail.

Recipient,You can send to multiple recipients by using comma
$to = 'rep@yahoo.co.in';

Subject
$subject = 'Mark List';

Message
$message = "Any Html Contents";


To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: sender ' . "\r\n";
$headers .= 'Cc: rep1@gmail.com' . "\r\n";
$headers .= 'Bcc: rep2@gmail.com' . "\r\n";

// Send Mail
mail($to, $subject, $message, $headers);

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.

Tuesday, June 23, 2009

Different Array Sorting Functions


We all are familiar with arrays. But PHP provides lot of array functions. Those functions help us to reduce code length and save time. Here I am trying to point out some of the useful array sorting functions.

sort

To sort an array

Syntax : sort($array,$flags)
sort functiom has four flags.They are :
SORT_REGULAR - compare items normally
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale

eg :

$arr = array( 35, 40, 20, 14, 36);
sort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}

You will get following Output

arr[0] = 14 arr[1] = 20 arr[2] = 35 arr[3] = 36 arr[4] = 40


rsort

rsort function sorts an array in reverse order. This function is just opposite of sort function.

eg :
$arr = array( 35, 40, 20, 14, 36);
rsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}
You will get following Output
arr[0] = 40 arr[1] = 36 arr[2] = 35 arr[3] = 20 arr[4] = 14


asort

asort function is similar to sort function. The only difference is it will not change the array index.

eg :

$arr = array( 35, 40, 20, 14, 36);
asort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}

You will get following Output

arr[3] = 14 arr[2] = 20 arr[0] = 35 arr[4] = 36 arr[1] = 40


arsort

arsort function is similar to rsort function. The only difference is it will not
change the array index.

eg :

$arr = array( 35, 40, 20, 14, 36);
arsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}

You will get following Output

arr[1] = 40 arr[4] = 36 arr[0] = 35 arr[2] = 20 arr[3] = 14


ksort

ksort function will sort an array by key.

eg :

$arr = array( 35, 40, 20, 14, 36);
ksort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}

You will get following Output

arr[0] = 35 arr[1] = 40 arr[2] = 20 arr[3] = 14 arr[4] = 36


krsort

krsort function will sort an array by key in reverse order.

eg :

$arr = array( 35, 40, 20, 14, 36);
krsort($arr);
foreach ($arr as $key => $val) {
echo "arr[" . $key . "] = " . $val . "\n";
}

You will get following Output

arr[4] = 36 arr[3] = 14 arr[2] = 20 arr[1] = 40 arr[0] = 35