designermonkey Posted February 2, 2010 Share Posted February 2, 2010 Hi! I'm new here and need some help finishing off a function I have written to output thumbnails. I've written it the only way I know how, but now I've hit a dead end. The function is: function output_thumbs( $path, $thumbpath, $thumbsize ){ // Set working directory chdir(getcwd().$path); // Make thumbs dir if it doesn't exist if(!file_exists($thumbpath)){ mkdir($thumbpath); } // Array the images $images = list_files(getcwd(), '.jpg'); //Set up the counters $precounter = 0; $counter = 1; $imagecount = count($images); $iterator = 1; $output = false; // Output the initial <ol> echo "<ul class=\"thumbs fl\">"; // Iterate through the images foreach($images as $image){ // Create thumbs if not exists if(!file_exists(getcwd().$thumbpath.'/'.$image)) create_thumb($image, $thumbpath, $thumbsize); // Set up the output numbering strings from the counter $thiscounter = $counter + $precounter; if($thiscounter <= 9) $countstring = '0'.$thiscounter; else $countstring = ''.$thiscounter.''; if($thiscounter <=99) $countstring = '0'.$countstring; // Get iptc details $iptc = get_iptc(getcwd().'/'.$image); ($iptc[0]) ? $description = $iptc[0] : $description = "" ; ($iptc[1]) ? $headline = $iptc[1] : $headline = "" ; ($iptc[2]) ? $date = $iptc[2] : $date = "" ; // Strip extension for rewrite $imgexp = explode(".", $image); echo "<li class=\"fl mr mb thumb\" id=\"thumb$countstring\">"; echo "<a href=\"$path/$imgexp[0]/\" title=\"View $headline\" rel=\"[thumbnails]\">"; echo "<img class=\"\" src=\"$path/$thumbpath/$image\" alt=\"$description\" />"; echo "<div class=\"hidden mbDesc\">$description</div>"; echo "</a>"; echo "</li>"; // Increase the counter if($counter === 12){ echo "</ol>"; echo "<ol class=\"thumbs fl\">"; $precounter += $counter; $counter = 1; $iterator ++; }else{ $counter ++; } } echo '</ul>'; chdir($_SERVER['DOCUMENT_ROOT']); } Basically, it iterates through the images and outputs the <ul>s required per 12 images. I have set a variable to count the images, so that I could output blank <li>s in the last <ul> to fill it up to 12, that is where I'm stuck... How do I do that? Thanks in advance for any help offered... Link to comment https://forums.phpfreaks.com/topic/190653-help-with-an-iterating-function/ Share on other sites More sharing options...
jammesz Posted February 2, 2010 Share Posted February 2, 2010 Not sure if i understand your problem correctly or not, but you can modify this snippet to fix it. BTW this example has 100 images. <?php // example image array (100 images) $image_array=array(); for($i=1;$i<101;$i++){ $image_array[]=$i; } // end $count=0; foreach($image_array as $image){ $count++; if($count == 1){ echo '<ul>'; } echo '<li>'.$image.'</li>'; if($count==12){ $count=0; echo '</ul>'; } } if($count !== 0){ $diff=12-$count; for($i=1;$i<$diff+1;$i++){ echo '<li>Blank '.$i.'</li>'; } echo '</ul>'; } ?> It fills in the shortage of images with 'blank' to make up 12 items in a list. The result of the last ul is this: * 97 * 98 * 99 * 100 * Blank 1 * Blank 2 * Blank 3 * Blank 4 * Blank 5 * Blank 6 * Blank 7 * Blank 8 hope this helps Link to comment https://forums.phpfreaks.com/topic/190653-help-with-an-iterating-function/#findComment-1005458 Share on other sites More sharing options...
designermonkey Posted February 2, 2010 Author Share Posted February 2, 2010 Thanks for your help jammesz, I've answered my own question with a 'tea break epiphany', it's amazing what a cup of tea can do... Completed function below (added $multiple variable to make it useful for other counts): function output_thumbs( $path, $thumbpath, $thumbsize, $multiple ){ // Set working directory chdir(getcwd().$path); // Make thumbs dir if it doesn't exist if(!file_exists($thumbpath)){ mkdir($thumbpath); } // Array the images $images = list_files(getcwd(), '.jpg'); //Set up the counters $precounter = 0; $counter = 1; $imagecount = count($images); $iterator = 1; $output = false; // Output the initial <ol> echo "<ul class=\"thumbs fl\">"; // Iterate through the images foreach($images as $image){ // Create thumbs if not exists if(!file_exists(getcwd().$thumbpath.'/'.$image)) create_thumb($image, $thumbpath, $thumbsize); // Set up the output numbering strings from the counter $thiscounter = $counter + $precounter; if($thiscounter <= 9) $countstring = '0'.$thiscounter; else $countstring = ''.$thiscounter.''; if($thiscounter <=99) $countstring = '0'.$countstring; // Get iptc details $iptc = get_iptc(getcwd().'/'.$image); ($iptc[0]) ? $description = $iptc[0] : $description = "" ; ($iptc[1]) ? $headline = $iptc[1] : $headline = "" ; ($iptc[2]) ? $date = $iptc[2] : $date = "" ; // Strip extension for rewrite $imgexp = explode(".", $image); echo "<li class=\"fl mr mb thumb\" id=\"thumb$countstring\">"; echo "<a href=\"$path/$imgexp[0]/\" title=\"View $headline\" rel=\"[thumbnails]\">"; echo "<img class=\"\" src=\"$path/$thumbpath/$image\" alt=\"$description\" />"; echo "<div class=\"hidden mbDesc\">$description</div>"; echo "</a>"; echo "</li>"; // Increase the counter if($counter === $multiple){ echo "</ul>"; echo "<ul class=\"thumbs fl\">"; $precounter += $counter; $counter = 1; $iterator ++; }else{ $counter ++; } } // If less than $multiple thumbnails, output blanks up to $multiple if( ($iterator * $multiple) > $imagecount ){ while( $imagecount < ($iterator * $multiple) ){ echo "<li class=\"fl mr mb thumb\"> </li>"; $imagecount ++; } } echo '</ul>'; chdir($_SERVER['DOCUMENT_ROOT']); } Quite a useful thumbnailer, uses the following two functions also, feel free to use it as you please! // Folder list of actual files into array(string) function list_files( $directory, $extension ){ foreach( array_diff( scandir( $directory ), array( '.', '..' ) ) as $file ){ if( is_file( $directory.'/'.$file ) && ( ( $extension ) ? preg_grep( ('/'.$extension.'$/'), array($file) ) : 1 ) ){ $list[] = $file; } } return $list; } // Create thumbs of passed filepaths function create_thumb( $image, $thumbpath, $dimension ){ // get the path info for the image $info = pathinfo( $image ); // coninue only if this is a jpeg if( strtolower($info['extension']) == 'jpg' ){ // load image and get size $img = imagecreatefromjpeg( $image ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size if($width >= $height && $width > $dimension) { $new_height = $dimension; $new_width = floor($width / ($height / $new_height)); }elseif($width <= $height && $height > $dimension) { $new_width = $dimension; $new_height = floor($height / ($width / $new_width)); } // create a new temporary image $tmp_img = imagecreatetruecolor( $dimension, $dimension ); // copy and resize old image into new image imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail into a file imagejpeg( $tmp_img, $thumbpath.'/'.$image ); } } Link to comment https://forums.phpfreaks.com/topic/190653-help-with-an-iterating-function/#findComment-1005459 Share on other sites More sharing options...
jammesz Posted February 2, 2010 Share Posted February 2, 2010 Your welcome, glad i can help. Remember to mark this topic as solved too. Link to comment https://forums.phpfreaks.com/topic/190653-help-with-an-iterating-function/#findComment-1005468 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.