Archive for the 'code tips' Category
compressing files in PHP
by Justin Silverton
Zlib compression has been built into php since version 3 and it can be used to compress the output of your php applications (which can significantly decrease the amount of bandwidth of a page), but what you can also do is compress any file accessible from your webserver.
The code
The following are two functions: compress and uncompress, which can compress and uncompress a specified file.
function uncompress($srcName, $dstName) {
$string = implode(”", gzfile($srcName));
$fp = fopen($dstName, “w”);
fwrite($fp, $string, strlen($string));
fclose($fp);
}
function compress($srcName, $dstName)
{
$fp = fopen($srcName, “r”);
$data = fread ($fp, filesize($srcName));
fclose($fp);
$zp = gzopen($dstName, “w9″);
gzwrite($zp, $data);
gzclose($zp);
}
compress(”test.php”,”test.gz”);
uncompress(”test.gz”,”test2.php”);
Source code can be downloaded here
Description of related zlib functions
More information on zlib related functions can be found here
8 commentsHow to write an image gallery script in PHP
Introduction
This is a simple yet useful tutorial on how to write an image gallery script in PHP. It could be used for anything from banner rotation to just a couple of images that you would like to display in rotation on your website. It requires no external libraries, works with php 4.0.3 and above, and will allow you to display all the images in a specifiied directory.
The Code
The code consists of a simple class with two main functions: GetAllFiles() and DisplayImages(). GetAllFiles() retrieves all the images within a specified directory and DisplayImages randomly displays them.
//set your directory here and remember to include the trailing slash
//the script will automatically pull images out of the specified directory and display them
define(”IMAGE_DIRECTORY”, “images/”);
class jrotate {
var $Imagelist = array(); //this stores all of the images that were found in your directory
var $ImageExtList = array(); //this will store the image extensions
function GetAllFiles() {
// Open a known directory, and proceed to read its contents
if (is_dir(IMAGE_DIRECTORY)) {
if ($dh = opendir(IMAGE_DIRECTORY)) {
while (($file = readdir($dh)) !== false) {
//check to see if it is a file
if (filetype(IMAGE_DIRECTORY.$file) == “file”) {
//get the files extension and make sure it is one of our supported image types
$current_ext = substr($file,strlen($file)-3,strlen($file));
if ($current_ext == “jpg” || $current_ext == “gif” || $current_ext == “png”) {
//push the filename and extension onto our arrays
array_push($this->Imagelist,IMAGE_DIRECTORY.$file);
array_push($this->ImageExtList,$current_ext);
}
}
}
closedir($dh);
}
}
}
function DisplayImage() {
//get the total number of images
$imagecount = count($this->Imagelist);
//randomly generate the next image that we will display
$nextimage = rand(0,$imagecount-1);
//determine the image type and return the proper header information
switch($this->ImageExtList) {
case ‘png’:
header(”Content-type: image/png”);
break;
case ‘gif’:
header(”Content-type: image/gif”);
break;
case ‘jpg’:
header(”Content-type: image/jpeg”);
break;
}
//read image into string and output it
$contents = file_get_contents($this->Imagelist[$nextimage]);
echo $contents;
}
} //end class
//create a new instance of the jrotate class
$imagerotator = new jrotate;
//display random image
$imagerotator->GetAllFiles();
$imagerotator->DisplayImage();
//$test = array();
Download
To download the entire script explained here and an example of usage, please go Here
2 comments





