Creating a FaceBook like profile badge

You know what a facebook profile badge is, right? (if no, just look here on sidebar my facebook profile badge). Well creating a profile badge like that is really easy. Here i created a small script to show the basic of how this thing works. So, here is the profile badge class

<?php
class profile_badge{
	public static function calc_image_height($strs,$limit){
		$total_line=0;

		foreach($strs as $str){
			$len=strlen($str);
			$div=(int)($len/$limit);
			$mod=$len%$limit;
			if($mod>0){
				$total_line=$total_line+$div+1;
			}else{
				$total_line=$total_line+$div;
			}
		}

	return ($total_line*15)+40;
	}

	public static function get_badge($text_name,$text_email){
	$CHARLIMIT=19;
	$label_name='Name:';
	$label_email='Email:';

	$logo="http://127.0.0.1/logo_fade.jpg";
	$extra=profile_badge::calc_image_height(array($text_name,$text_email),$CHARLIMIT);

	list($width,$height)=getimagesize($logo);
	$im=imagecreatefromjpeg($logo);

	$bg=imagecreatetruecolor($width,$height+$extra);

	$black=imagecolorallocate($bg,0,0,0);
	$white=imagecolorallocate($bg,255,255,255);
	$gray=imagecolorallocate($bg,124,124,124);
	$bdr=imagecolorallocate($bg,218,218,218);

	imagecopymerge ($bg,$im,0,0,0,0,$width,$height,100);

	imagefilledrectangle ($bg,0,$height,$width,$height+$extra,$white);
	imagerectangle ($bg,0,0,$width-1,$height+$extra-1,$bdr);

	$font='tahoma.ttf';
	$pad=15;
	$text_pos=$height+$pad;
	imagettftext($bg, 10,0,5, $text_pos, $black, $font, $label_name);
	$text_pos=$text_pos+$pad;

	if(strlen($text_name)<=$CHARLIMIT){
		imagettftext($bg, 10,0,5, $text_pos, $gray, $font, $text_name);
		$text_pos=$text_pos+$pad;
	}else{
		$name_array=str_split($text_name,$CHARLIMIT);
		foreach($name_array as $text_name){
			imagettftext($bg, 10,0,5, $text_pos, $gray, $font, $text_name);
			$text_pos=$text_pos+$pad;
		}
	}

	imagettftext($bg, 10,0,5, $text_pos, $black, $font, $label_email);
	$text_pos=$text_pos+$pad;

	if(strlen($text_email)<=$CHARLIMIT){
		imagettftext($bg, 10,0,5, $text_pos, $gray, $font, $text_email);
		$text_pos=$text_pos+$pad;
	}else{
		$email_array=str_split($text_email,$CHARLIMIT);
		foreach($email_array as $text_email){
			imagettftext($bg, 10,0,5, $text_pos, $gray, $font, $text_email);
			$text_pos=$text_pos+$pad;
		}
	}

	return $bg;
	}
}

logo_fade.jpg

Notice, at line 25, yes that’s my site’s logo image url. Also notice at line 43, yes this my font file. you can use your own font of choice in there. Ok now include that class and call its get_badge function, this way,

$name='Mahabubul Hasan';
$email='uzzal.me@gmail.com';

$bg=profile_badge::get_badge($name,$email);

header("Content-type: image/png");
imagepng($bg);
imagedestroy($bg);

and it generates a output like this:

isn’t it cool?😉

About uzzal

Software developer
This entry was posted in php, web design and tagged , . Bookmark the permalink.

2 Responses to Creating a FaceBook like profile badge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s