Jump to content


Photo

Extract photos from directory based filename criteria


  • Please log in to reply
1 reply to this topic

#1 jwheatley

jwheatley

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 23 January 2013 - 03:19 PM

Oh Joy! Another PHP noob with novice OOP skills looking for help! Ha.

I want to thank anyone in advanced for their help. I'm trying to avoid creating a separate image gallery for some 3000 intersections and move this old approach into a template. I've gone through the documentation to try and pull this together myself with mild success, but no luck. My code is below but is probably a jumbled mess.

We have a directory of some 20,000 photographs (.jpg). They are countywide photos of road intersections. Each intersection can have several (varying count) “sections” and each section can have multiple photos (also varying count). The photos have been named with the following convention - ID_SEC_IMG#, where: ID = numeric identifier that identifies the image as belonging to a particular intersection. Within the intersection, it is then broken into multiple sections (potential of 12 unique sections within each intersection). Each section then can have multiple photos. So the folder may look like this…

117_1_1.jpg where 117=intersection ID; 1=Section #; 1=Image #
117_1_2.jpg
117_1_3.jpg
117_2_1.jpg
117_3_1.jpg
117_4_1.jpg
117_4_2.jpg
117_4_3.jpg
117_4_4.jpg
118_1_1.jpg...

What we need to do is:
1. Find all photos in the folder that belong to a particular intersection ID that will be pass into a $sigID (117) variable via an <a> link on a separate page;
2. Of those photos, read out the Section # and display all photos with Section #1 in <div class=”s1”>; all photos with Section #2 in <div class=”s2”>; and so on.
3. Then ensure to iterate (and echo) all images that match the first 2 criteria to ensure a complete dump of all photos for the intersection within the proper div based on section.

Resulting in markup as follows:

<div class=”s1”><img src=”117_1_1.jpg” /><img src=”117_1_2.jpg” /><img src=”117_1_3.jpg” /></div>
<div class=”s2”><img src=”117_2_1.jpg” /></div>
…and so on.

I feel like Im close.

Again I appreciate your help with all of this. I’m still learning PHP and trying to strengthen my OOP skills. I feel like I understand what I need to accomplish, I’m just lacking the wherewithal to make it happen.

<?php
$sigID= 117;
$myPicFolder = "../photos/";
$thePics = array();
if (is_dir($myPicFolder)) {
if ($dh = opendir($myPicFolder)) {
while (($file = readdir($dh)) !== false) {
if(!is_dir($myPicFolder.$file)){
	 list($pictureID,$quadID)=explode('_',$file);//Seperates the parts of the name for easier access
	 //check if the ID is already a key in $thePics
	 if(!array_key_exists($pictureID,$thePics)){
	 //Add the new ID if it does not exist with an empty array.
	 $thePics[$pictureID]=array();
	 }
	 // Add a new part to the ID since each picture ID has 4 pictures.
	 $thePics[$pictureID][$quadID] = $file;
}
}
closedir($dh);
}
}



//Cycle through the pictures...
foreach($thePics as $pictureID=>$quad){
if($pictureID == $sigID){
var_dump($pictureID);
var_dump($quad);
//echo '<div class="quad">';
foreach($quad as $quadPart=>$pictureSRC){
echo '<img src="'.$myPicFolder.$pictureSRC.'"/>';
}
//echo '</div>';
}
}
?>

Edited by jwheatley, 23 January 2013 - 03:20 PM.


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,152 posts
  • LocationWA

Posted 23 January 2013 - 04:32 PM

#1. glob
$files = glob("/path/to/images/117_*_*.jpg");
#2. basename and explode
// $filename = "/path/to/images/117_1_1.jpg";
list($intersection, $section, $image) = explode("_", basename($filename, ".jpg"));





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com