Tuesday, May 19, 2009

Create Dynamic Images with Text Using PHP GD

First check weather php gd library is enabled.


<?PHP

echo '<pre>';

print_r(gd_info());

echo '</pre>';

?>


If you get a error or a blank page that means the gd is not enabled in your server.
Contact your hosting and ask for php gd to be enabled.

Try following coding, everything is commented.


<?PHP

//save the arial.ttf font file in the same folder
$font = 'arial.ttf';
$fontsize = 10;

$YourText = 'Hi This Work';
$Text_Box = imagettfbbox($fontsize, 0, $font, $YourText);

//create image width 250, height 100
$image = imagecreatetruecolor(250,100);

//0,0,0 is black
$bgcolor = imagecolorallocate($image, 0, 0, 0);
//255, 255, 255 is white
$fontcolor = imagecolorallocate($image, 255, 255, 255);

//create a rectangle with the back ground coloe
imagefilledrectangle($image, 0, 0, 250, 100, $bgcolor);

$x = 10;
$y = 20;
//draw text on image
imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $YourText);

//setting png headers
header('Content-type: image/png');
//create png image
imagepng($image);
//delete tempory files
imagedestroy($image);

?>



You can use this php file in a html file like this also


<img src="your_file_name.php" />

Sunday, May 17, 2009

phpMyAdmin mysql table overhead


If you are using phpmyadmin to manage mysql databases and tables you have probably seen a red figure for overhead in the space usage section in a table's detailed view.

This is a usual thing when you have lot of deletes and updates in your table fields. Think overhead as empty space and mysql want it to be defragmented. You can simply use the Optimize table link to get rid of the overhead.

Thursday, May 14, 2009

Create your own Konami code or custom code.

I have used Paul Irish code for my example.



<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>

jQuery(function($){

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e)
{
kkeys.push( e.keyCode );
if( kkeys.toString().indexOf( konami ) >= 0 )
{
$(document).unbind('keydown',arguments.callee);
//Any message or event you want goes here
alert('The Konami Code Is Activated');
}
});
});
</script>



By replacing unicode char values in this line you can define any key combination you like.
Example
var kkeys = [], konami = "38,40,75";
this means up,down,k

You can find any key code from this site http://www.javascriptkit.com/jsref/eventkeyboardmouse.shtml

Tuesday, May 12, 2009

Use Gmail to send mails in php

You can use Gmail SMTP to send emails remotely even in your localhost machine with WAMP.
First you must download phpmailer
After you extratced all zip files goto examples folder. In there you will find test_smtp_gmail_basic.php file. Just edit the code with your gmail username and password and you are ready to go.

Before executing the script goto php.ini file and remove the ; that is infront of extension=php_openssl.dll

Then restart your server.

Leave a comment if you need any help.

Monday, May 11, 2009

check only for numbers in JavaScript

This code will show you how to check only for numbers in JavaScript.


<script type="text/javascript">

var input = '112334';
var vali_regex = /^[0-9]+$/;

if(input.match(vali_regex))
{
alert('This is a number');
}
else
{
alert('Not a number');
}
</script>

Monday, May 4, 2009

How to search an array in php

You can use array_search() function to easily search values in an php array. If you didn't know about this function you should have probarbly used a foreach or for loop to find a matching value in your array.

array_search() will return the array key if found the given value. This function is case sensitive to strings.


<?php

$my_array = array('1', 'kuppiya', 'Tom', 'Hello');

$key = array_search('Tom', $my_array);

if($key === false)
{
echo 'Not found';
}
else
{
echo $my_array[$key];
}

?>