Php >> Galeria zdjęć
Polimax

Drukarnia w Warszawie realizująca druk wizytówek, plakatów jak również fotoksiążki, fotoobrazy i fotokalendarze - z wysyłką na terenie całego kraju.

Opis

Oto przykład zastosowania php do stworzenia albumu zdjęć. Plik "photo_gallery.php" jest przykładowym szablonem strony www w której osadzono galerię zdjęć. Zbiór wszystkich zdjęć jest podzielony wg lat kalendarzowych, natomiast w kolejnych latach znajdują się różne kolekcje. Gość odwiedzajacy galerię wybiera i akceptuje rok oraz kolekcję z listy kolekcji znajdujących się w danym roku. Tworzenie nowej kolekcji polega na umieszczeniu plików zdjęć w odpowiednich folderach oraz ich opisów w odpowiednich plikach znajdujących się w folderze "collections"

The following is an example of php usage to create an image gallery. "photo_gallery.php" file is an exemplary website template containing a gallery. A collection of all pictures is divided into calendar years, whereas the following years contain various collections. An incoming guest picks and then accepts a year and a collection out of the collection list of the given year. New collection creation consists of inserting a number of picture files along with their descriptions in the corresponding folders and files in the "collections" folder, respectively.
<? php /* author:Józef Kańczugowski, www.teacher.webd.pl, Programy komputerowe dla nauczycieli, 26.05.2013 */ //support------------------------------------ function text_between_texts($a_text, $from_text, $to_text, $with_texts) { if ($from_text==='' || $to_text==='') return ''; $from=strpos($a_text,$from_text); $to=strpos($a_text,$to_text); if ($from===false || $to===false) return ''; $from_text_len=strlen($from_text); $to_text_len=strlen($to_text); if (!$with_texts) return substr($a_text,$from+$from_text_len,$to-$from-$from_text_len); else return substr($a_text,$from,$to+$to_text_len); } function text_delete_between_texts($a_text, $from_text, $to_text, $delete_with_texts) { $between=text_between_texts($a_text, $from_text, $to_text, $delete_with_texts); if ($between==='') return $a_text; $between_pos=strpos($a_text,$between); $between_len=strlen($between); $text_len=strlen($a_text); $before=''; if ($between_pos> 0) $before=substr($a_text,0,$between_pos); $after=''; if ($between_pos+$between_len<$text_len-1) $after=substr($a_text,$between_pos+$between_len,$text_len-$between_pos-$between_len); return $before.$after; } function text_before_str($a_text, $to_str, $with_str) { if ($to_str==='') return ''; $to_str_pos=strpos($a_text,$to_str); if ($to_str_pos===false) return ''; if ($with_str) return substr($a_text, 0, $to_str_pos+strlen($to_str)); else return substr($a_text, 0, $to_str_pos); } function text_after_str($a_text, $from_str, $with_str) { if ($from_str==='') return ''; $from_str_pos=strpos($a_text,$from_str); if ($from_str_pos===false) return ''; $from_str_len=strlen($from_str); $a_text_len=strlen($a_text); if ($with_str) return substr($a_text, $from_str_pos, $a_text_len-$from_str_pos); else return substr($a_text, $from_str_pos+$from_str_len, $a_text_len-$from_str_pos-$from_str_len); } function text_from_file($file_path) { if (!file_exists($file_path)) return ''; @$plik=fopen($file_path,'r'); if (!$plik) return ''; flock($plik, LOCK_SH); $a_text=fread($plik, filesize($file_path)); flock($plik, LOCK_UN); fclose($plik); return $a_text; } function files_in_dir($dir_path, &$files) { $files=array(); @$files_all=scandir($dir_path);if (!$files_all) return false; if (count($files_all)> 0) { $f=-1; foreach($files_all as $file) if($file != '.' && $file != '..') { $f++; $files[$f]=$file; } } return true; } //elementary problems---------------------------------------------- function comments_in_text_delete(&$a_text) { $a_text=text_delete_between_texts($a_text, '/*', '*/', true); } function row_start($i)//first photo index === 0 { return $i===0 || ($i+1)%4 === 1; } function row_end($i) { return ($i+1)%4 === 0; } function n_tab_write($tab_start,$n_tab) { $n=$tab_start+$n_tab; $result=''; for ($t=1; $t<=$n;$t++) $result= $result."\t"; return $result; } function year_from_dirname($dir_name) { @$result=(int)substr($dir_name,1,4); if (!$result) return -1; else return $result; } function number_delete($a_str) { if (trim($a_str)==='') return $a_str; $p=strlen($a_str); while((is_numeric(substr($a_str,$p-1,1)) || trim(substr($a_str,$p-1,1)==='')) && $p-1>=0) $p--; return substr($a_str,0,$p); } function captions_from_captions_file($captions_file, &amp;$captions) { $captions=array(); $captions_text=trim(text_from_file($captions_file)); if ($captions_text==='') return false; comments_in_text_delete($captions_text); $captions_text=trim($captions_text); if ($captions_text==='') return false; $c=-1; while (strpos($captions_text,'|')!==false) { $c++; $line=text_after_str($captions_text, '|', false); $captions_text=$line; $captions_text=text_after_str($captions_text, '|', true); if (strpos($line, '|')!==false) $line=text_before_str($line, '|', false); $line=number_delete($line); $captions[$c]=trim($line); } return count($captions)> 0; } function collections_in_year($year, &amp;$collections) { return captions_from_captions_file('collections/y'.$year.'/collections.txt',$collections); } function photos_in_collection($year, $collection_nr, &amp;$photos) { return captions_from_captions_file('collections/y'.$year.'/c'.$collection_nr.'/photos.txt',$photos); } function list_index_to_nr($list_index)//nr 1, 2, 3, ... list_index 0, 1, 2, ... { return (string)($list_index+1); } function nr_to_list_index($nr) { return (int)$nr-1; } function path_dir_collection($year, $collection_nr) { return 'collections/y'.$year.'/c'.$collection_nr.'/'; } function path_photo($year, $collection_nr, $photo_nr, $is_small_photo) { $dir_photos=path_dir_collection($year, $collection_nr); if ($is_small_photo) return $dir_photos.'min/p'.$photo_nr.'.jpg'; else return $dir_photos.'max/p'.$photo_nr.'.jpg'; } function unset_variables() { //year - id1 //id2 //photo_nr id3 //id4 small_photos ,large_photo if (isset($_SESSION['collection_nr'])) unset($_SESSION['collection_nr']); if (isset($_SESSION['what_show'])) unset($_SESSION['what_show']); } function up_variables() { if (isset($_POST['year_button']) &amp;& isset($_POST['years'])) { $_SESSION['year']=$_POST['years']; unset_variables(); } else { if (isset($_GET['id1'])) $_SESSION['year']=$_GET['id1']; if (isset($_GET['id2'])) $_SESSION['collection_nr']=$_GET['id2']; else if (isset($_POST['collections_button']) &amp;& isset($_POST['collections'])) $_SESSION['collection_nr']=$_POST['collections']; if (isset($_GET['id3'])) $_SESSION['photo_nr']=$_GET['id3']; if (isset($_GET['id4'])) $_SESSION['what_show']=$_GET['id4']; else if (isset($_POST['what_show']) &amp;& isset($_POST['collections_button'])) $_SESSION['what_show']=$_POST['what_show']; if (isset($_POST['year_button']) &amp;& isset($_POST['what_show'])) $_SESSION['what_show']=$_POST['what_show']; } if (isset($_SESSION['year']) &amp;& isset($_SESSION['collection_nr']) &amp;& (!isset($_GET['id4']))) $_SESSION['what_show']='small_photos'; } // fhotos show--------------------------------------------------- function small_photos_show($year, $collection_nr, $tab_start,&$photos) { if (!photos_in_collection($year, $collection_nr, $photos))return false; $photo_nr='';if (isset($_SESSION['photo_nr'])) $photo_nr=$_SESSION['photo_nr']; for ($i=0; $i<count($photos); $i++) { $file_path=path_photo($year, $collection_nr, list_index_to_nr($i), true); $image_size = getimagesize($file_path); $im_width=$image_size[0]; if ($im_width > 150) $im_width=150; $im_height=$image_size[1];if ($im_height > 150) $im_height=150; $text_height=180-$im_height; echo "\n"; if (row_start($i)) echo n_tab_write($tab_start,1).'<div class="photos_small_row">'."\n"; echo n_tab_write($tab_start,2).'<div class="photo_small_panel">'."\n"; echo n_tab_write($tab_start,3).'<div class="photo_small">'."\n"; echo n_tab_write($tab_start,4).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.list_index_to_nr($i).'&id4=large_photo'.' " style="width:'."$im_width".'px; height:'.$im_height.'px; background-image:url('.$file_path.');"'.'></a>'."\n"; echo n_tab_write($tab_start,3).'</div>'."\n"; if (list_index_to_nr($i)===$photo_nr) echo n_tab_write($tab_start,3).'<div class="photo_small_text" style= "background-color:rgb(180,201,233); border:1px solid white;">'."\n"; else echo n_tab_write($tab_start,3).'<div class="photo_small_text" style= "background-color:transparent;">'."\n"; echo n_tab_write($tab_start,4).'<a style="height:'.(string)$text_height.'px; width:'.(string)($im_width+2).'px;" href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.list_index_to_nr($i).'&id4=large_photo">'.$photos[$i].'</a>'."\n"; echo n_tab_write($tab_start,3).'</div>'."\n"; echo n_tab_write($tab_start,2).'</div>'."\n"; if (row_end($i) || $i===count($photos)-1) echo n_tab_write($tab_start,1).'</div>'."\n"; } return true; } function photo_large_show($year, $collection_nr, $photo_nr,$tab_start, &amp;$photos) { $path=path_photo($year, $collection_nr, $photo_nr,false); if (!file_exists($path)) return false; if (!photos_in_collection($year, $collection_nr, $photos)) return false; if (nr_to_list_index($photo_nr)>count($photos)-1) return false; $image_size = getimagesize($path); $im_width=$image_size[0]-10; if ($im_width> 810) $im_width=810; $im_height=$image_size[1]+15; echo "\n"; echo n_tab_write($tab_start,1).'<div class="photo" style="height:'.(string)$im_height.'px; background-image:url('.$path.');">'."\n"; echo n_tab_write($tab_start,2).'<div class="large_photo_close">'."\n"; echo n_tab_write($tab_start,3).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.$photo_nr.'&id4=small_photos"></a>'."\n"; echo n_tab_write($tab_start,2).'</div>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="photo_text" style="width:'.(string)$im_width.'px;">'."\n"; echo n_tab_write($tab_start,2).$photos[nr_to_list_index($photo_nr)]."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; return true; } function message_show($is_error,$a_text,$tab_start) { echo "\n"; if ($is_error) { echo n_tab_write($tab_start,1).'<div class="message_panel message_error" >'."\n"; echo n_tab_write($tab_start,2).$a_text."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } else { echo n_tab_write($tab_start,1).'<div class="message_panel message_inf" >'."\n"; echo n_tab_write($tab_start,2).$a_text."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } } function all_years(&$years) { if (!files_in_dir('collections/', $dirs)) return false; if (count($dirs)===0) return false; $years=array(); $j=-1; for ($i=0; $i<count($dirs);$i++) if (strpos($dirs[$i],'y')===0) { $j++; $years[$j]=(int)substr($dirs[$i],1,4); } if (!sort($years)) return false; if (count($years)===0) return false; return true; } function max_year_and_collection(&$year, &amp;$collection_nr) { if (!all_years($years)) return false; $year=(string)$years[count($years)-1]; if (!collections_in_year($year, $collections)) return false; $collection_nr=(string)(count($collections)); return true; } function photo_or_photos_show($tab_start, &amp;$photos) { if (!isset($_SESSION['year'])) { if (!max_year_and_collection($year, $collection_nr)) return false; else { $_SESSION['year']=$year; $_SESSION['collection_nr']=$collection_nr; $_SESSION['what_show']='small_photos'; return small_photos_show($year, $collection_nr, $tab_start, $photos); } } else { $year='';if (isset($_SESSION['year'])) $year=$_SESSION['year']; $collection_nr='';if (isset($_SESSION['collection_nr'])) $collection_nr=$_SESSION['collection_nr']; $photo_nr='';if (isset($_SESSION['photo_nr'])) $photo_nr=$_SESSION['photo_nr']; $small_ok=true; $large_ok=true; $small_show=false; $large_show=false; $small_show=isset($_SESSION['what_show']);if ($small_show) $small_show=$_SESSION['what_show']==='small_photos'; $large_show=isset($_SESSION['what_show']);if ($large_show) $large_show=$_SESSION['what_show']==='large_photo'; if ($small_show) $small_ok=small_photos_show($year, $collection_nr, $tab_start, $photos); else if ($large_show) $large_ok=photo_large_show($year, $collection_nr, $photo_nr,$tab_start, $photos); return $small_ok &amp;& $large_ok; } } // form -------------------------------------------------- function form_objects($tab_start, &amp;$collections) { if (!all_years($years)) return false; echo "\n"; echo n_tab_write($tab_start,1).'<select name="years" class="list" style="width:120px;">'."\n"; echo n_tab_write($tab_start,2).'<option value="'.'Rok - year'.'">Rok - year</option>'."\n"; $year_selected=''; for ($i=0; $i<count($years);$i++) { $option_selected=isset($_SESSION['year']) ; if ($option_selected) $option_selected=((string)$years[$i]===$_SESSION['year']); if ($option_selected) { $year_selected=(string)$years[$i]; echo n_tab_write($tab_start,2).'<option selected="selected" value="'.(string)$years[$i].'">'.$year_selected.'</option>'."\n"; } else echo n_tab_write($tab_start,2).'<option value="'.(string)$years[$i].'">'.(string)$years[$i].'</option>'."\n"; } echo n_tab_write($tab_start,1).'</select>'."\n"; echo n_tab_write($tab_start,1).'<input type="submit" value="" name="year_button" class="ok_button" />'."\n"; $is_collections=false; echo "\n"; echo n_tab_write($tab_start,1).'<select name="collections" class="list" style="width:540px;">'."\n"; echo n_tab_write($tab_start,2).'<option value="'.'Kolekcja - collection'.'">Kolekcja - collection</option>'."\n"; if ($year_selected!=='') { if (collections_in_year($year_selected,$collections)) { $is_collections=true; for ($i=0; $i<count($collections);$i++) { $option_selected=isset($_SESSION['collection_nr']); if ($option_selected) $option_selected=(list_index_to_nr($i)===$_SESSION['collection_nr']); if ($option_selected) echo n_tab_write($tab_start,2).'<option selected="selected" value="'.list_index_to_nr($i).'">'.$collections[$i].'</option>'."\n"; else echo n_tab_write($tab_start,2).'<option value="'.list_index_to_nr($i).'">'.$collections[$i].'</option>'."\n"; } } } echo n_tab_write($tab_start,1).'</select>'."\n"; if ($is_collections) echo n_tab_write($tab_start,1).'<input type="hidden" value="small_photos" name="what_show" />'."\n"; echo n_tab_write($tab_start,1).'<input type="submit" value="" name="collections_button" class="ok_button" />'."\n"; return true; } //photos back and next ------------------------------------- function back_next_arrows($tab_start, $collections, $photos) { $result=true; $year='';if (isset($_SESSION['year'])) $year=$_SESSION['year']; $collection_nr='';if (isset($_SESSION['collection_nr'])) $collection_nr=$_SESSION['collection_nr']; $photo_nr='';if (isset($_SESSION['photo_nr'])) $photo_nr=$_SESSION['photo_nr']; $small_show=isset($_SESSION['what_show']);if ($small_show) $small_show=$_SESSION['what_show']==='small_photos'; $large_show=isset($_SESSION['what_show']);if ($large_show) $large_show=$_SESSION['what_show']==='large_photo'; if ($small_show) $what_photo='small_photos'; else $what_photo='large_photo'; if (!$small_show &amp;& !$large_show) $result=false; if (($year==='' || $collection_nr==='')&& $small_show) $result=false; if (($year==='' || $collection_nr==='' || $photo_nr==='')&& $large_show) $result=false; if (!isset($collections)) $result=collections_in_year($year, $collections); if (!isset($photos)) $result=photos_in_collection($year, $collection_nr, $photos); $back_collection_ok=false; $next_collection_ok=false; $back_photo_ok=false; $next_photo_ok=false; if ($result) { if ($small_show) { $back_collection_ok=(int)$collection_nr> 1; $next_collection_ok=(int)$collection_nr<count($collections); } if ($large_show) { $back_photo_ok=(int)$photo_nr> 1; $next_photo_ok=(int)$photo_nr<count($photos); } } if (!$back_collection_ok &amp;& !$back_photo_ok) { echo n_tab_write($tab_start,1).'<div class="arrow start">'."\n"; echo n_tab_write($tab_start,2).'<a style="background-image:url('.'template_images/start_gray.png'.');" href="photo_gallery.php" tabindex="1"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow left">'."\n"; echo n_tab_write($tab_start,1).'<a style="background-image:url('.'template_images/arrow_left_gray.png'.');" href="photo_gallery.php" tabindex="3"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } else if ($back_collection_ok) { $collection_nr_back=(string)(((int)$collection_nr)-1); echo n_tab_write($tab_start,1).'<div class="arrow start">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2=1&id3=1&id4='.$what_photo.'" tabindex="1"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow left">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr_back.'&id3=1&id4='.$what_photo.'" tabindex="3"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } else if ($back_photo_ok) { $photo_nr_back=(string)(((int)$photo_nr)-1); echo n_tab_write($tab_start,1).'<div class="arrow start">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3=1&id4='.$what_photo.'" tabindex="1"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow left">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.$photo_nr_back.'&id4='.$what_photo.'" tabindex="3"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } if (!$next_collection_ok &amp;& !$next_photo_ok) { echo n_tab_write($tab_start,1).'<div class="arrow end">'."\n"; echo n_tab_write($tab_start,2).'<a style="background-image:url('.'template_images/end_gray.png'.');" href="photo_gallery.php" tabindex="2"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow right">'."\n"; echo n_tab_write($tab_start,1).'<a style="background-image:url('.'template_images/arrow_right_gray.png'.');" href="photo_gallery.php" tabindex="4"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } else if ($next_collection_ok) { $collection_nr_next=(string)(((int)$collection_nr)+1); $collection_nr_end=(string)count($collections); echo n_tab_write($tab_start,1).'<div class="arrow end">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr_end.'&id3=1&id4='.$what_photo.'" tabindex="2"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow right">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr_next.'&id3=1&id4='.$what_photo.'" tabindex="4"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } else if ($next_photo_ok) { $photo_nr_next=(string)(((int)$photo_nr)+1); $photo_nr_end=(string)count($photos); echo n_tab_write($tab_start,1).'<div class="arrow end">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.$photo_nr_end.'&id4='.$what_photo.'" tabindex="2"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; echo n_tab_write($tab_start,1).'<div class="arrow right">'."\n"; echo n_tab_write($tab_start,2).'<a href="photo_gallery.php?id1='.$year.'&id2='.$collection_nr.'&id3='.$photo_nr_next.'&id4='.$what_photo.'" tabindex="4"></a>'."\n"; echo n_tab_write($tab_start,1).'</div>'."\n"; } return $result; } function html_code_create($tab_start) { $message_text='Błędna struktura katalogów i plików lub brak zdjęć! - invalid directory structure or photos not exists!'; echo "\n"; echo n_tab_write($tab_start,2).'<div class="photos_panel">'."\n"; if (!photo_or_photos_show($tab_start+2,$photos)) message_show(true,$message_text,3); echo n_tab_write($tab_start,2).'</div>'."\n"; echo n_tab_write($tab_start,2).'<form action="photo_gallery.php" method="post">'."\n"; echo n_tab_write($tab_start,3).'<div class="photos_menu">'."\n"; if (!form_objects($tab_start+3,$collections)) message_show(true,$message_text,3); echo n_tab_write($tab_start,3).'</div>'."\n"; echo n_tab_write($tab_start,2).'</form>'."\n"; echo n_tab_write($tab_start,2).'<div class="photos_menu" style="height:50px;">'."\n"; echo n_tab_write($tab_start,3).'<div class="arrows">'."\n"; back_next_arrows($tab_start+3, $collections, $photos); echo n_tab_write($tab_start,3).'</div>'."\n"; echo n_tab_write($tab_start,2).'</div>'."\n"; } ?>

W pliku - file "photo_gallery.php":
<?php @session_start()?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"> <head> <title>Galeria zdjęć</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="styles.css" /> </head> <!--author:Józef Kańczugowski, www.teacher.webd.pl, Programy komputerowe dla nauczycieli, 26.05.2013--> <body> <?php include('functions_php.php'); up_variables(); ?> <div class="head_footer"> Przykładowy szablon strony www w której osadzono galerię zdjęć - example gallery website template </div> <div class="yellow_line"> </div> <div class="work_area"> <div class="left_menu"> Menu </div> <div class="photos_area"> <?php html_code_create(2); ?> </div> </div> <div class="yellow_line"> </div> <div class="head_footer"> Stopka - footer </div> </body> </html>