Archive | PHP RSS feed for this section

“Auto Load” Method and Singleton Pattern helps to manage resource in appRain

12 Oct

It’s really easy to load a class in appRain. We don’t need to include the class but it attach on demand. “On demand” means when we create first instance of the class then system automatically include the class in the system.

But!

There are some common conversion to construct the class. Here is some guide line

1) The class name must be given with the combination of it’s directory path. Let say we have a class in:

development/plugin/paypal/standard.php

Then class name must be

Development_Plugin_Paypal_Standard

2. Class name must be in lower case.

Here each underscore ( _ ) is used as a directory separator so we must use it carefully.Once we have create the class the installation is done. It will be attached in the system when we will create it’s first instance. We can create the class object like

$object = new Development_Plugin_Paypal_Standard()

But appRain recommend in different way..

For plug-in it will be : $obj = App::Plugin(‘Paypal_Standard’);
For helper it will be : $obj = App::Helper(‘Paypal_Standard’);

But in some case if we need to create class object directly then we can use like
$obj = App::__Obj(‘Development_Plugin_Paypal_Standard’);

Because, in this way the system manage the resource in Singleton Pattern so that, It hepls not to over load resource any more.

😀

Advertisements

To encrypt PHP soruce code

5 Dec

Yesterday night I was looking for something to encrypt my PHP source code. Three years back I used one script to encrypt my PHP source code. Unfortunately I have lost the script.

Finally I got this service to get the job done.  They used same way that I did with the script that I lost. I am sharing with you… 🙂

Easy way to generate thumbnails with GD library on runtime

23 Jan

It is really a panic work with image when we need to make several thumbnails. We can do it directly from Photoshop or with GD library. Generally when we upload an image we make two copies. One is for thumbnail view and another for large view.

It would be an inefficient idea to make 4 or 5 copies of image. This technique is wasting of space as well as time. Let’s say if you have to change the size of the thumbs then either you have to do it manually or you have to say not possible.

What if we don’t generate any thumbnail?!! 😀

Actually it is very easy to write some line of code to resize your image runtime. To get ride of this panic situation. I just wrote a simple function. It receive file path and new size and return image object. And IMG tag can render it easily. So i can resize image in any size with it.

Lets we have a look in following information

<?php

$image_file_path = ‘img/my_image.jpg’; // File path of the image
$resize_to = ‘100’; //100 pixel

?>

Now we have to set an URL in image tag from where my server side langue pick the image file path and width. Then we have to generate the image again and return the image object. To send the image file path I like to make it base64_encode otherwise it will edit the image base URL. Now let’s see how I called the image ULR from image tag.

For CakePHP we can do it.

<?php

echo ‘<img ” src=”‘ . $html->url(‘/’) . ‘common/get_image/’ . base64_encode($image_file_path) . ‘/100/fix” />’;

?>

Here we have a controller named “Common” and from where we calling “get_image” function.

For general use. We can write is like this.

<?php

echo ‘<img ” src=”http://www.abc.com/resize_my_image.php? file=’ . base64_encode($image_file_path) . ‘&size=100& resize_flag=fix” />’;

?>

Now we have to execute the function. This function receive three parameter. First parameter is encode file path of the image. Second one is the new size of the image. If you sent third parameter in percentage or keep it blank then you have to set size like .5 for 50%, .25 or 25% or 1 for 100%. And if you keep third parameter “Fix” then you have to set size in pixel.

For cakePHP simply we have to place the function in “Common” controller.

<?php

class CommonController extends AppController {
var $name = ‘Common’;
var $uses = array(”);
var $layout =’blunk’;
var $pageTitle = ‘Process Image’;

// Resize image
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )
{
// Image resize code here
}
}

for simple implementation we can do following process

$image_path = $_GET[‘file’];
$size = $_GET[‘size’];
$resize_flag = $_GET[‘resize_flag’];

// Call the function from here
get_image($image_path, $size, $resize_flag )

// Declaration of the resize image function
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )
{
// Image resize code here
}

This is the final step of the process. New here is the source of the get_image function.

// Resize image
function get_image( $file = NULL, $size = NULL,$resize_flag = ‘Percentage’ )
{

//Set the layout to ajax
$this->layout=’ajax’;

// Decode the image path
$file = base64_decode($file);
// This sets it to a .jpg, but you can change this to png or gif
header(‘Content-type: image/jpeg’);

// Setting the resize parameters
list($width, $height) = getimagesize($file);

if( $resize_flag == ‘Percentage’)
{
$modwidth = $width * $size;
$modheight = $height * $size;
}
else
{
$modwidth = $size;
$modheight = ( $height * $size ) / $width;
}

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
}//function get_image()

?>

Once you have set total process then you need not to think about resize. Just change the size from image source. Then image will resize proportionally. I got a good advantage from this function. Hope you guys will enjoy it.

Run PHP and mySQL in IIS!!

13 Sep

I have been going through some challenges since I started analyzing Sugar CRM for my Siemens project. I better call them interesting challenge. After discovering this solution I feel I should share this with my readers. Well main challenge was to run PHP under IIS environment.

I was a little bit tensed about php and IIS server. But this is pretty easy process just to follow some steps…
This is simply some configuration process. I can tell you, I will just forget them once im done configuring 🙂

So let me tell you how I solved this shit…

Fist of all we need the php pack from eher http://www.php.net&#8230; Then extract the file and copy in location C:/php

