Page 1 of 1

Why does function execute again?

Posted: Fri Apr 01, 2005 9:17 pm
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.

Posted: Fri Apr 01, 2005 10:25 pm
by feyd
I think you may want to switch file_exists() to is_dir() on line 27

Posted: Fri Apr 01, 2005 10:30 pm
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).

Posted: Fri Apr 01, 2005 10:31 pm
by feyd
I was confused about something.. it looks like your code is trying to create thumbs of thumbs..

Posted: Fri Apr 01, 2005 10:54 pm
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 ?

Posted: Fri Apr 01, 2005 11:39 pm
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.

Posted: Sat Apr 02, 2005 6:52 pm
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.