upload.php 9.7 KB

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