Jump to content

Recommended Posts

I was wondering if it was possible and if anyone knew how to make a simple php script that looks at one specified directory and makes <img src="*.*" \> type links out of it?

 

ive got a very nice javascript type image viewer but i cant seem to get it to show every image.

 

and the next step would be getting it to view like 10 images per page but i havent gotten to the time that i need to figure that out yet lol.

 

anyone have any ideas?

 

 

this is what i have but i would like it to show more than 2 images lol:

$extensions = array('jpg','jpeg','gif','png','bmp');

// images folder

$images_folder_path = 'images/screenshots/';

// url to folder

$url_to_folder = 'http://ingis.com/butsagsms/images/screenshots/';

// Images Array (SRC value)

$images = array();

srand((float) microtime() * 10000000); // IF PHP Version < 4.2.0

// Open directory and read images

if ($handle = opendir($images_folder_path)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            
		// get file extension
		$ext = strtolower(substr(strrchr($file, "."), 1));

		if(in_array($ext, $extensions))
		{
		$images[] = $url_to_folder.$file;
		}

        }
    }
    closedir($handle);
}

if(!empty($images)) // Do we have something to show?
{
$rand_key = array_rand($images, 1);

$src = $images[$rand_key];

echo "   <a id='thumb1' href='".$src."' class='highslide' onclick='return hs.expand(this)'>
            <img src='".$src."' align='absmiddle' width='100' height='100' border='2px'></a>
	 </a>";

// Show second image; Make sure it will not be the same as the first one; We will remove the element of the first image from the array. This way the script will not reselect it

unset($images[$rand_key]);

$rand_key = array_rand($images, 1);

$src = $images[$rand_key];

echo "   <a id='thumb2' href='".$src."' class='highslide' onclick='return hs.expand(this)'>
            <img src='".$src."' align='absmiddle' width='100' height='100' border='2px' style='padding-left:3px;'>
	 </a>";
}
else
{
echo '<small>No images found</small>';
}

 

 

if i set the array to say 6 and then i dont have 6 images , ill get a error

so is there a way to present as many images as are in the directory?

Link to comment
https://forums.phpfreaks.com/topic/178136-solved-simple-image-grab-script/
Share on other sites

thats a pretty good resource actually.

 

i think i have most of what i need already in the above script.

 

i think i just need a simple script that shoots out however many images are there.

 

something like :

 

if(!empty($images)) {
$rand_key = array_rand($images, !);

$src = $images[$rand_key];

echo "   <a id='thumb1' href='".$src."' class='highslide' onclick='return hs.expand(this)'>
            <img src='".$src."' align='absmiddle' width='100' height='100' border='2px'></a>
	 </a>";
}

 

would that work?

 

sorry im very new at php

Something simple like this should do it:

 

<?php
$path = '/images/';
$numImages = 0;

foreach (new DirectoryIterator('/path/to/images') as $item) {
if ($item->isFile() && in_array(pathinfo($item->getPathname(), PATHINFO_EXTENSION), array('png', 'jpg', 'gif'))) {
	echo "<img src='" . $path . $item->getFilename() . "'>";

	++$numImages;
}
}

printf('There are %d images.', $numImages);

hey thats a great start but when i tried it , it only jumped to teh print function and tells me the amount of images there are in the directory.

lol

maybe the img src part is wrong?

 

it deffinately sees the correct directory tho in order to tell how many images.

yep your right i was missing 1 slash after the directory name haha thank you so much thats a very big help.

 

1 last thing/

 

would you happen to know how to make them group into pages of say 15 for example?

 

wouldnt want extraneous images making the page miles long  ;)

DBMS= database management system?

 

Yep.

 

i am using mysql for the website but mysql doesnt store images but i think i know what you mean. thanks for all your help :)

 

Actually, it can store binary files (like images), but I just meant that you should store the path to the file.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.