rss
twitter
  •  

PHP-GD: Create Image from Text

| Posted in Programming |

0

Some people want to hide their text into image for any reason. As example, to avoid spambot steal their email address, or to generate dynamic images content.

Its simply done with PHP and GD Library. See this simple function:

function imgfromstring($string){
	$font  = 5;
	//dynamic size of image. to fit your text.
	$width  = ImageFontWidth($font) * strlen($string);
	$height = ImageFontHeight($font);

	//start creating the image
	$gbr = imagecreate ($width,$height);

	//set background to white
	$bgcolor = imagecolorallocate($gbr, 255, 255, 255);
	//set color for text
	$txtcolor = imagecolorallocate ($gbr, 0, 0, 0);

	//generate the image
	imagestring($gbr, $font, 0, 0, $string, $txtcolor);
	header("Content-type: image/jpeg");
	return imagejpeg($gbr);
}

//usage:
$txt = "u...@email.com";
echo imgfromstring($txt);

example output: u...@sample.com" border="0" alt="" />

Another sample is using image as the background. Its useful when you need to create waterwark to protect your images.
Sample code:

function toImgWithBG($str){
	$bg = "background.jpg";
	$font = 5; //Font size

	$gbr =imagecreatefromjpeg($bg);

	//Calculate string width and height
	$pos_x  = imagesx($gbr) - (ImageFontWidth($font) * strlen($str));
	$pos_y = imagesy($gbr) - ImageFontHeight($font);

	//set text color
	$teks = imagecolorallocate($gbr, 255, 0, 0);

	//generate the image, and put on the right bottom
	imagestring($gbr, 5, $pos_x, $pos_y, $str, $teks);

	header("Content-type: image/jpeg");
	return imagejpeg($gbr);
}

//Usage:
echo toImgWithBG("copyright: http://www.akemapa.com/");

example output:

You can download source code of this example here:
download here download this file

VN:F [1.9.3_1094]
Rating: 5.5/10 (2 votes cast)
VN:F [1.9.3_1094]
Rating: +2 (from 2 votes)

Restore Ubuntu Boot Loader

| Posted in Programming, Tech-News, ubuntu |

1

To repair your Ubuntu GRUB, you need Ubuntu LiveCD, boot from this CD, and open a terminal. Be a root, by typing “sudo su” and enter your password.

Find the partition which your Ubuntu system is installed.

# fdisk -l

On my machine, my Ubuntu partition is on /dev/sda3, so i’ll use this for example.

Create a mountpoint for this:

# mkdir /media/rootgrub
# mount /dev/sda3 /media/rootgrub

Check if your partition is the correct partition

# ls /media/rootgrub

if the output is not like this, then you have the wrong partition

bin dev home lib mnt root srv usr boot etc initrd lib64 opt sbin sys
var cdrom initrd.img media proc selinux tmp vmlinuz

unmount the partition if have the wrong one, then mount the others. To make sure you have the correct partition, run ls /media/root/boot, which should output something like this :

config-2.6.18-3-686   initrd.img-2.6.18-3-686.bak   System.map-2.6.18-3-686
grub lost+found      vmlinuz-2.6.18-3-686       initrd.img-2.6.18-3-686
memtest86+.bin

Now when everything is ok, install the Grub:

# sudo grub-install –root-directory=/media/rootgrub /dev/sda

If you have some warning, try this:

# sudo grub-install –root-directory=/media/rootgrub /dev/sda –recheck

Change /dev/sda to other partition you want to install Grub on. If all goes fine, you will see the output like this:

Installation finished. No error reported. This is the contents of the device map /boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install’.

(hd0) /dev/sda

Restart to your harddrive (not to LiveCD) and see, your GRUB should be AUTOMAGICALLY appear!

VN:F [1.9.3_1094]
Rating: 9.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Reset Mysql Root Password

| Posted in Programming, Tech-News, ubuntu |

0

Terrible, just setup a new server, then i forgot root password for mysql database. So i need to reset the root password.

This is the way i do:
1. Login to our server machine as root (system administrator). Yes! we must have root access to our machine.

2. Shutdown mysql server (if already running). Make sure no other mysql daemon running.

$ /etc/init.d/mysql stop

3. Create a text file and place the following statements in it. Replace the password with the password that you want to use.

UPDATE mysql.user SET Password=PASSWORD(‘YourNewPasswordHere’) WHERE User=’root’;
FLUSH PRIVILEGES;

The UPDATE and FLUSH statements each must be written on a single line. And make sure we have statement “where user=’root’“, to make sure, only our root password is updated.

4. Save the text file into our home directory, or somewhere else. Example, i saved this as: “/home/vandai/mysql-reset

5. Start the MySQL server with the special –init-file option:

mysqld_safe –init-file=/home/vandai/mysql-reset &

This will execute the contents of the file named by the –init-file option at startup, changing each root account password.

6. Don’t forget to delete mysql-reset file immediately!!

Now we will be able to login to MySql database as root with our new password.

Never forget the password again!!

VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

PHP-MySql: Select previous row and next row from current ID

| Posted in Programming |

30

During the dark age, the simplest way to get previous row from current ID was using this query:

  1. SELECT field1
  2. FROM tablename
  3. WHERE id = ($currentId - 1)

and to get next row:

  1. SELECT field1
  2. FROM tablename
  3. WHERE id = ($currentId + 1)

But there’s a problem. Like common data, we need to delete some rows. For example, the current ID is 14 ($currentid = 14) , and i deleted row id number 13, when i run the query to get the previous row, the result will empty. The sql cannot find ID number 13 ($currentid – 1).

How to fix it?
Again, the first simplest way i got is to loop the ID. When it cannot find the first query, it calculate again by looping it with PHP.

//the bad way..
  1. < ?
  2. $ok = false;
  3. $prev = $currentid - 1;
  4. while($ok == false){
  5.    $sql = "select * from tablename where id = $prev ";
  6.    $data = $db->query($sql);
  7.    if(empty($data)){
  8.       $prev = $prev-1;
  9.    }else{
  10.       $ok = true;
  11.    }
  12. }
  13. ?>

This is really bad idea. It will consume more resources by continually query the database. What happen when you deleted 10 rows from current ID?
There will be 10 looping with 10 query to database. How about deleted 100 rows?? You must be insane doing this.

VN:F [1.9.3_1094]
Rating: 9.3/10 (6 votes cast)
VN:F [1.9.3_1094]
Rating: +3 (from 3 votes)

PHP-GD: Resize Transparent Image PNG & GIF

| Posted in Programming |

48

By default, you will get black background if you resize a transparent image. To fix it, you need set alpha channel imagecolorallocatealpha to 127.
With imagecolorallocatealpha, it will allocate a color for an image.

Usage:
int imagecolorallocatealpha ( resource image, int red, int green, int blue, int alpha)

From PHP manual:
imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha which may have a value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
Returns FALSE if the allocation failed.

Before using it, you must set to false the blending mode for an image and set true the flag to save full alpha channel information.

Example:

  1. < ?
  2. $newImg = imagecreatetruecolor($nWidth, $nHeight);
  3. imagealphablending($newImg, false);
  4. imagesavealpha($newImg,true);
  5. $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  6. imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  7. imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
  8. ?>
VN:F [1.9.3_1094]
Rating: 8.1/10 (8 votes cast)
VN:F [1.9.3_1094]
Rating: +2 (from 2 votes)