upload.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * @name Upload Module
  4. * @author Philipp Maurer
  5. * @author Tobias Reich
  6. * @copyright 2014 by Philipp Maurer, Tobias Reich
  7. */
  8. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  9. function upload($files, $albumID) {
  10. global $database;
  11. switch($albumID) {
  12. // s for public (share)
  13. case 's':
  14. $public = 1;
  15. $star = 0;
  16. $albumID = 0;
  17. break;
  18. // f for starred (fav)
  19. case 'f':
  20. $star = 1;
  21. $public = 0;
  22. $albumID = 0;
  23. break;
  24. default:
  25. $star = 0;
  26. $public = 0;
  27. }
  28. foreach ($files as $file) {
  29. $id = str_replace('.', '', microtime(true));
  30. while(strlen($id)<14) $id .= 0;
  31. $tmp_name = $file["tmp_name"];
  32. $type = getimagesize($tmp_name);
  33. if (($type[2]!=1)&&($type[2]!=2)&&($type[2]!=3)) return false;
  34. $data = array_reverse(explode('.', $file["name"]));
  35. $data = $data[0];
  36. $photo_name = md5($id) . ".$data";
  37. // Import if not uploaded via web
  38. if (!is_uploaded_file($tmp_name)) {
  39. if (copy($tmp_name, "../uploads/big/" . $photo_name)) {
  40. unlink($tmp_name);
  41. $import_name = $tmp_name;
  42. }
  43. } else {
  44. move_uploaded_file($tmp_name, "../uploads/big/" . $photo_name);
  45. $import_name = "";
  46. }
  47. // Read infos
  48. $info = getInfo($photo_name);
  49. // Set orientation based on EXIF data
  50. if (isset($info['orientation'])&&isset($info['width'])&&isset($info['height'])) {
  51. if ($info['orientation']==3||$info['orientation']==6||$info['orientation']==8) {
  52. $newWidth = $info['width'];
  53. $newHeight = $info['height'];
  54. $sourceImg = imagecreatefromjpeg("../uploads/big/$photo_name");
  55. switch($info['orientation']){
  56. case 2:
  57. // mirror
  58. // not yet implemented
  59. break;
  60. case 3:
  61. $sourceImg = imagerotate($sourceImg, -180, 0);
  62. break;
  63. case 4:
  64. // rotate 180 and mirror
  65. // not yet implemented
  66. break;
  67. case 5:
  68. // rotate 90 and mirror
  69. // not yet implemented
  70. break;
  71. case 6:
  72. $sourceImg = imagerotate($sourceImg, -90, 0);
  73. $newWidth = $info['height'];
  74. $newHeight = $info['width'];
  75. break;
  76. case 7:
  77. // rotate -90 and mirror
  78. // not yet implemented
  79. break;
  80. case 8:
  81. $sourceImg = imagerotate($sourceImg, 90, 0);
  82. $newWidth = $info['height'];
  83. $newHeight = $info['width'];
  84. break;
  85. }
  86. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  87. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  88. imagejpeg($newSourceImg, "../uploads/big/$photo_name", 100);
  89. }
  90. }
  91. // Create Thumb
  92. if (!createThumb($photo_name)) return false;
  93. // Save to DB
  94. $query = "INSERT INTO lychee_photos (id, title, url, description, type, width, height, size, sysdate, systime, iso, aperture, make, model, shutter, focal, takedate, taketime, thumbUrl, album, public, star, import_name)
  95. VALUES (
  96. '" . $id . "',
  97. '" . $info['title'] . "',
  98. '" . $photo_name . "',
  99. '" . $info['description'] . "',
  100. '" . $info['type'] . "',
  101. '" . $info['width'] . "',
  102. '" . $info['height'] . "',
  103. '" . $info['size'] . "',
  104. '" . $info['date'] . "',
  105. '" . $info['time'] . "',
  106. '" . $info['iso'] . "',
  107. '" . $info['aperture'] . "',
  108. '" . $info['make'] . "',
  109. '" . $info['model'] . "',
  110. '" . $info['shutter'] . "',
  111. '" . $info['focal'] . "',
  112. '" . $info['takeDate'] . "',
  113. '" . $info['takeTime'] . "',
  114. '" . md5($id) . ".jpeg',
  115. '" . $albumID . "',
  116. '" . $public . "',
  117. '" . $star . "',
  118. '" . $import_name . "');";
  119. $result = $database->query($query);
  120. if (!$result) return false;
  121. }
  122. return true;
  123. }
  124. function getInfo($filename) {
  125. global $database;
  126. $url = '../uploads/big/' . $filename;
  127. $iptcArray = array();
  128. $info = getimagesize($url, $iptcArray);
  129. // General information
  130. $return['type'] = $info['mime'];
  131. $return['width'] = $info[0];
  132. $return['height'] = $info[1];
  133. $return['date'] = date('d.m.Y', filectime($url));
  134. $return['time'] = date('H:i:s', filectime($url));
  135. // Size
  136. $size = filesize($url)/1024;
  137. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  138. else $return['size'] = round($size, 1) . ' KB';
  139. // IPTC Metadata Fallback
  140. $return['title'] = '';
  141. $return['description'] = '';
  142. // IPTC Metadata
  143. if(isset($iptcArray['APP13'])) {
  144. $iptcInfo = iptcparse($iptcArray['APP13']);
  145. if (is_array($iptcInfo)) {
  146. $temp = $iptcInfo['2#105'][0];
  147. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  148. $temp = $iptcInfo['2#120'][0];
  149. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  150. }
  151. }
  152. // EXIF Metadata Fallback
  153. $return['orientation'] = '';
  154. $return['iso'] = '';
  155. $return['aperture'] = '';
  156. $return['make'] = '';
  157. $return['model'] = '';
  158. $return['shutter'] = '';
  159. $return['focal'] = '';
  160. $return['takeDate'] = '';
  161. $return['takeTime'] = '';
  162. // EXIF Metadata
  163. if ($info['mime']=='image/jpeg'&&function_exists('exif_read_data')&&@exif_read_data($url, 'EXIF', 0)) {
  164. $exif = exif_read_data($url, 'EXIF', 0);
  165. $temp = $exif['Orientation'];
  166. if (isset($temp)) $return['orientation'] = $temp;
  167. $temp = $exif['ISOSpeedRatings'];
  168. if (isset($temp)) $return['iso'] = $temp;
  169. $temp = $exif['COMPUTED']['ApertureFNumber'];
  170. if (isset($temp)) $return['aperture'] = $temp;
  171. $temp = $exif['Make'];
  172. if (isset($temp)) $return['make'] = $exif['Make'];
  173. $temp = $exif['Model'];
  174. if (isset($temp)) $return['model'] = $temp;
  175. $temp = $exif['ExposureTime'];
  176. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  177. $temp = $exif['FocalLength'];
  178. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  179. $temp = $exif['DateTimeOriginal'];
  180. if (isset($temp)) {
  181. $exifDate = explode(' ', $temp);
  182. $date = explode(':', $exifDate[0]);
  183. $return['takeDate'] = $date[2].'.'.$date[1].'.'.$date[0];
  184. $return['takeTime'] = $exifDate[1];
  185. }
  186. }
  187. // Security
  188. foreach(array_keys($return) as $key) $return[$key] = mysqli_real_escape_string($database, $return[$key]);
  189. return $return;
  190. }
  191. function createThumb($filename, $width = 200, $height = 200) {
  192. global $settings;
  193. $url = "../uploads/big/$filename";
  194. $info = getimagesize($url);
  195. $photoName = explode(".", $filename);
  196. $newUrl = "../uploads/thumb/".$photoName[0].".jpeg";
  197. $newUrl2x = "../uploads/thumb/".$photoName[0]."@2x.jpeg";
  198. // Set position and size
  199. $thumb = imagecreatetruecolor($width, $height);
  200. $thumb2x = imagecreatetruecolor($width*2, $height*2);
  201. if ($info[0]<$info[1]) {
  202. $newSize = $info[0];
  203. $startWidth = 0;
  204. $startHeight = $info[1]/2 - $info[0]/2;
  205. } else {
  206. $newSize = $info[1];
  207. $startWidth = $info[0]/2 - $info[1]/2;
  208. $startHeight = 0;
  209. }
  210. // Fallback for older version
  211. if ($info['mime']==='image/webp'&&floatval(phpversion())<5.5) return false;
  212. // Create new image
  213. switch($info['mime']) {
  214. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  215. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  216. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  217. case 'image/webp': $sourceImg = imagecreatefromwebp($url); break;
  218. default: return false;
  219. }
  220. imagecopyresampled($thumb,$sourceImg,0,0,$startWidth,$startHeight,$width,$height,$newSize,$newSize);
  221. imagecopyresampled($thumb2x,$sourceImg,0,0,$startWidth,$startHeight,$width*2,$height*2,$newSize,$newSize);
  222. imagejpeg($thumb,$newUrl,$settings['thumbQuality']);
  223. imagejpeg($thumb2x,$newUrl2x,$settings['thumbQuality']);
  224. return true;
  225. }
  226. function importPhoto($name, $albumID = 0) {
  227. $tmp_name = "../uploads/import/$name";
  228. $info = getimagesize($tmp_name);
  229. $size = filesize($tmp_name);
  230. $nameFile = array(array());
  231. $nameFile[0]['name'] = $name;
  232. $nameFile[0]['type'] = $info['mime'];
  233. $nameFile[0]['tmp_name'] = $tmp_name;
  234. $nameFile[0]['error'] = 0;
  235. $nameFile[0]['size'] = $size;
  236. if (upload($nameFile, $albumID)) return true;
  237. return false;
  238. }
  239. function importUrl($url, $albumID = 0) {
  240. if (strpos($url, ',')!==false) {
  241. // Multiple photos
  242. $url = explode(',', $url);
  243. foreach ($url as &$key) {
  244. $key = str_replace(' ', '%20', $key);
  245. if (@getimagesize($key)) {
  246. $pathinfo = pathinfo($key);
  247. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  248. $tmp_name = "../uploads/import/$filename";
  249. copy($key, $tmp_name);
  250. }
  251. }
  252. return importServer($albumID);
  253. } else {
  254. // One photo
  255. $url = str_replace(' ', '%20', $url);
  256. if (@getimagesize($url)) {
  257. $pathinfo = pathinfo($url);
  258. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  259. $tmp_name = "../uploads/import/$filename";
  260. copy($url, $tmp_name);
  261. return importPhoto($filename, $albumID);
  262. }
  263. }
  264. return false;
  265. }
  266. function importServer($albumID = 0) {
  267. global $database;
  268. $i = 0;
  269. $files = glob('../uploads/import/*');
  270. foreach ($files as $file) {
  271. if (@getimagesize($file)) {
  272. if (!importPhoto(basename($file), $albumID)) return false;
  273. $i++;
  274. }
  275. }
  276. if ($i===0) return "Warning: Folder empty!";
  277. return true;
  278. }
  279. ?>