Syed Abdul Baqi

Archive for the ‘php’ Category

Hello Everybody,

Recently I have written new Image Thumb Generator class for PHP for one of my project. The class is having very small footprint and easy to use, just wanna share with all of you. It is available for download here. This class uses GD Library for generating thumb images, so make sure you have GD Library Extension enabled before use it.

Usage:

include 'image.class.php';
$image = new Imagethumb(<source file path>, <Maintain Aspect Ratio(TRUE|FALSE)>[DEFAULT: FALSE]);   
$image->getThumb('thumb file path to save', (int)<width>, (int)<height>);

Example:

include 'image.class.php';
$image = new Imagethumb('Blue hills.jpg', true);   
$image->getThumb('thumb.jpg', 125, 125);

Comments are welcomed….

Advertisements

Hello Everybody,

Today I will show you a simple approach to achieve Multilingual functionality in the website powered by PHP. This approach is so simple that I believe we can use it in small and medium scale projects without any hesitations :).

Before going into more details, if you want to check the code you can download it from here (rename extension “doc” to “zip”).

My approach towards this requirement is simple. First create different Language Files (example: english.lng, dutch.lng, etc) which will hold language vocabulary defined in a constants. After this we will have to replace the text (in our PHP files) with constant defined in file, where ever neccessary. Now depending upon the language selected load the particular language file. Best way to this approach is by passing language name using query string like,

http://localhost/multilingual/index.php?language=en

So based upon the query string parameter language file will be loaded and hence the text in the website will change as per the language file. To make things more better we can also make use of Apache Mod Rewrite to make URL more user friendly.

.htaccess

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteRule    ^(en|du|EN|DU)/(.*) $2?language=$1 [NC,L,QSA]
 RewriteRule    ^(en|du|EN|DU)$ http://localhost/multilingual/$1/index.php [R=301,L]
</IfModule>

This code will convert the URL

http://localhost/multilingual/index.php?language=en

to

http://localhost/multilingual/en/index.php

You can download the complete code from here (rename extension “doc” to “zip”). Let me know how I can make this code more better and robust.

Comments are welcomed….

Hello Everybody,

Recently I have coded one dynamic graph-code for PHP using GD Library. Just want to share the code with all of you guys. Feel free to make any modifications and use it in your projects. You can find the code below:

graphcode.php

<?php

$data1 = $_GET['tot'];
$data2 = $_GET['correct'];

$im = imagecreate(100,20); // width , height px

$lightblue = imagecolorallocate($im,226,224,224); 

if($data2 > 0 && $data2 <= 10)
{
 $darkblue = imagecolorallocate($im,255,0,1);
}
elseif($data2 > 10 && $data2 <= 20)
{
 $darkblue = imagecolorallocate($im,243,80,1);
}
elseif($data2 > 20 && $data2 <= 30)
{
 $darkblue = imagecolorallocate($im,244,116,1);
}
elseif($data2 > 30 && $data2 <= 40)
{
 $darkblue = imagecolorallocate($im,243,147,1);
}
elseif($data2 > 40 && $data2 <= 50)
{
 $darkblue = imagecolorallocate($im,243,189,1);
}
elseif($data2 > 50 && $data2 <= 60)
{
 $darkblue = imagecolorallocate($im,243,225,1);
}
elseif($data2 > 60 && $data2 <= 70)
{
 $darkblue = imagecolorallocate($im,214,243,1);
}
elseif($data2 > 70 && $data2 <= 80)
{
 $darkblue = imagecolorallocate($im,189,243,1);
}
elseif($data2 > 80 && $data2 <= 90)
{
 $darkblue = imagecolorallocate($im,140,243,1);
}
elseif($data2 > 90)
{
 $darkblue = imagecolorallocate($im,92,243,1);
}

$x = 0;         // bar x1 position
$y = 20;         // bar $y1 position
$x_width = 0;     // width of bars
$y_ht = 20;     // height of bars, will be calculated later

for ($i=1;$i<$data1;$i++)
{
 $x_width = $i * 1;
 imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$lightblue);
}

$y = 20;
$y_ht = 20;

for ($i=1;$i<$data2;$i++)
{
 $x_width = $i * 1;
 imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$darkblue);
}

imagejpeg($im);

?>

You can use the above file as shown below:

<img src=graphcode.php?tot=100&correct=75 />

You will have to pass 2 query string variables to file as shown above. The first variable (tot) represents total percentage, and second variable (correct) represents correct percentage. Also make sure you have GD library enabled in PHP.ini

Let me know, how I can make the script more flexible.

Comments are welcomed…