March 29, 2007

Run Time Image Thumbnail

This code can be used to create Dynamic Image thumbnails on the fly
i.e. You need not to bother about the actual image size while uploading. Just show the desired scaled image anywhere on your website. Same image may need to display in different size on different web-page.


/********** (thumb.php) ****************/
$img=$_GET['img']; $w=$_GET['w']; $h=$_GET['h'];
if(!defined('DIR_CACHE'))
define('DIR_CACHE', './image_cache/');

if (!Is_Dir(DIR_CACHE))
mkdir(DIR_CACHE, 0777);

#IMAGE RESIZE AND SAVE TO FIT IN $new_width x $new_height
if (file_exists($img))
{
$thumb=strtolower(preg_replace('/\W/is', "_", "$img $w $h"));
$changed=0;

if(!is_file($img))
$img = "images/product-image.jpg";

if (file_exists($img) && file_exists(DIR_CACHE.$thumb))
{
$mtime1=filemtime(DIR_CACHE.$thumb);
$mtime2=filemtime($img);
if ($mtime2 > $mtime1)
$changed=1;
}
elseif (!file_exists(DIR_CACHE.$thumb))
$changed=1;

if ($changed)
{
$filename=$img;
$new_width=(int)@$w;
$new_height=(int)@$h;
$lst=GetImageSize($filename);
$image_width=$lst[0];
$image_height=$lst[1];
$image_format=$lst[2];

if ($image_format==1)
$old_image=imagecreatefromgif($filename);
elseif ($image_format==2)
$old_image=imagecreatefromjpeg($filename);
elseif ($image_format==3)
$old_image=imagecreatefrompng($filename);
else
exit;

if (($new_width!=0) && ($new_width < $image_width))
{
$image_height=(int)($image_height*($new_width/$image_width));
$image_width=$new_width;
}

if (($new_height!=0) && ($new_height < $image_height))
{
$image_width=(int)($image_width*($new_height/$image_height));
$image_height=$new_height;
}

$new_image=ImageCreateTrueColor($image_width, $image_height);
$white = ImageCopyResampled($new_image, $old_image, 0, 0, 0, 0, $image_width, $image_height, imageSX($old_image), imageSY($old_image));
#ImageFill($new_image, 0, 0, $white);
imageJpeg($new_image, DIR_CACHE.$thumb);
}

header("Content-type:image/jpeg");
readfile(DIR_CACHE.$thumb);
}
/********** CODE END ****************/

Here is the example use
< img src="thumb.php?img=image1.jpg?w=150&h=100" >

Tricky & nice :
If a thumbnail of a particular size of an image is created, script will not create the dynamic thumbnail again. It will pick that thumb from image_cache folder, if already exists.

note: "image_cache" folder requires 777 permission

March 26, 2007

Reading site content using CURL

For reading content of a website in PHP, there are lot of different ways & functions available like:
$site_content=file($url); #OR
$site_content=file_get_contents($url) ;

But these functions does not work in some server environments where "allow_url_fopen" is off in php.ini & you don't have permission to change this.
You can use the following alternative (using CURL) in that case.

$url="http://www.bestenough.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_close ($ch);
echo($store);

March 15, 2007

Website Shifting (All files & DataBase)

Here is the method for Shifting your website & database from one server to another with simple steps. This works only on Linux server.

Steps to follow on Existing(Source) Server:
1. Move in the root folder of project i.e. change the directory
cd /path/to/dir

2. Compress the website(project) folder
tar -cvf xxxx.tar * (Supposing 'xxxx' is the name of project)

3. Export the Database in a tar file
mysqldump dbName -u dbUserName -p > dbFileName.tar
Password: myPassword

example:
mysqldump xxxx -u root -p > xxxx_DB_Backup.tar
Password: password

4. Copy the tar files to New server, where you want to shift (move) the website
scp xxxx.tar RemoteUserName@RemoteIP:/path/to/dir/xxxx.tar
Password: (password for that user of Remote IP)

example:
scp xxxx.tar root@127.0.0.1:/home/xyz/public_html/xxxx.tar
Password: myRootPassword

scp xxxx_DB_Backup.tar root@127.0.0.1:/home/xyz/public_html/xxxx_DB_Backup.tar
Password: myRootPassword

Steps to follow onNew(Target) Server:
1. Move in the root folder of project, where you have copied the compressed file
cd /path/to/dir

2. UnTar the tar file
tar -xvf xxxx.tar

3. Import the Database
mysql myDbName -u
dbUserName -p < dbFileName.tar
Password: myPassword

March 14, 2007

Full Backup of MySQL DataBase

Here are some methods for FULL backup of MySQL Databases. This way all the constraints, stored procedures, triggers etc will be copied for backup and/or replicating the database.

Depending on your access to server, you can choose the different options.
e.g. you will not be able to copy the files from a shared hosting server (Method 3)

METHOD 1:
shell> mysqldump --tab=/path/to/some/dir --opt --full

METHOD 2:
shell> mysqlhotcopy database /path/to/some/dir

METHOD 3:
simply copy all table files (`*.frm', `*.MYD', and `*.MYI' files)

METHOD 4:
You can write your own script which you can use to backup your database with various options. An example script can be downloaded from here

Please comment to this post to suggest more methods which can be added here.
Thanks