Why does function execute again?

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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Why does function execute again?

Post by SBro »

I have the following function within a class I made:

Code: Select all

/**
	* @return array
	* @param array $img_array
	* @param string $width
	* @param string $height
	* @desc returns thumbnails in path
	**/
	function get_thumbs($img_array, $width, $height) {
		// create filesystem
		global $filesystem;
		
		foreach ($img_array as $image) {
			// file info
			$info = $filesystem->path_info($image);
			$dir_name = $info['dirname'].'/';
			$file_name = $info['basename'];
			$file_ext = $info['extension'];
			$thumb_dir = $dir_name.'thumbs/';
			$thumb_path = $thumb_dir.$file_name;
			
			// thumbnail exists
			if (file_exists($thumb_path)) {
				$thumb_array[] = $thumb_path;
			}
			else {
				// first create directory if not there
				if (!file_exists($thumb_dir)) {
					$mkdir = mkdir($thumb_dir);
					if (!$mkdir) {
						// error
						echo 'Unable to create directory: '.$thumb_dir.'';
						exit;
					}
				}
				// now put thumbs in directory
				$thumb = $this->generate_thumbs($image, $thumb_path, $width, $height);
				if ($thumb) {
					// thumb creation was successful
					$thumb_array[] = $thumb_path;
				}
				else {
					// errror
					echo 'Unable to generate thumbnail '.$thumb_path.'<br>';
					exit;
				}
			}
		}
		return $thumb_array;
	}
It works fine the first time I run it, a directory is created, and thumbnails are added to that directory. But if I run it again, instead of just grabbing the thumbnail names, it creates them again in a lower directory. For example:

first time i run it in /images -> dir is created /images/thumbs/ and thumbs are put in that dir
second time i run it in /images -> dir is created /images/thumbs/thumbs and thumbs are put in that dir, and the resulting $thumb_array contains all the thumbnails in /images down, thus each time I run it, the array gets bigger as it is creating a deeper /thumbs directories.

I'm unsure why it is doing this, any help would be appreciated, cheers.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think you may want to switch file_exists() to is_dir() on line 27
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Unfortunately that doesn't change things. A thumbs directory is still created (under the existing thumbs directory) instead of just grabbing the thumbnail files and placing them in an array. Thumbnail files are still placed in array, but they are placed there again (under the newly created thumbs directory).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was confused about something.. it looks like your code is trying to create thumbs of thumbs..
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Sorry to confuse :)

The function works as follows, an array of images (full-size) is sent to it, along with the height and width of the thumbnail size we want. The function then checks to see if a thumbnail exists (thumbnails are stored as same filename but under a /thumbs directory) if it exists it should just grab the thumbnail name and add it to the array, if it doesn't exist it should create the thumbnail and add the name to the array, maybe that will clear things up ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it sure looks like the code recurses directory structures, which would make it create thumbs of the thumbs folder.

If you changed the thumb path name to the current timestamp (when the function started processing), you may see some "odd" behaviour.

This may be a problem of how $img_array is created. You could try switching the file_exists() call on line 22 to is_file(), which may fix it to only process the directory you pass.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Thanks Feyd, it was a problem with $img_array; which was calling a function which loops through a directory and returns images, as thumbs were already created it returned them as well. So I simply stripped them out from $img_array, thanks again for your help.
Post Reply