I am using IIS 6.0 and PHP version 5
We will assume in this section that IIS is up and running,
Then follow the below tree…
Click Start
|_ Setting
|_ Control Panel
|_Administrative Tools
|_ Internet Service Manager

Select Default Web Site, right click and select property…

In Home Directory Tab, click Configuration
In the Application Configuration window select App Mappings and click Add Button

Enter the executable as C:\PHP\PHP.EXE (Or the location where you have installed PHP) and the extension as .php as shown in the above figure.

Now copy the php.ini-recommended in windows folder and rename it to php.ini. Edit it if you need necessary change.

For example: To test find out “;extension=php_curl.dll” And remove the semicolon. Then copy php_curl.dll from C:\wamp\php\ext and past it in system32 folder. Now curl lib will enabled with your php.

Configuration not completed yet. You have to include php.ini in our environment variable.
To make it easy right click on my computer properties. Now in advance tab click Environment variable. In system variable section click new or press (ALT + w )
“New system variable “will appear. Now set the following text
Variable name: PHPRC
Variable value: c:/WINNT/php.ini
Reboot the computer.

This is the configurations of IIS to run PHP Files with PHP interpreter.

Now open your favorite text editor and create a text file and type the following code and past in root directory.

And browse the URL. Hope you are getting the necessary information of php.

Now we need the mysql working with php. Let’s have a look there. 🙂

First download mySQL 5.0 and install it like other software. Usually the mySQL installation folder create in C:/ mySQL.

MySQL just installed. But IIS and PHP do not know about it. This is where I had to spend some good amount of time.

Open the MySQL Administrator program. Set server
host = localhost,
username=root
password=123456

Or whatever you set it to. Click on catalogs, if you see “mysql” then that is a main database.

Let’s go to introduce mySQL and PHP.

In the C:\PHP folder copy the file “libmysql.dll” to your C:\WINDOWS\SYSTEM32 folder. Make sure you don’t overwrite any existing file in this folder. If file exists, first rename it so you can go back.

Edit this file “php.ini” that (u kept it in windows directory during php configuration) using a text editor like notepad. Find the line where it says “extension_dir = *something*”. Uncomment this line by removing the preceding semi-colon. and set it to say: extension_dir=”c:\php\ext”

In the same file “php.ini” also un-comment out 2 other lines by removing semi-colon. Find the lines where it says: extension=php_mysql.dll and extension=php_mysqli.dll and uncomment both lines by removing the semi-colon.

Reboot the computer and write some code to access database.

I am thinking to write my next post on about installing SQL Server in IIS.
But till now I am getting an error CGI error

“CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:”

During execution php except validate HTML syntax. Any body have idea pls let know.

Interesting variable assigning in PHP

26 Aug

We commonly use the double $ sign to use the value of a variable as a variable name. For example

<?php$Jon = ‘Mrs. Merry’;
$name = ‘Jon’;
echo $$name ;
// We can easily use to get the output “Mrs. Merry”;

?>

Now let’s have a fun.

<?php$name = ‘Jams’;
$$name = ‘Jams Smith’;
echo $$name;
echo $Jams;

?>

What will be the out put. Easily we can evaluate it. Both $$name and $Jams will give same out put “Jams Smith”; Now yesterday I was reading the fundamentals of PHP. Lets see another example.

<? php$name = ‘Ja ms’;
$$name = ‘Jams Smith’;
echo $$name;
echo $Ja ms;

?>

Now $Ja ms will generate a parser error. Because $Ja ms doesn’t follow the convention of variable declaration.

But what about $$name. Is it no confusing…..
But reality is $$name will print ‘Jams Smith’.
PHP manage $$name itself. 🙂

Desktop programming with PHP!

13 Aug
php-gtk.gif GTK is an interesting library of php. This library used for desktop application.
Yap! No kidding here. Php is not only using for server side language. We can develop desktop based application also.Recently PHP-GTK2 is released for PHP5 with documentation. A strong forum also developed for developers.I am really exited about it.After installing GTK we can start an example.

<?php

//Step 1
if (!class_exists(‘gtk’)) {
die(“Please load the php-gtk2 module in your php.ini”);
}

//Step 2
$wnd = new GtkWindow();

//Step 3
$wnd->set_title(‘Hello world’);
$wnd->connect_simple(‘destroy’, array(‘gtk’, ‘main_quit’));//step 4
$lblHello = new GtkLabel(“Hello world!'”);
$wnd->add($lblHello);

//step 5
$wnd->show_all();
Gtk::main();

?>

This is a sample program for GTK.
Type it in a simple text editor like notepad and save as sample.phpw

It is good practice to check is GTK package available or not. So in first step we should check it.

In second step we make an object of window and in third step we attach the title of the window. In fourth step we add a level to show a message

And in final step we just say the console to display it.

This is the source code of the program now how you should run it.

Don’t be confuse that this is a desktop based application so don’t think about the we server. We will run it there.

To just test you can do a easy way. But before it I am admitting this is not good process. You can copy the source file in your PHP folder and open the dos console. And just go to the file location and run it like other java program( type: C:/wamp/php>php sample.phpw)

If you find the message
“Please load the php-gtk2 module in your php.ini”
it means you did not install GTK2 yet.

Otherwise a window will popup for you. This is amazing.

Swap value except third variable.

27 Jun

Idly I was passing my time. But it was interesting.

It is a simple problem but all try was failing each time. Finally I thought it is impossible and this universal.

Finally I could solve it partially…
Continue reading