Codeigniter Image Manupulation Example
7th Oct 2011 at 9:31 AM | Posted in PHP | 4 CommentsCreate a folder “uploaded” where your “application” folder is. (Not inside “application” folder”). Create a sub folder “thumbs” inside “uploaded”. Put a font in system/fonts/arial.ttf (copy the font from your windows).
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Imagetest extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
}
function index() {
$path = realpath(APPPATH . '../uploaded');
$file = "asd.jpg";
$path = str_replace('\\','/',$path);
$config = array(
'source_image' => $path.'/'.$file,
'new_image' => $path.'/thumbs/'.$file,
'maintain_ration' => true,
'width' => 150,
'height' => 100,
'x_axis' => '100',
'y_axis' => '40',
'rotation_angle' => '180',
'wm_text' => 'Test asdfsda sa dsa dsa dsa a',
'wm_type' => 'text',
'wm_font_path' => 'system/fonts/arial.ttf',
'wm_font_size' => '30',
'wm_font_color' => 'FF0000',
'wm_vrt_alignment' => 'bottom',
'wm_hor_alignment' => 'center',
'wm_padding' => '10'
);
print "<pre>";
print_r($config);
$this->load->library('image_lib', $config);
//$this->image_lib->crop();
//$this->image_lib->clear();
$this->image_lib->resize();
/*$this->image_lib->rotate();
$this->image_lib->clear();
if(!$this->image_lib->watermark()) {
echo $this->image_lib->display_errors();
} */
$this->load->view('imagetest');
}
}
PHP curl. Simple curl script
22nd Mar 2011 at 10:36 AM | Posted in PHP | Leave a commentUsing curl we can get the output content of a given url.
$URL = "http://google.com"; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, $URL); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl); curl_close ($curl); echo $result;
PHP preg_match_all to remove a part of a text.
22nd Mar 2011 at 10:28 AM | Posted in PHP | Leave a comment
<?php
echo $text = "This test [CONFIRMLINK] is working fine [/CONFIRMLINK] for preg_match_all";
preg_match_all('|\[CONFIRMLINK\](.*)\[\/CONFIRMLINK\]|', $text, $text1);
$text1_val = $text1[1][0];
$text = str_replace($text1_val,'',$text);
$text = str_replace("[CONFIRMLINK]",'',$text);
$text = str_replace("[/CONFIRMLINK]",'',$text);
echo "<br/>";
echo $text;
?>
PHP Class Example
6th Mar 2010 at 11:10 AM | Posted in PHP | Leave a commentCreate a simple class.
<?php
class validateClass
{
public function validatePass($pass,$pass1)
{
if($pass=='' || $pass1=='')
return("Enter in Both Pasword Fields.<br/>");
else
if($pass!=$pass1)
return "Two Passwords Are Not Same.<br/>";
}//End function
}//End class
$validate = new validateClass; //creating object of this class
$validate->validateName("abc","abc"); //calling the funtion
/* If passwords(the two parameter) are same it returns true else return false
*/
?>
PHP URL rewrite-htacces
30th Jan 2010 at 1:01 PM | Posted in PHP, URL Rewriting | 4 CommentsBy rewriting url we can change the original url. We can hide the extension of page. It is also named as SEO friendly URL.
For rewrite the URL you must have the mod_rewrite module loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?
1. Type <?php phpinfo(); ?> in a php file and save it and run that file in the server. Run tis page.
2. If mod_rewrite is found under the “Loaded Modules” section then this module is already loaded as you see in the picture above, otherwise you need to go to the next step for enabling mod_rewrite module.
If not active then active it:
1. Find the “httpd.conf” file under the “conf” folder inside the apache’s installation folder.
2. Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.
3. Remove the “#” at the starting of the line, “#” rpresents that line is commented.
4. Now restart the apache server. You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.
Now a simple script:
1. Create a folder “url_rewrite”. Inside this we will create our files.
2. Create a file test.php:
<html>
<body>
This is test webpage
</body>
</html>
3. Create a .htaccess file. If you can’t create make a file create_htaccess.php.
<?php
$url=$_SERVER['REQUEST_URI'];
$url=explode(“/”,$url);
$final_url=”;
for($i=0;$i<(count($url)-1);$i++)
{
$final_url.=$url[$i].”/”;
}if(!file_exists(“.htaccess”))
{
if($filename=fopen(“.htaccess”,”w”))
{
echo “.htaccess file created.<br/>Path: “.$final_url;
fwrite($filename,”Options +FollowSymLinks
RewriteEngine on”);
}
fclose($filename);
}
else
echo “.htaccess file already exists in: “.$final_url;
?>
4. In the .htaccess file write code below:
Options +FollowSymLinks
RewriteEngine on
#give your base url path
RewriteBase /sourav/url_rewrite/
RewriteRule ^(.*)\.htm$ $1.php [nc]
[ Explain: In this we rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.]
5. Now run the test.php file in browser or
run the test.html file in browser the result is same that is opening the test.php file.
Here we overwriting the extension .php by .html.
Now litle complex script:
1.Create a file “common_function.php” your url_rewrite folder.
<?php
function rewrite($val,$id)
{
if($val==”products”)
return “product-”.$id.”.html”;
if($val==”product_details”)
return “product_details-”.$id.”.html”;
}
?>
2. Create another file “products.php”.
<?php
echo “This is products.php?id=”.$_REQUEST['id'];
?>
3. Create another file “product_details.php”.
<?php
echo “This is product_details.php?product_id=”.$_REQUEST['id'];
?>
4. Include the “common_function.php” file in your test.php page.
<?php
include_once(“common_function.php”);
?>
This is test webpage<br/>
go to <a href=”<?php echo rewrite(“products”,”5″); ?>”>products.php?id=5</a>
go to <a href=”<?php echo rewrite(“product_details”,”3″); ?>”>product_details.php?product_id=3</a>
5. Now change your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
#give your base url path
RewriteBase /sourav/url_rewrite/
RewriteRule ^(.*)\.htm$ $1.php [nc]
#This is for product page
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1
#This is for product_details page
RewriteRule ^product_details-([0-9]+)\.html$ product_details.php?id=$1
6. Now run test.php file or test.html file and click on the links on this page.
see the links are changed.
2. If it is found under the “Loaded Modules” section then this module is already loaded as you see in the picture below, otherwise you need to go to the next step for enabling mod_rewrite module.
Introduction
11th Jan 2010 at 12:27 PM | Posted in PHP | Leave a commentPHP – Personal Home Pages
Php: Hypertext Preprocessor.
* PHP is a server-side scripting language, like ASP
* PHP scripts are executed on the server
* PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
* PHP is an open source software
* PHP is free to download and us. You can download it from: http://www.php.net
* Download MySQL for free here:http://www.mysql.com/downloads/index.html
* Download Apache for free here: http://httpd.apache.org/download.cgi
* Or you can install XAMPP. It is a very easy to install Apache Distribution for Linux, Solaris, Windows and Mac OS X. The package includes the Apache web server, MySQL, PHP, Perl, a FTP server and phpMyAdmin. Download XAMPP (for Linux, Windows, Mac Os X & Solaris) from here:http://www.apachefriends.org/en/xampp.html
http://sourceforge.net/projects/xampp/
It is easy to install. Installation guide is given here (http://www.apachefriends.org/en/xampp.html) for different Operating System.
XAMPP package is better to download & install.
* PHP files can contain text, HTML tags and scripts
* PHP files are returned to the browser as plain HTML
* PHP files have a file extension of “.php”, “.php3″, or “.phtml”
MySQL:
* MySQL is a database server
* MySQL is ideal for both small and large applications
* MySQL supports standard SQL
* MySQL compiles on a number of platforms
* MySQL is free to download and use. It is included in XAMPP package.
* PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform)
PHP because:
* PHP runs on different platforms (Windows, Linux, Unix, etc.)
* PHP is compatible with almost all servers used today (Apache, IIS, etc.)
* PHP is FREE to download from the official PHP resource: www.php.net
* PHP is easy to learn and runs efficiently on the server side
Start Learning PHP
11th Jan 2010 at 12:26 PM | Posted in PHP | Leave a commentPHP documentation is available here: http://www.php.net/download-docs.php. Get help from http://www.w3schools.com/php
PHP Syntax
<?php
// code
?>
A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.
On servers with shorthand support enabled you can start a scripting block with .
For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
1st PHP program-
<html>
<body>
<?php
echo “Hello World”;
?>
</body>
</html>
PHP Comments-
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block
<?php
// php statement terminates with a semicolon(;).
/*We can use
echo or print
to display the output*/
echo “Hello World”;
print “Hello World”;
?>
PHP Variables:
All variables in PHP start with a $ sign symbol.
Syntax: $a=”Hello World!”;
$a=16;
PHP is a Loosely Typed Language
In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.
Variable Naming Rule:
* A variable name must start with a letter or an underscore “_”
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
The Concatenation Operator:
PHP has only one Concatenation operator. That is dot(.).
<?php
$txt1=”Hello World!”;
$txt2=”What a nice day!”;
echo $txt1 . ” ” . $txt2;
?>
Output will be: Hello World! What a nice day!
phpinfo:
phpinfo
11th Jan 2010 at 12:24 PM | Posted in PHP | Leave a commentThis outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License.
Because every system is setup differently, phpinfo() is commonly used to check configuration settings and for available predefined variables on a given system.
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

