Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Array Sort

Options
  • 09-05-2008 1:28pm
    #1
    Registered Users Posts: 1,606 ✭✭✭


    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

    [PHP]<?
    // 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;

    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;
    $h=$_GET;

    $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=$HTTP_GET_VARS;
    $dir=preg_replace("/\.+/", ".", $dir);
    }

    if (IsSet($dir) && ($dir!=$init_dir)) {
    $directory=$dir;
    $tmp=array_reverse(explode('/', $dir));
    array_shift($tmp);
    array_shift($tmp);
    $out=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=$descriptions[$file];
    }
    $rec["DIR"]=$directory;
    if (($k+1)%$on_row==0) {
    $rec=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)) {?>
    <p align=center>
    <a href="?dir=<?=$out?>"><<< 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?>', 'picture', 'height=10,width=10,status=no,toolbar=no,menubar=no,location=no'); return false;">
    <img src="?img=<?=$f?>&w=<?=THUMBNAIL_WIDTH?>&h=<?=THUMBNAIL_HEIGHT?>" border=0 alt="<?echo $f;?>"></a>
    </td>
    <?if ($f) 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>[/PHP]


Comments

  • Registered Users Posts: 1,606 ✭✭✭sgarvan


    here is a screenshot of how the output looks with and without the sort added

    display.jpg


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    I think that this happens because you are setting $rec when reading the file list. When you do the sort() this info ends up in the wrong element.

    I recommend using a counter or for() loop when printing out the html code and test the counter or loop variable against IMAGES_ROW to determine where to add the table row tags.

    BTW, you are using short php tags (i.e. '<?' as opposed to '<?php'). This could make your script less portable. My server requires the long tags (<?php) so I had to change each short tag before I could begin looking at your script.

    Aside: I recommend using LightBox2 to display the images instead of opening a popup with picture_view.html.


Advertisement