upload.php 10 KB

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