/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/ImageThumbnailWidget.inc
ViewVC logotype

Annotation of /nfo/php/libs/com.newsblob.phphtmllib/widgets/ImageThumbnailWidget.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Sat Feb 22 21:08:23 2003 UTC (21 years, 4 months ago) by jonen
Branch: MAIN
Changes since 1.1: +11 -11 lines
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

1 jonen 1.1 <?php
2    
3     /**
4     * This contains the ImageThumbnailTable widget
5     *
6 jonen 1.2 * $Id: ImageThumbnailWidget.inc,v 1.5 2002/12/16 23:27:42 hemna Exp $
7 jonen 1.1 *
8     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
9     * @package phpHtmlLib
10     *
11     */
12    
13     /**
14     * This widget creates a N by x visual table of
15     * thumbnails.
16     *
17     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
18     * @package phpHtmlLib
19     */
20     class ImageThumbnailWidget extends BaseWidget {
21    
22     /**
23     * hold the path on disk to image dir
24     *
25     * @access private
26     */
27     var $_filedir = NULL;
28    
29     /**
30     * hold the path on disk to the thumbs
31     * that we will create.
32     *
33     * @access private
34     */
35     var $_thumbsdir = NULL;
36    
37     /**
38     * hold the url path to the images
39     *
40     * @access private
41     */
42     var $_urldir = NULL;
43    
44     /**
45     * hold the list of images from disk
46     *
47     * @access private
48     */
49     var $_filelist = array();
50    
51    
52     /**
53     * The number of thumbs to show per page.
54     *
55     * @access private
56     */
57     var $_maxthumbsperpage = 10;
58    
59     /**
60     * The number of columns for a
61     * page.
62     *
63     */
64     var $_columns;
65    
66    
67     /**
68     * The thumbnail image width.
69     *
70     * @access private
71     */
72     var $_thumb_width = 50;
73    
74     /**
75     * The thumbnail image height.
76     *
77     * @access private
78     */
79     var $_thumb_height = 50;
80    
81     /**
82     * Array of supported image
83     * type flags.
84     *
85     */
86     var $_supported_image_types = array("WBMP" => FALSE,
87     "PNG" => FALSE,
88     "JPG" => FALSE,
89     "GIF" => FALSE);
90    
91    
92     /**
93     * holds the prefix for all variables
94     * that are added to a url,
95     * so we can possibly have more
96     * then 1 of these per page.
97     *
98     */
99     var $_global_prefix='';
100    
101     /**
102     * Holds the name of the offset
103     * variable. It's prefixed w/
104     * the _global_prefix var.
105     */
106     var $_offsetVar = 'offset';
107    
108    
109    
110    
111    
112     /**
113     * Constructor for this class
114     * It just sets the width for the
115     * widget.
116     *
117     * @param int $width - the width of the widget
118     * @param int $cols - the number of columns of images
119     * the default is 5.
120     */
121     function ImageThumbnailWidget( $width = 760, $cols=5, $filedir=NULL, $urldir=NULL) {
122     $this->set_width( $width );
123     $this->set_cols( $cols );
124     $this->set_filedir( $filedir );
125     $this->set_urldir( $urldir );
126    
127     $this->set_global_prefix('');
128     $this->get_supported_image_formats();
129     }
130    
131    
132     //functions for adding/updating data
133     function push($url, $text, $selected=FALSE) {
134     array_push($this->data, array("type"=>"url", "url"=>$url,
135     "text"=>$text, "selected" => $selected));
136     }
137    
138     function push_blank( $num=1 ) {
139     for ($x=1; $x<=$num; $x++)
140     array_push($this->data, array( "type"=>"blank" ));
141     }
142    
143     function push_text( $text, $selected=FALSE ) {
144     array_push($this->data, array( "type"=>"text", "text"=>$text,
145     "selected" => $selected ));
146     }
147    
148     /**
149     * This function sets a prefix for all
150     * variables that are used in the item list
151     * table on a page. This allows you to have
152     * multiple itemlists on a single html page.
153     *
154     * @param string $prefix - the prefix for all vars.
155     */
156     function set_global_prefix($prefix) {
157     $this->_global_prefix = $prefix;
158     $this->_offsetVar = $prefix . "offset";
159     }
160    
161    
162     /**
163     * Set the url for the thumbnail generation
164     * script.
165     */
166     function set_thumbnail_script( $script ) {
167     $this->thumbnail_script = $script;
168     }
169    
170    
171     /**
172     * Set the full path on disk where
173     * the images live.
174     */
175     function set_filedir( $dir ) {
176     $this->_filedir = $dir;
177     $this->_thumbsdir = $this->_filedir."/thumbs";
178     }
179    
180     /**
181     * Set the base url path where the files
182     * live on the web site.
183     */
184     function set_urldir( $dir ) {
185     $this->_urldir = $dir;
186     }
187    
188     /**
189     * set how many columns the user wants
190     * to display per row of thumbnails.
191     */
192     function set_cols( $cols ) {
193     $this->_columns = $cols;
194     }
195    
196     /**
197     * set how many thumbnails to display
198     * per page.
199     */
200     function set_maxthumbs_per_page( $num=10 ) {
201     $this->_maxthumbsperpage = $num;
202     }
203    
204     /**
205     * Make sure the maxthumbsperpage
206     * isn't > then total items.
207     *
208     */
209     function test_maxthumbs_per_page() {
210     if ($this->_maxthumbsperpage > $this->total_items()) {
211     $this->set_maxthumbs_per_page( $this->total_items() );
212     }
213     }
214    
215     /**
216     * Sets all thumnail dimensions.
217     *
218     * @param int $width - thumbnail width
219     * @param int $height - thumbnail height
220     */
221     function set_thumbnail_dimensions( $width=50, $height=50 ) {
222     $this->_thumb_width = $width;
223     $this->_thumb_height = $height;
224     }
225    
226    
227     /**
228     * This tests to see what type of gd
229     * lib image format this server supports.
230     *
231     */
232     function get_supported_image_formats() {
233    
234     $flag = imagetypes();
235    
236     $r['WBMP'] = $flag & IMG_WBMP;
237     $r['PNG'] = $flag & IMG_PNG;
238     $r['JPG'] = $flag & IMG_JPG;
239     $r['GIF'] = $flag & IMG_GIF;
240    
241     $this->_supported_image_types = $r;
242     }
243    
244    
245     /**
246     * Get the current value of the offset var
247     *
248     * @return int - the page offset
249     */
250     function offset() {
251     global ${$this->_offsetVar};
252     if ( ! isset(${$this->_offsetVar}) )
253     ${$this->_offsetVar} = 0;
254     return (int) ${$this->_offsetVar};
255     }
256    
257    
258     /**
259     * Calculates the # of rows
260     * per page possible.
261     *
262     * @return int
263     */
264     function rows_per_page() {
265     if ($this->_columns > $this->total_items()) {
266     user_error("ImageThumbnailTable: number of columns per page is larger then the total items to display",
267     E_USER_ERROR);
268     }
269     $rows = ($this->_maxthumbsperpage / $this->_columns);
270     if (($rows > 0) & ($rows < 1)) {
271     $rows = 1;
272     }
273     return (int)$rows;;
274     }
275    
276    
277     /**
278     * Calculate the # of items on a page.
279     *
280     * @return int
281     */
282     function items_per_page() {
283     $this->test_maxthumbs_per_page();
284     return $this->rows_per_page() * $this->_columns;
285     }
286    
287    
288     /**
289     * Calculate the number of
290     * pages possible for this
291     * list of thumbnails. It depends
292     * on the # of columns, and the
293     * max thumbs per page, and the total.
294     *
295     * @return int - # of pages.
296     */
297     function num_pages() {
298     //get the # of items per page
299     $pages = $this->total_items() / $this->items_per_page();
300     $rem = $this->total_items() % $this->items_per_page();
301     if (($rem) > 1) {
302     //xmp_var_dump( $pages );
303     $pages = ((int)$pages) + 1;
304     //xmp_var_dump( $pages );
305     }
306     return $pages;
307     }
308    
309     /**
310     * Is thie offset for the last page?
311     *
312     * @param int - page offset
313     *
314     * @return boolean TRUE:FALSE
315     */
316     function is_last_page( $offset ) {
317     return ($offset == ($this->num_pages() - 1));
318     }
319    
320    
321     /**
322     * returns the total # of
323     * items we have to display.
324     *
325     * @return int - total items.
326     */
327     function total_items() {
328     return count($this->_filelist);
329     }
330    
331     /**
332     * Calculate the # of items
333     * to on a particular page.
334     *
335     * @param int - page offset #
336     * @return int - # of items on offset's page.
337     */
338     function num_items_on_page( $offset ) {
339     if ($this->is_last_page( $offset )) {
340     $items = $this->total_items() - $this->item_index($offset);
341     return $items;
342     } else {
343     return $this->items_per_page();
344     }
345     }
346    
347    
348     /**
349     * Calculate the item # for the offset.
350     *
351     * @return int
352     */
353     function item_index( $offset ) {
354     return $this->offset() * $this->items_per_page();
355     }
356    
357    
358    
359    
360     /**
361     * build the list of images
362     * from a directory on disk
363     */
364     function build_filelist() {
365     //First lets get the list
366     $this->_filelist = $this->getdirlist( $this->_filedir );
367     }
368    
369    
370     function getdirlist($dir) {
371     $dir=str_replace("%20"," ",$dir);
372     $d=array();
373     $i=0;
374     if(is_dir($dir)) {
375     $handle=opendir($dir);
376     while ($file = readdir($handle)) {
377     if ($file != "." && $file != "..") {
378     $tmp = strtoupper($file);
379     if (strstr($tmp, ".JPG")) {
380     $d[] = $file;
381     } elseif (strstr($tmp, ".PNG")){
382     $d[] = $file;
383     } elseif (strstr($tmp, ".GIF")) {
384     $d[] = $file;
385     }
386     }
387     }
388     closedir($handle);
389     }
390    
391     //$di=implode("\n",$d);
392     //xmp_var_dump( $d );
393     return $d;
394     }
395    
396     /**
397     * build the link td.
398     *
399     */
400     function build_link_td( $file ) {
401    
402     $img_link = $this->thumbnail_script . "?img=";
403     $img_link .= $this->_filedir . "/$file&w=50&h=50";
404     $filename = $this->_urldir . "/thumbs/" . $file;
405    
406     $img = html_img($filename, $this->_thumb_width, $this->_thumb_height);
407    
408     $link = $this->_urldir . "/" . $file;
409 jonen 1.2 $a = html_a(htmlentities($link), $img, "treenavnormal");
410 jonen 1.1
411     $td = new TDtag( array("align" => "left"), $a);
412     return $td;
413     }
414    
415    
416     /**
417     * This function strips the offset
418     */
419     function strip_offset( $query_string ) {
420    
421     $arr = explode( '&', $query_string );
422     $str = '';
423    
424     foreach( $arr as $var ) {
425     if (($var != '') && (strncmp($var, $this->_offsetVar,
426     strlen($this->_offsetVar)) != 0) ) {
427     $str .= "&".$var;
428     }
429     }
430     return $str;
431     }
432    
433    
434    
435     function build_page_control() {
436    
437     $base_url = $_SERVER["PHP_SELF"]. "?" . $this->strip_offset( $_SERVER["QUERY_STRING"] );
438    
439     //we just need to render some links
440     //to walk thru the pages
441     $total = $this->total_items();
442     $num_pages = $this->num_pages();
443     $offset = $this->offset();
444    
445     $container = container();
446    
447     //ok start to add the links
448     if ($offset == 0) {
449     //we are looking at the first page.
450     $container->add("FIRST");
451     } else {
452     //we aren't on the first page.
453 jonen 1.2 $container->add( html_a(htmlentities($base_url."&".$this->_offsetVar."=0"), "FIRST", "thumblink") );
454 jonen 1.1 }
455    
456     //render the "prev" link
457     if ($offset == 0) {
458     //we are looking at the first page.
459     $container->add( "PREV" );
460     } else {
461     //we aren't on the first page.
462     $page = $offset - 1;
463 jonen 1.2 $container->add( html_a(htmlentities($base_url."&".$this->_offsetVar."=$page"), "PREV", "thumblink") );
464 jonen 1.1 }
465    
466     //Render the "next" link
467     if ($this->is_last_page($offset)) {
468     //we are looking at the first page.
469     $container->add("NEXT");
470     } else {
471     //we aren't on the first page.
472     $page = $offset + 1;
473 jonen 1.2 $container->add( html_a(htmlentities($base_url."&".$this->_offsetVar."=$page"), "NEXT", "thumblink"));
474 jonen 1.1 }
475    
476     //Render the "Last" link
477     if ($this->is_last_page($offset)) {
478     //we are looking at the first page.
479     $container->add("LAST");
480     } else {
481     //we aren't on the first page.
482     $page = $num_pages - 1;
483 jonen 1.2 $container->add( html_a(htmlentities($base_url."&".$this->_offsetVar."=$page"), "LAST", "thumblink"));
484 jonen 1.1 }
485    
486     return $container;
487     }
488    
489    
490    
491     /**
492     * function that will render the widget.
493     * child class should override this.
494     *
495     */
496     function build_thumb_table( ) {
497    
498     $table = html_table("100%", 0, 1, 0);
499     $table->set_class( "treenvwrapper" );
500    
501     $cnt = $this->total_items();
502     $tr = new TRtag;
503    
504     //set the index into the page
505     $items_this_page = $this->num_items_on_page( $this->offset() );
506     $index = $this->item_index($this->offset());
507     $per_page = $this->items_per_page();
508     for ($i=$index; $i <= $index + $items_this_page; $i++) {
509     $tr->add( $this->build_link_td( $this->_filelist[$i] ) );
510    
511     //xmp_var_dump( (($i+1) % $this->_columns) );
512     //xmp_var_dump( $i );
513     if (((($i+1) % $this->_columns) == 0) && ($i != 0)) {
514     $table->add( $tr );
515     $tr = new TRtag;
516     } else if (( (($i+1) % $this->_columns) < $this->_columns) &
517     ($i == $index + $items_this_page)) {
518     //xmp_var_dump ( $i );
519     $table->add( $tr );
520     }
521     }
522    
523     return $table;
524     }
525    
526    
527     /**
528     * Render the entire widget. this includes the
529     * page offset links, as well as the thumbnails.
530     *
531     * @param int - the indentation level for
532     * the container.
533     * @param int - the output debug flag to
534     * maintain compatibility w/ the API.
535     *
536     * @return string the raw html output.
537     */
538     function render( $indent_level=1, $output_debug=0 ) {
539    
540     //try and build the list of images
541     $this->build_filelist();
542    
543     $this->build_thumbnails();
544    
545     $images_div = html_div("", $this->build_thumb_table() );
546     $images_div->set_style("float: left;padding-top: 5px;" );
547     $container = container( $page_div, html_br(), $images_div );
548    
549     $fieldset = html_fieldset();
550     $fieldset->set_style("width: ".$this->width);
551     $fieldset->set_id("imagethumbnail");
552    
553     $legend = html_legend($this->build_page_control());
554     $fieldset->add( $legend, $images_div );
555    
556     return $fieldset->render( $indent_level, $output_debug );
557     }
558    
559    
560    
561     /**
562     * this function builds a cache dir of thumbnails, so we
563     * don't have to render thumbnails every time we hit the
564     * thumbnails page.
565     *
566     */
567     function build_thumbnails() {
568    
569     //first make sure the thumnail cache dir
570     //exists.
571     //Lets try and create it.
572     $this->_create_thumbnail_cache_dir();
573    
574     set_time_limit(120);
575    
576     //ok lets walk through each file,
577     //create the resized image, and write it
578     //to disk.
579     $index = $this->item_index($this->offset());
580     $items_this_page = $this->num_items_on_page( $this->offset() );
581     for ($i=$index; $i <= $index + $items_this_page; $i++) {
582     $this->build_thumbnail_file( $this->_filelist[$i] );
583     }
584     }
585    
586    
587     /**
588     * Builds a thumbnail version of a file,
589     * and writes it to disk.
590     *
591     * @param string $filename - the filename to thumbnail
592     */
593     function build_thumbnail_file( $file ) {
594     $filename = $this->_filedir . "/" . $file;
595     $thumbname = $this->_thumbsdir . "/" . $file;
596    
597     if (!file_exists($thumbname)) {
598     $type = $this->_get_file_type( $file );
599    
600     //we currently support jpeg and png.
601     // Create the image...
602     switch( strtolower($type) ) {
603     case 'jpg':
604     $orig_img = imagecreatefromjpeg($filename);
605     break;
606    
607     case 'png':
608     $orig_img = imagecreatefrompng($filename);
609     break;
610    
611     default:
612     echo 'Error: Unrecognized image format.';
613     exit();
614     break;
615     }
616    
617     //Ok we have an Image, lets resize it.
618     $o_width = imagesx ($orig_img); // Original image width
619     $o_height = imagesy ($orig_img); // Original image height
620    
621     $img = imagecreate($this->_thumb_width, $this->_thumb_height);
622     imagecopyresized($img, $orig_img, 0, 0, 0, 0,
623     $this->_thumb_width, $this->_thumb_height,
624     $o_width, $o_height);
625     imagedestroy($orig_img);
626    
627     //Now lets write the image to disk
628     $this->write_thumb_to_disk( $file, $img, $type );
629     }
630     }
631    
632    
633     /**
634     * discover the type of image based off of the extension
635     *
636     * @param string $filename - the filename
637     */
638     function _get_file_type( $file ) {
639     $ext = explode('.', $file);
640     $ext = $ext[count($ext)-1];
641     switch( strtolower($ext) ) {
642     case 'jpeg' :
643     $type = 'jpg';
644     break;
645    
646     default :
647     $type = $ext;
648     break;
649     }
650    
651     return $type;
652     }
653    
654    
655     /**
656     * write the image to disk.
657     * We assume we have write permissions
658     * to the images dir.
659     *
660     */
661     function write_thumb_to_disk( $file, &$img, $type ) {
662    
663     $filename = $this->_thumbsdir . "/" . $file;
664    
665     //now lets write file to disk.
666     switch( strtolower($type) ) {
667     case 'jpg':
668     imagejpeg($img, $filename);
669     break;
670    
671     case 'png':
672     imagepng($img, $filename);
673     break;
674     }
675     imagedestroy($img);
676     }
677    
678     /**
679     * Try and create the thumbnail cache dir.
680     *
681     */
682     function _create_thumbnail_cache_dir() {
683     $oldmask = umask(0);
684     $ret = @mkdir($this->_thumbsdir, 0755);
685     @chmod($this->_thumbsdir, 0755);
686     umask($oldmask);
687     }
688     }
689    
690    
691     /**
692     * This class defines the css used by the
693     * FooterNav Object.
694     *
695     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
696     * @package phpHtmlLib
697     */
698     class ImageThumbnailWidgetCSS extends CSSBuilder {
699    
700     function user_setup() {
701 jonen 1.2 $this->add_entry(".imagethumbnail", "",
702 jonen 1.1 array("margin" => "0px 0px 0px 0px",
703     "font-family" => "arial, helvetica, sans-serif",
704     "font-size" => "10pt",
705     "border" => "1px solid #999999") );
706    
707 jonen 1.2 $this->add_entry(".imagethumbnail", "legend",
708 jonen 1.1 array("font-size" => "10pt",
709     "font-weight" => "bold",
710     "color" => "#000000"));
711    
712 jonen 1.2 $this->add_entry(".imagethumbnail", "a.thumblink:active,a.thumblink:link,a.thumblink:visited",
713 jonen 1.1 array("font-size" => "10pt",
714     "font-weight" => "bold",
715     "color" => "#999999",
716     "background" => "#FFFFFF"));
717    
718 jonen 1.2 $this->add_entry(".imagethumbnail", "a.thumblink:hover",
719 jonen 1.1 array("font-size" => "10pt",
720     "font-weight" => "bold",
721     "color" => "#4141FF",
722     "background" => "#eeeeee",
723     "text-decoration" => "none"));
724 jonen 1.2 $this->add_entry(".imagethumbnail", "table",
725 jonen 1.1 array("padding-left" => "5px"));
726     }
727     }
728     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed