Showing images from directory help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Showing images from directory help

Post by stijn22 »

Hi,

I now have this code for showing images from a certain directory:

Code: Select all

<?php
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
		while($file = readdir($handle)){
			if($file !== '.' && $file !== '..'){
				echo '<img src="pictures/'.$file.'" border="0" />';
			}
		} ?>
This will show all images from that directory in their original size under eachother. What I want, is that the code shows miniatures of the images (for example 60x60px), and when you click on them you get the whole image in it's original size (in a new page or on the same page).

I have no idea where to start or how to make this code :oops:. I hope somebody can help me :D
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Showing images from directory help

Post by twinedev »

Code: Select all

<?php
	$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
	while($file = readdir($handle)){
		if($file !== '.' && $file !== '..'){
			echo '<a href="pictures/'.$file.'"><img src="pictures/'.$file.'" border="0" style="width: 60px; height:60px" alt="'.$file.'"/></a>';
		}
	}
?>
Now, if these are something that will be displayed often, it would be better to actually keep a copy of them already resized on the server. Especially the bigger the image is. The method above just basically shows the full size image at a smaller size, but the user still has to download the full size image.

Imagemagick is great for this, see if you have access to it, either through PHP extension (http://www.php.net/manual/en/imagick.examples-1.php) or at least to be able to execute command line via system().
stijn22
Forum Commoner
Posts: 43
Joined: Sat Aug 13, 2011 10:14 am

Re: Showing images from directory help

Post by stijn22 »

Thanks! It is working like a charm :D
Post Reply