honor jpeg orientation
[bizou.git] / functions.php
1 <?php
2
3 // load plugins
4 $plugins = array();
5 if (is_dir("plugins")) {
6         $plugins = scandir("plugins");
7         array_shift($plugins); array_shift($plugins); // remove . and ..
8         foreach ($plugins as $p) if (is_file("plugins/$p/functions.php"))
9                 require "plugins/$p/functions.php";
10 }
11
12 function plugins_include($phpFile)
13 {
14         foreach ($GLOBALS['plugins'] as $p) if (is_file("plugins/$p/$phpFile"))
15                 require "plugins/$p/$phpFile";
16 }
17
18 function getPathInfo()
19 {
20         $simplePath = $_SERVER["PATH_INFO"];
21         if ($simplePath == '/') $simplePath = '';
22         // extra security check to avoid /photos/index/../.. like urls, maybe useless but..
23         if (strpos($simplePath, '..') !== false) die(".. found in url");
24         return $simplePath;
25 }
26
27
28 if (! function_exists('getImageLink')) {
29 function getImageLink($imageSimplePath)
30 {
31         return $GLOBALS['rootUrl'].IMAGES_DIR.$imageSimplePath;
32 }
33 }
34
35 if (! function_exists('getPreview')) {
36 function getPreview($imgFile, $maxSize = THUMB_SIZE)
37 {
38         # example: data/myalbum/100.mypic.jpg
39         $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile);
40         
41         # if the preview is a symlink, image is already good sized
42         if (is_link($newImgFile)) return $imgFile;
43         
44         if (! is_file($newImgFile))
45         {
46                 # this tels the template to flush output after displaying previews
47                 $GLOBALS["generating"] = true;
48
49                 # reset script time limit to 20s (wont work in safe mode)
50                 set_time_limit(20);
51
52                 $ext = strtolower(substr($imgFile, -4));
53                 if ($ext == ".jpg") {
54                         $img = imagecreatefromjpeg($imgFile);
55                         $exif = exif_read_data($imgFile);
56                 } else {
57                         $img = imagecreatefrompng($imgFile);
58                 }
59                 if ($img === false) return ""; #read error (wrong permission...)
60
61                 $w = imagesx($img);
62                 $h = imagesy($img);
63                 # if the image is already small, make a symlink, and return it
64                 if ($w <= $maxSize and $h <= $maxSize) {
65                         imagedestroy($img);
66                         symlink($imgFile, $newImgFile);
67                         return $imgFile;
68                 }
69
70                 # config to allow group writable files
71                 umask(DATA_UMASK);
72                 # create the thumbs directory recursively
73                 if (! is_dir(dirname($newImgFile))) @mkdir(dirname($newImgFile), 0777, true)
74                         or die("Could not write in data dir. Please fix permissions.");
75
76                 if ($w > $h) {
77                         $newW = $maxSize;
78                         $newH = $h/($w/$maxSize);
79                 } else {
80                         $newW = $w/($h/$maxSize);
81                         $newH = $maxSize;
82                 }
83
84                 $newImg = imagecreatetruecolor($newW, $newH);
85
86                 imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newW, $newH, $w, $h);
87
88                 if ($ext == ".jpg") {
89                         if (!empty($exif['Orientation'])) {
90                                 $orientation = $exif['Orientation'];
91                                 if     ($orientation === 3) $newImg = imagerotate($newImg, 180, 0);
92                                 elseif ($orientation === 6) $newImg = imagerotate($newImg, -90, 0);
93                                 elseif ($orientation === 8) $newImg = imagerotate($newImg, 90, 0);
94                         }
95                         imagejpeg($newImg, $newImgFile, JPEG_QUALITY);
96                 } else {
97                         imagepng($newImg, $newImgFile);
98                 }
99                 
100                 imagedestroy($img);
101                 imagedestroy($newImg);
102         }
103
104         return $newImgFile;
105 }
106 }
107
108 function getAlbumPreview($dir)
109 {
110         $previewFile = DATA_DIR."/$dir/albumpreview";
111
112         if (is_file("$previewFile.jpg")) {
113                 return "$previewFile.jpg";
114         } else if (is_file("$previewFile.empty")) {
115                 return "";
116         } else if (is_file("$previewFile.png")) {
117                 return "$previewFile.png";
118         } else {
119                 # config to allow group writable files
120                 umask(DATA_UMASK);
121                 # create the thumbs directory recursively
122                 if (! is_dir(dirname($previewFile))) @mkdir(dirname($previewFile), 0777, true)
123                         or die("Could not write in data dir. Please fix permissions.");
124
125                 // no preview: look for a preview in current dir, write it, return it
126                 foreach (scandir($dir) as $file) if ($file[0] != '.') {
127                         $ext = strtolower(substr($file, -4));
128                         if ($ext == ".jpg" or $ext == ".png") {
129                                 $thumb = getPreview("$dir/$file");
130                                 copy($thumb, $previewFile.$ext);
131                                 return $previewFile.$ext;
132                         } else if (is_dir("$dir/$file")) {
133                                 $subPreview = getAlbumPreview("$dir/$file");
134                                 if ($subPreview) {
135                                         $myPreview = dirname($previewFile)."/".basename($subPreview);
136                                         copy($subPreview, $myPreview);
137                                         return $myPreview;
138                                 }
139                         }
140                 }
141
142                 // nothing found. create empty file
143                 touch("$previewFile.empty");
144                 return "";
145         }
146 }
147
148 ?>