Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, September 30, 2009

How Can I Avoid "Page Has Expired" Warnings


Most of the time many developer may face in this problem in may times. Might be they have already solved or not. Here i m pointing why is it happing and how to solve it.

The warning is a result of the user utilizing the browser's history mechanism to access a previously viewed page, usually by clicking the back button or refreshing. One cause of the warning is that the page has expired from the browser's cache.

PHP, by default, does not forbid caching. When you request a simple PHP script, no caching headers are returned. To solve this you can use two ways.

1) Edit php.ini file.

Set session.cache_limiter to PRIVATE by default it comes with nocache.
 session.cache_limiter = private
 

2) Edit through you PHP Code
      ini_set('session.cache_limiter', 'private');
 


Wednesday, July 15, 2009

Image Compression

Nowadays it is common to work with images in programming language. Here I m putting some image compression function which can use in PHP. This is comes to common when developer working with image uploading and stuffz. Bcoz due to the storage matter we have to reduce images capacities to manage it.

It’s better to execute image compression PHP script in uploading page than ask user to do it.

Before using this script you have to enable GD library in your web server. And it’s better to enable EXIF library too when you are going to validate the image header files.

1 ) Script automatically compress the image base on the given dimension.
2 ) Automatically calculate the landscape image or portrait image.


function imageCompression($imgfile="",$thumbsize=0,$savePath=NULL) {

if($savePath==NULL) {
          header('Content-type: image/jpeg');
     }
 
     list($width,$height)=getimagesize($imgfile);
     $imgratio=$width/$height; 

     if($imgratio>1) {
          $newwidth = $thumbsize;
          $newheight = $thumbsize/$imgratio;
     } else {
          $newheight = $thumbsize;       
          $newwidth = $thumbsize*$imgratio;
     }
   
 // Making a new true color image
    $thumb=imagecreatetruecolor($newwidth,$newheight); 

 // Now it will create a new image from the source
     $source=imagecreatefromjpeg($imgfile); 

 // Copy and resize the image
 imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);  
     imagejpeg($thumb,$savePath,100);

    imagedestroy($thumb);
   
}



This imageCompression() you have to pass 3 parameter to execute.
// 1 - Source of your image file.
// 2 - Output image dimension.
// 3 - Destination path of the image.



imageCompression($uploadfile,250,$uploadfile);


Thursday, July 09, 2009

Find and Replace Missing Files

With using this PHP Class you can be able to check the count of files in 2 locations and find the missing files in destination location compare with source location.

1) Identify the missing files.
2) Count the missing files from source location and update it to destination.
3) If it is exist compare the file sizes and update un-match files.
4) All the details are saved in text file.



// | File         : matchdir.php
// | Created by   : Lahiru Manchanayake
// | Created Date : 20/06/2009
// | Modified     :

class matchDir {

public $pathDir1 ;
public $pathDir2 ;
public $f2 ;
public $file2 ;
public $missingCounter = 0 ;
public $counter = 0 ;

function matchDir($pathDir1 , $pathDir2)
{
$this -> pathDir1 = $pathDir1 ;
$this -> pathDir2 = $pathDir2 ;
$this -> f2 = opendir($this -> pathDir2);
}

function match()
{
while($this -> file2 = readdir($this -> f2))
{
if(is_file($this -> pathDir2.$this -> file2))
{
if (!file_exists($this -> pathDir1.$this -> file2))
{
 echo $this -> file2.'Not Exists';

 $f3 = fopen("lahiru.txt",'a+') ; 
 fwrite($f3,$this -> file2); 
 $this -> missingCounter++; 
 fclose($f3);

  if (copy($this -> pathDir2.$this -> file2 , $this -> pathDir1.$this -> file2))
  {
   echo "Copied";
  }else{
   echo "Faile to copy";
  } 
} else{
 echo $this -> file2.' Exists ';

  if (filesize($this -> pathDir2.$this -> file2) > filesize($this -> pathDir1.$this -> file2))
  {
   copy($this -> pathDir2.$this -> file2 , $this -> pathDir1.$this -> file2);
   echo "Updated";
  }else{
   echo "Faile to update";
  }
}
}
$this -> counter++ ;
}
closedir($this -> f2);
echo $this -> counter .'  Total Files'  ;
echo $this -> missingCounter .'  Missing Total Files'  ;
}
}

// distination
$pathDir1 = "folder2/";

// source
$pathDir2 = "folder1/";

$obj =new matchDir($pathDir1 , $pathDir2);
$obj -> match();