-1

I am working on a website where images will be displayed using a library called photoswipe. I have got two folders, one for thumbnails and one with the original image. Both folders contain 70 images with filenames starting from '001.jpg' until '070.jpg'.

I am using PHP to put all these file names in an array. After that, I am using a foreach-loop to print the images.

The printing looks like this:

<a href="<?php echo $portraitsDir ?>original/<?php echo $valueOriginal; ?>" itemprop="contentUrl" data-size="<?php echo $dimensions?>" data-index="<?php $key ?>"> 
<img src="<?php echo $portraitsDir ?>thumbnail/<?php echo $imagesPortaitsThumbnail[$key]; ?>" height="100" width="100" itemprop="thumbnail" alt="All rights reserved."></a>

What would be the easiest way to re-order the files. Let's say that image '052.jpg' should be placed on position 7 on the website. Until now I was manually editing the file names. So 052 would become 007, 007 would become 008, 008 would become 009 and so on.

Sammekl
  • 501
  • 4
  • 20
  • 2
    What's the logic for reordering the array? In other words, how do you know 052.jpg should be seventh? – Mureinik Sep 01 '15 at 19:54
  • @Mureinik The website will be used by a photographer. She would like to have newer photo's to be showed first for example. – Sammekl Sep 01 '15 at 19:57
  • Rather that you editing filenames, it may be worthwhile providing a facility where the photographer can specify what order the images are to be displayed, by giving them a 'priority or rank'. You would display them in descending date order within 'priority', so that 'latest' ones are at the top of the list.. – Ryan Vincent Sep 01 '15 at 20:22

2 Answers2

0

Sorts $Originals and $Thumbnails by modified time of $Originals:

array_multisort(array_map('filemtime', $Originals), SORT_DESC, $Originals, $Thumbnails);

Based on your comment, you should use a database, even a small one. Then you just select the data and sort on a custom sort order stored in the database. You can present a form or button to allow people to move them and change that sort order. Check SQLite.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • That would be a good idea. However if you want to move photo's down in position, rather than to the top of the array this wouldn't be an ideal solution. I was thinking about a file which holds the order of photo's. It could even be a txt file. An example with 5 photos: "002, 004, 005, 001, 003" in a separate txt file. With this you only have to move one number. Would this be a good solution to the problem? – Sammekl Sep 01 '15 at 20:28
  • That's what databases are for. You asked how to sort with the most recent on top. To store a custom sort order you should use a database. – AbraCadaver Sep 01 '15 at 20:37
0

You might have to use the filemtime() function to find the modification date of each file.BTW check out this answer for more info! https://stackoverflow.com/a/16599152

Community
  • 1
  • 1
Silent_Coder
  • 150
  • 1
  • 11