Display Image In PHP and ASP.NET

In a nutshell it says same thing. When end user will load a page, few pictures will automatically opened up.
In PHP, a DirectoryItems class has been constructed and later in index.php page that class has been used to dispaly images.
First the code of DirectoryItems Class:

class DirectoryItems{
//data members
private $filearray = array();
private $replacechar;
private $directory;
//////////////////////////////////////////////////////////////////
//constructor
/////////////////////////////////////////////////////////////////
public function __construct($directory, $replacechar = "_"){
$this->directory = $directory;
$this->replacechar = $replacechar;
$d = "";
if(is_dir($directory)){
$d = opendir($directory) or die("Failed to open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}else{
//error
die("Must pass in a directory.");
}
}
//////////////////////////////////////////////////////////////////
//destructor
//////////////////////////////////////////////////////////////////
public function __destruct(){
unset($this->filearray);
}
//////////////////////////////////////////////////////////////////
//public fuctions
//////////////////////////////////////////////////////////////////
public function getDirectoryName(){
return $this->directory;
}
//////////////////////////////////////////////////////////////////
public function indexOrder(){
sort($this->filearray);
}
//////////////////////////////////////////////////////////////////
public function naturalCaseInsensitiveOrder(){
natcasesort($this->filearray);
}
//////////////////////////////////////////////////////////////////
//returns false if files are not all images of these types
//////////////////////////////////////////////////////////////////
public function checkAllImages(){
$bln = true;
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
$bln = false;
break;
}
}
return $bln;
}
//////////////////////////////////////////////////////////////////
//returns false if not all specified extension
//////////////////////////////////////////////////////////////////
public function checkAllSpecificType($extension){
$extension = strtolower($extension);
$bln = true;
$ext = "";
foreach ($this->filearray as $key => $value){
$ext = substr($key,(strpos($key, ".")+1));
$ext = strtolower($ext);
if($extension != $ext){
$bln = false;
break;
}
}
return $bln;
}
//////////////////////////////////////////////////////////////////
public function getCount(){
return count($this->filearray);
}
//////////////////////////////////////////////////////////////////
public function getFileArray(){
return $this->filearray;
}
//////////////////////////////////////////////////////////////////
//eliminate all elements from array except specified extension - Phase 2
/////////////////////////////////////////////////////////////////
public function filter($extension){
$extension = strtolower($extension);
foreach ($this->filearray as $key => $value)
{
$ext = substr($key,(strpos($key, ".")+1));
$ext = strtolower($ext);
if($ext != $extension){
unset($this->filearray[$key]);
}
}
}
//////////////////////////////////////////////////////////////////
//eliminate all elements from array except images - Phase 2
/////////////////////////////////////////////////////////////////
public function imagesOnly(){
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
unset($this->filearray[$key]);
}
}
}
//////////////////////////////////////////////////////////////////
//recreate array after filtering - Phase 2
/////////////////////////////////////////////////////////////////
public function removeFilter(){
unset($this->filearray);
$d = "";
$d = opendir($this->directory) or die("Couldn't open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$this->directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}
//////////////////////////////////////////////////////////////////
//private functions
/////////////////////////////////////////////////////////////////
private function createTitle($title){
//strip extension
$title = substr($title, 0, strrpos($title, "."));
//replace word separator
$title = str_replace($this->replacechar, " ", $title);
return $title;
}
}//end class

Next the index.php page where this class’s properties and methods has been used.

The index.php code where DirectoryItems class has een used

The page looks like this:

The index page with Images being displayed

Next day I’ll show the asp.net page where you can do the same thing using .NET class library.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s