Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read images into an array using a function ?

Status
Not open for further replies.

c0deM0nK424

Programmer
Oct 28, 2007
126
0
0
GB
okay heres whats up folks - i've recently got back into PhP Programming after a 7 1/2 year absence from it and my word its changed a bit - PHP5 caters for so much more functionality and flexibility than its predecessor !

So i'm a little rusty with my PhP coding, but its slowly all coming back to me - I was never a coding wizkid tbh, i learn through experimenting with segments of code and seeing it executed on screen and discover what it does etc.

I'll cut to the chase. Basicaly this is a segment of code that I wish to revise, improve and eventualy utilise in an e-commerce application that i'm building.


<?php
// all these images are in the same folder as the script itself. I have a total of 44 atm, but for testing purposes i've tried just 14 to begin with - resized them all to the same dimensions and the script works fine.

$images = array("1.jpg","2.jpg","3.jpg","4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "13.jpg", "14.jpg");

// for ($i=0; $i<count($images); $i++)
for ($i=0; $i<14; $i++)
{
echo ("<img src=\"$images[$i]\">");
print ("<br>");
}
?>

As you'd imagine, a series of images appear on the far left hand side of the screen nicely aligned and in sync. These images are actualy Music CD Album Covers. Thing is, how would one go abouts reading ALL the images 1 to 44 contained in a folder in the same directory ?

so now instead of actualy declaring them in the array above, I'd be simply declaring an array - followed by usage of some kind of function yes ? a function that will allow me to read in, as arguments - the filenames of those images.

Then I could either stick with my loop and traverse through and outptu the images or - if u guys can suggest another handy PhP Function , like a File Read or w/e - that'd be great.

Remember im a little 'rusty' with my php atm - but with a bit of direction It will speed things up for me.

That is all folks , remember - all im asking is how to elimate the need for:

$images = array("1.jpg, 2.jpg " " " 44.jpg)

and replace this with something that will load all the images into the array without me having to actualy enter the contents of the array as in the above.

thats cumbersome.

any suggestions folks ?

attachment supplied - screenshot and code segment.
 
Hi, I think you are on the right track. Use dirlist or database, dont define an array in the script :)

Olav Alexander Mjelde
 
Yes, the opendir and readdir functions are the thing to use.

I would strongly suggest not using a database to store the images, as that only makes it more cumbersome to manage, and adds to the complexity of the script. It also requires a secondary output script to be able to display the image.

Personally, I'd take the example from the Php.net online manual and run with it.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Yes, I would not store the images in the db, but you can store metadata.
However, you can also use metadata in the files :)

If you look here:

There I read the metadata that the camera stored to the image.
You can also store metadata to the files if you wish.

I dont think a metadata-search engine based on PHP would perform very well, but if you just use it on some few images, you could use that approach too.
It would however demand some coding :) The advantage with the metadata in the file, is that it follows your file. if you resize and store it as a new image, it will stay with the file (unless one clears the metadata or stores it with an application that does not support metadata).

One cool thing if you store metadata in mysql, is that you can make an rss-feed that complies with the webmaster tools/bing webmasters central. You can then place that feed in the admin centres and they will then index your images as soon as they are uploaded. You can also specify geolocation-data, copyright, etc. so the images will appear in local search. However this might be out of your scope for such images :p

Olav Alexander Mjelde
 
I wrote a simple album a while back. I use a plain text file to list the albums and then by parsing this file I display them up on the screen. Each album triggers a display of photos associated to the album upon clicking on the album's cover image.

Here is the album code
Code:
<?php
$handle = fopen("media/photos/album/albumlist.txt","rb");
$data = stream_get_contents($handle);
// print_r($data); echo '<br /><p>&nbsp;</p>';

$lines = explode("\r\n",$data);
$info = explode(":",$lines);
// print_r($lines); echo '<br /><p>&nbsp;</p>';
$img=0;
for($x=1; $x<=sizeof($lines); $x++) {
	$split = explode(":",$lines[$x]);
	if($split[0]) { 
		echo "<div style=\"float: left; padding-left: 2px; padding-top: 20px;\"><img onclick=\"xajax_lazen('photolist','".$split[2]."~".$split[3]."');\" title=\"".$split[0]."\" onmouseover=\"this.style.cursor='pointer';\" onmouseout=\"this.style.cursor='default';\" src=\"".$split[1]."\" style=\"width: 155px; height: 175px; padding-right: 2px; border: 0px;\" /><br />".$split[0]."<br />".sizeof(scandir($split[2]))."&nbsp;Photos</div>";
		$img++;
		if($img > 3) {
			$img=0;
			echo '<p>&nbsp;<br /></p><p>&nbsp;<br /></p>';
		}
	}
}
?>

Here is a sample of the plain text file - please notice the : is used a column separator
Code:
Album Name:Album Heading Image:Album Photos Directory:Album Thumnail Directory:
Kaya Jones 2010:media/photos/album/kayajones2010.jpg:media/photos/album/kayajones2010:media/photos/thumbnail/kayajones2010:
Oro Solido 2010:media/photos/album/orosolido2010.jpg:media/photos/album/orosolido2010:media/photos/thumbnail/orosolido2010:
Halloween Party 2010:media/photos/album/halloween2010.jpg:media/photos/album/halloween2010:media/photos/thumbnail/halloween2010:
REMAX<br />Christmas Party 2010:media/photos/album/remax2010.jpg:media/photos/album/remax2010:media/photos/thumbnail/remax2010:
Saturday 18 2010:media/photos/album/sat-12-18-2010.jpg:media/photos/album/sat-12-18-2010:media/photos/thumbnail/sat-12-18-2010:
Christmas Day 2010:media/photos/album/christmas-day-2010.jpg:media/photos/album/christmas-day-2010:media/photos/thumbnail/christmas-day-2010:
Saturday 30 2010:media/photos/album/sat-12-30-2010.jpg:media/photos/album/sat-12-30-2010:media/photos/thumbnail/sat-12-30-2010:
Assorted Images/photos/album/assorted.jpg:media/photos/album/assorted:media/photos/thumbnail/assorted:

I use XAJAX to call AJAX routines which in turn run my PHP Server Side code to display the images within the album
Code:
<?PHP
	include('bin/config.php');

	// open this directory 
	$myDirectory = scandir($_SESSION['thumbnail']);
	$img=0;
	for($x=0; $x<=sizeof($myDirectory); $x++) {
		$file = $myDirectory[$x];
		if($file != "." && $file != '..') { 
			echo "<div style=\"float: left; padding-left: 2px; padding-top: 20px;\"><img class=\"magnify\" src=\"".$_SESSION['thumbnail'].'/'.$file."\" style=\"width: 155px; height: 175px; padding-right: 2px; border: 0px;\" onclick=\"xajax_lazen('LARGEPHOTO','".$_SESSION['photolist']."/".$file."');\" /></div>";
			$img++;
			if($img > 3) {
				$img=0;
				echo '<p>&nbsp;<br /></p><p>&nbsp;<br /></p>';
			}
		}
	}
	echo '</div><div style="clear: both;">&nbsp;</div>';
?>

Hope these snippets help you out!!!!




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
take a look at glob() too. no need to do file opens /opendir etc

Code:
function getFiles ($myDir, $ext = 'jpg'){
 return glob( realpath ($myDir) . DIRECTORY_SEPARATOR . '*.' . $ext);
}

(note that glob is case sensitive, even on systems that don't have case sensitive file systems).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top