{_IMAGE_ALT_}
_CONTENT_; $default_html_template=<<<_CONTENT_ Photo Gallery
{_PHOTO_CONTENT_}
_CONTENT_; class PhotoGallery { private $dirname; private $thumbnail_scale=0.065; private $image_scale=0.275; private $thumbnails_dir="thumbnails"; private $full_thumbnails_dir; private $images_dir="images"; private $full_images_dir; private $pages_dir="pages"; private $full_pages_dir; private $original_dir="original"; private $homepage="default"; //filename with no extension, index or default private $dh; private $number_of_thumbnails_per_row=5; private $number_of_thumbnails_per_page=25; private $html_content; private $xml_filename='photos.xml'; private $full_xml_path; function __construct($dirname) { if(!is_dir($dirname)) { die("Error reading $dirname.\r\n"); } $this->dirname=realpath($dirname); $this->original_dir=$this->dirname."/".$this->original_dir; $this->dh=opendir($this->original_dir); if(!$this->dh) { die("Error reading defined directory {$this->dirname}.\r\n"); } $this->full_thumbnails_dir=$this->dirname."/".$this->thumbnails_dir; $this->full_images_dir=$this->dirname."/".$this->images_dir; $this->full_pages_dir=$this->dirname."/".$this->pages_dir; $this->full_xml_path=$this->dirname."/".$this->xml_filename; system("rm -f {$this->dirname}/*.html {$this->dirname}/*.xml"); print "{$this->dirname}/*.html is deleted.\r\n"; if(file_exists($this->full_thumbnails_dir)) { system("rm -rf {$this->full_thumbnails_dir}"); print "{$this->full_thumbnails_dir} JPG files are deleted.\r\n"; } mkdir($this->full_thumbnails_dir,0755); print "Create {$this->full_thumbnails_dir}.\r\n"; if(file_exists($this->full_images_dir)) { system("rm -rf {$this->full_images_dir}"); print "{$this->full_images_dir} JPG files are deleted.\r\n"; } mkdir($this->full_images_dir,0755); print "Create {$this->full_images_dir}.\r\n"; if(file_exists($this->full_pages_dir)) { system("rm -rf {$this->full_pages_dir}"); print "{$this->full_pages_dir} html files are deleted.\r\n"; } mkdir($this->full_pages_dir,0755); print "Create {$this->full_pages_dir}.\r\n"; } function __destruct() { if($this->dh) { closedir($this->dh); } } function set_number_of_thumbnails_per_row($number) { $this->number_of_thumbnails_per_row=$number; } function set_number_of_thumbnails_per_page($number) { $this->number_of_thumbnails_per_page=$number; } function set_thumbnail_scale($new_scale) { $this->thumbnail_scale=$new_scale; } function set_image_scale($new_scale) { $this->image_scale=$new_scale; } function create_photo_gallery() { global $default_html_template,$image_page_template; $xml_fp=fopen($this->full_xml_path,"w"); $xml_content=""; $xml_content.=""; $i=0; while(($filename=readdir($this->dh))!==false) { $new_filename=$this->original_dir."/".$filename; if(!is_file($new_filename)) { continue; } $path=pathinfo($new_filename); if(strtolower($path['extension'])!='jpg') { continue; } $arr_images[$i]['path']=$new_filename; $arr_images[$i]['filename']=$filename; list($width,$height,$type,$attr)=getimagesize($new_filename); $arr_images[$i]['width']=$width; $arr_images[$i]['height']=$height; $arr_images[$i]['thumbnail_width']=$width*$this->thumbnail_scale; $arr_images[$i]['thumbnail_height']=$height*$this->thumbnail_scale; $arr_images[$i]['image_width']=$width*$this->image_scale; $arr_images[$i]['image_height']=$height*$this->image_scale; $xml_content.=""; $xml_content.="$filename"; $xml_content.="{$arr_images[$i]['image_width']}"; $xml_content.="{$arr_images[$i]['image_height']}"; $xml_content.=""; $i++; } usort($arr_images,"cmp"); if(count($arr_images)==0) { return; } $number_of_pages=ceil(count($arr_images)/$this->number_of_thumbnails_per_page); print "There will be $number_of_pages default html pages.\r\n"; for($i=0;$i<$number_of_pages;$i++) { if($i==0) { $arr_homepage[$i]=$this->homepage.".html"; } else { $arr_homepage[$i]=$this->homepage."_$i.html"; } } $file_index=0; $number_photos_exported=0; $content=""; for($i=0;$i<=count($arr_images);$i++) { if($number_photos_exported==$this->number_of_thumbnails_per_page || $i==count($arr_images)) { $number_photos_exported=0; $homepage_fp=fopen("{$this->dirname}/{$arr_homepage[$file_index]}","w"); if(!$homepage_fp) { die("Error to create {$arr_homepage[$file_index]}.\r\n"); } if(count($arr_homepage)>0) { $page_navigator="
"; $slideshow="Start Slide Show"; if($file_index==0) { $page_navigator.=""; } else if($file_index==count($arr_homepage)-1) { $page_navigator.=""; } else { $page_navigator.=""; $page_navigator.=""; } $page_navigator.="
 $slideshowNext Page
Previous Page$slideshow 
Previous Page$slideshowNext Page


"; } if($i%$this->number_of_thumbnails_per_row!=1) { $content.=""; } $content.=""; $content=$page_navigator.$content."

".$page_navigator; $this->html_content=str_replace("{_PHOTO_CONTENT_}",$content,$default_html_template); fputs($homepage_fp,$this->html_content); fclose($homepage_fp); system("chmod 644 {$this->dirname}/{$arr_homepage[$file_index]}"); print "Create {$arr_homepage[$file_index]}.\r\n"; $file_index++; if($i==count($arr_images)) break; $content=""; } if($i==0) { $content.=""; } else if($i%$this->number_of_thumbnails_per_row==0) { $content.=""; $content.=""; } $new_filename=$arr_images[$i]['path']; $filename=$arr_images[$i]['filename']; $width=$arr_images[$i]['width']; $height=$arr_images[$i]['height']; //create thumbnails $thumbnail_width=$arr_images[$i]['thumbnail_width']; $thumbnail_height=$arr_images[$i]['thumbnail_height']; $image_width=$arr_images[$i]['image_width']; $image_height=$arr_images[$i]['image_height']; $im=imagecreatefromjpeg($new_filename); $im_thumbnail=imagecreatetruecolor($thumbnail_width,$thumbnail_height); $im_image=imagecreatetruecolor($image_width,$image_height); if(!$im || !$im_thumbnail || !$im_image) { die("Error to open $new_filename for thumbnail and image.\r\n"); } imagecopyresized($im_thumbnail,$im,0,0,0,0,$thumbnail_width,$thumbnail_height,$width,$height); imagejpeg($im_thumbnail,$this->full_thumbnails_dir."/".$filename); system("chmod 644 {$this->full_thumbnails_dir}/$filename"); print "Create {$this->full_thumbnails_dir}/$filename.\r\n"; imagecopyresized($im_image,$im,0,0,0,0,$image_width,$image_height,$width,$height); imagejpeg($im_image,$this->full_images_dir."/".$filename); system("chmod 644 {$this->full_images_dir}/$filename"); print "Create {$this->full_images_dir}/$filename.\r\n"; $content.=""; $fp_page=fopen("{$this->full_pages_dir}/$filename.html","w"); if(!$fp_page) { die("Error to open {$this->full_pages_dir}/$filename.html.\r\n"); } $arr_pattern=array("{_IMAGE_NAME_}", "{_PREVIOUS_}", "{_NEXT_}", "{_IMAGE_SOURCE}", "{_IMAGE_ALT_}", "{_IMAGE_WIDTH_}", "{_IMAGE_HEIGHT_}" ); $previous_html=($i>0)?"Previous Photo  ":""; $next_html=($iNext Photo":""; $arr_replace=array($filename, $previous_html, $next_html, $filename, $filename, $image_width, $image_height ); $image_page_content=str_replace($arr_pattern,$arr_replace,$image_page_template); fputs($fp_page,$image_page_content); fclose($fp_page); system("chmod 644 {$this->full_pages_dir}/$filename.html"); print "Create {$this->full_pages_dir}/$filename.html.\r\n"; imagedestroy($im); imagedestroy($im_thumbnail); imagedestroy($im_image); $number_photos_exported++; } //end of while loop $xml_content.=""; fputs($xml_fp,$xml_content); fclose($xml_fp); system("chmod 644 {$this->full_xml_path}"); print "xml file is created.\r\n"; system("wget -O {$this->dirname}/slideshow.html http://violet.ima.umn.edu/jing/images/slideshow.html"); system("chmod 644 {$this->dirname}/slideshow.html"); print "Create {$this->dirname}/slideshow.html.\r\n"; system("chmod 755 {$this->full_thumbnails_dir}"); system("chmod 755 {$this->full_images_dir}"); system("chmod 755 {$this->full_pages_dir}"); //system("chmod -R 755 {$this->dirname}"); }//end of function create_photo_gallery }//end of definition of class $pg=new PhotoGallery($argv[1]); if($argv[2]!="" && $argv[3]!="") { $pg->set_thumbnail_scale($argv[2]); $pg->set_image_scale($argv[3]); } $pg->create_photo_gallery(); ?>