Jump to content

PHP Array Sort


whatever

Recommended Posts

New to this forum so hope you can help me,

 

Before I pull my hair out here trying to get this to work, I was wondering can anyone see how come this isnt working.

 

The code creates thumbnails of pictures from the folder that it is in and displays them in a table which is 4 columns wide. At the moment the code seems to randomly pick files and place them in the html table.  I am looking to sort the files by name (all files have numbers at the end - picture01, picture 02 etc).

 

I placed sort($files) into the code just before the table is created and then the page layout is thrown out. It displays alot more than 4 pictures across the page. 

 

If anyone can see the problem It would be much appreciated.

 

Thanks

 

<?
// GALLERY SETTINGS

Define('DIR_CACHE', '../../../tmp/'); // directory to store modified images
Define('THUMBNAIL_WIDTH', '150'); // thumbnails width
Define('THUMBNAIL_HEIGHT', '150'); // thumbnails height
Define('IMAGES_ROW', 4); // images on row


// ----------------------------------------------------------------------

// Get folder name
$parentFolder = basename(dirname(__FILE__));

$img=$_GET['img'];

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

// IMAGE RESIZE AND SAVE TO FIT IN $new_width x $new_height
if ($img!='' && file_exists($img)) {

$w=$_GET['w'];
$h=$_GET['h'];

$thumb=strtolower(preg_replace('/\W/is', "_", "$img $w $h"));

  $changed=0;
  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) {
   Header("Content-Type:image/gif");
   readfile($filename);
   exit;
} 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;
//  $image_height=$new_height;
}

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


//  $new_image=imageCreate($image_width, $image_height); // for old GDlib
$new_image=imageCreateTrueColor($image_width, $image_height);

$white = ImageColorAllocate($new_image, 255, 255, 255);
ImageFill($new_image, 0, 0, $white);

// imagecopyresampled
// imageCopyResized (for old GDlib)
imageCopyResampled( $new_image, $old_image, 0, 0, 0, 0, $image_width, $image_height, imageSX($old_image), imageSY($old_image));

imageJpeg($new_image, DIR_CACHE.$thumb);
}


Header("Content-type:image/jpeg");
readfile(DIR_CACHE.$thumb);
exit;
}

// ----------------------------------------------------------------------

  $init_dir="./";

  if (IsSet($HTTP_GET_VARS['dir'])) {
   $dir=$HTTP_GET_VARS['dir'];
   $dir=preg_replace("/\.+/", ".", $dir);
  }

  if (IsSet($dir) && ($dir!=$init_dir)) {
   $directory=$dir;
   $tmp=array_reverse(explode('/', $dir));
   array_shift($tmp);
   array_shift($tmp);
   $out['UP']=urlencode(implode('/', array_reverse($tmp))."/");
  } else {
   $directory=$init_dir;
  }

  $dir=opendir($directory);
  $k=0;
  $on_row=IMAGES_ROW;


  if (file_exists($directory."files.txt")) {
   $data=LoadFile($directory."files.txt");
   $lines=explode("\n", $data);
   for($i=0;$i<count($lines);$i++) {
    $ar=explode(' ', trim($lines[$i]));
    $tmp=array_shift($ar);
    $descriptions[$tmp]=implode(' ', $ar);
   }
  }

  while ($file=readdir($dir)) {
   if (Is_Dir($directory."/$file") && ($file!='tmp') && ($file!='.') && ($file!='..')) {
    $directories[]=array('FILENAME'=>$file, 'DIR'=>$directory, 'DESCRIPTION'=>$descriptions[$file], 'URL'=>urlencode($directory."$file/"));
   }

   if (!Is_file($directory."/$file")) continue;

   $ext=strtolower(substr($file, -3));

   if ($ext!='gif' && $ext!='jpg') continue;

   $rec=array();
   $rec["FILENAME"]=$file;

   $size=(int)(filesize($directory."/".$file)/1024);
   if ($size>1024) {
    $rec["SIZE"]=(((int)($size*100/1024))/100)." Mb";
   } else {
    $rec["SIZE"]=$size." Kb";
   }
   if (IsSet($descriptions[$file])) {
    $rec['DESCRIPTION']=$descriptions[$file];
   }
   $rec["DIR"]=$directory;
   if (($k+1)%$on_row==0) {
    $rec['NEXT_ROW']=1;
   }
   $k++;
   $files[]=$rec;

  }

// ----------------------------------------------------------------------

function LoadFile($filename) {
// loading file
$f=fopen("$filename", "r");
$data="";
if ($f) {
  $data=fread($f, filesize($filename));
  fclose($f);
}
return $data;
}

?>

<html>
<body>

<?if (IsSet($out['UP'])) {?>
<p align=center>
<a href="?dir=<?=$out['UP']?>"><<< Up</a><br>
</p>
<?}?>

<?
if (Is_Array($files)) {
?>

<TABLE border="0" cellpadding="5" cellspacing="2" width="100%" bgcolor="#F0F0F0" id="table1">
<TR>
<TD>
<p align="left"><font face="Tahoma"><b>
	<?
	echo "O'Dwyers G.A.A: ".$parentFolder;
	?>
</b></font></TD>
</TR>
</TABLE>

<table border=0 align=center cellpadding=10>
<tr>
  <?foreach ($files as $f) {?>
  <td align=center valign=top>
   <font face="arial">
   <a href="#" onClick="window.open('../picture_view.html?image=<?echo $parentFolder.'/' ?><?=$f['FILENAME']?>', 'picture', 'height=10,width=10,status=no,toolbar=no,menubar=no,location=no'); return false;">
   <img src="?img=<?=$f['FILENAME']?>&w=<?=THUMBNAIL_WIDTH?>&h=<?=THUMBNAIL_HEIGHT?>" border=0 alt="<?echo $f['FILENAME'];?>"></a>
  </td>
  <?if ($f['NEXT_ROW']) echo "</tr><tr>";?>
  <?}?>
</tr>
</table>

<TABLE border="0" cellpadding="5" cellspacing="2" width="100%" bgcolor="#F0F0F0" id="table1">
<TR>
<TD>
<p align="center"><font face="Tahoma"><a href ="javascript:history.go(-1);">Back</a></font></TD>
</TR>
</TABLE>

<?
}
?>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/104841-php-array-sort/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.