PHP: Sorting array with natsort -
I have a PHP script that creates thumbnails and lists an image gallery that is causing me problems That it lists the timestamp on the server, but I want to put it in 'naturally' list.
& lt; Php # settings $ max_width = 100; $ Max_height = 100; $ Per_page = 24; $ Page = $ _GET ['Page']; $ Has_previous = false; $ Has_next = false; Function getPictures () {global $ page, $ per_page, $ is_Next, $ is_ext; If ($ handle = opendir (".")) {$ Lightbox = rand (); Echo & lt; Ul id = "picture" & gt; '; $ Count = 0; $ Skip = $ page * $ per_page; If ($ skip! = 0) is $ is_ previous = true; While ($ count '; Echo & lt; / A & gt; & Lt; / Li & gt; '; $ Count ++; }} Echo & lt; / Ul & gt; '; While (($ file = readdir ($ handle))! == Incorrect) {if_dir ($ file) & amp; Amp; ($ Type = getPictureType ($ file)) = '') {$ has_next = true; break; }}}} Function getPictureType ($ file) {$ split = explode ('.', $ File); $ Ext = $ partition [count ($ partition) - 1]; If (preg_match ('/ jpg | jpeg / i', $ ext)) {return 'jpg'; } And if (preg_match ('/ png / i', $ ext)) {return 'png'; } And if (preg_match ('/ gif / i', $ ext)) {return 'gif'; } Other {return ";"; }} Function MathSham ($ file, $ type) {global $ max_method, $ max_height; If ($ type == 'jpg') {$ src = imagecreatefromjpeg ($ file); } And if ($ type == 'PNG') {$ src = imagecreatefrompng ($ file); } And if ($ type == 'gif') {$ src = imagecreatefromgif ($ file); } If ($ oldW = imagesx ($ src)) & lt; ($ oldH = images ($ src))) {$ newW = $ oldW * ($ max_width / $ oldH); $ NewH = $ max_height; } And {$ newW = $ max_width; $ NewH = $ old H * ($ max_height / $ oldW); } $ New = imagecreatetruecolor ($ newW, $ newH); Imagecopyresampled ($ new, $ src, 0, 0, 0, 0, $ newW, $ newH, $ oldW, $ oldH); If ($ type == 'jpg') {imagejpeg ($ new, 'thumb /' .a file); } And if ($ type == 'PNG') {imagepng ($ new, 'thumb /'. $ File); } And if ($ type == 'GIF') {imagegif ($ new, 'thumb /'. $ File); } Imagedestroy ($ new); Imagedestroy ($ src); }? & Gt;
This trick was to keep everyone inside an array ... I am your freedom getPictures ()
rewrite the function ... it executes a sorting.
function getPictures () {global $ page, $ per_page, $ is_Next, $ is_ext; If ($ id = '' thumbs '') (MKDIR ('thumb');} if ($ handle = opendir (".")) {$ Lightbox = rand (); $ files = array (); while (($ File = readdir ($ handle))! == incorrect) {if_dir ($ file) & amp; amp; ($ type = getPictureType ($ file)) = '') {$ files [] = $ file; }} Natsort ($ files); $ Is_first = $ skip! = 0; $ Has_next = (counting ($ files) - $ skip - $ per_page) & gt; 0; $ SpliceLength = min ($ per_page, count ($ files) - $ skip); $ Files = array_slice ($ files, $ left, $ splistange); Echo & lt; Ul id = "picture" & gt; '; Foreign currency (files as $ $ file) {if (! File_exists ('thumbs /'. $ File)) {$ type = getPictureType ($ file); MakeThumb ($ file, $ type); } Echo & lt; Li & gt; & Lt; A href = "'. $ File.'" Class = "zoom" rel = "group" & gt; '; Echo '& lt; Img src = "thumbs / '. $ File." "Alt =" "/>, echo & lt; / a & gt; & lt; / li & gt; ';} echo' & lt; / ul & gt; '}}}
< / Pre>
Comments
Post a Comment