upload.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. $settings['importFilename']==='1')
  54. $info['title'] = mysqli_real_escape_string($database, substr(str_replace(".$extension", '', $file['name']), 0, 30));
  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, 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. '" . $info['description'] . "',
  106. '" . $info['type'] . "',
  107. '" . $info['width'] . "',
  108. '" . $info['height'] . "',
  109. '" . $info['size'] . "',
  110. '" . $info['date'] . "',
  111. '" . $info['time'] . "',
  112. '" . $info['iso'] . "',
  113. '" . $info['aperture'] . "',
  114. '" . $info['make'] . "',
  115. '" . $info['model'] . "',
  116. '" . $info['shutter'] . "',
  117. '" . $info['focal'] . "',
  118. '" . $info['takeDate'] . "',
  119. '" . $info['takeTime'] . "',
  120. '" . md5($id) . ".jpeg',
  121. '" . $albumID . "',
  122. '" . $public . "',
  123. '" . $star . "',
  124. '" . $import_name . "');";
  125. $result = $database->query($query);
  126. if (!$result) return false;
  127. }
  128. return true;
  129. }
  130. function getInfo($filename) {
  131. global $database;
  132. $url = '../uploads/big/' . $filename;
  133. $iptcArray = array();
  134. $info = getimagesize($url, $iptcArray);
  135. // General information
  136. $return['type'] = $info['mime'];
  137. $return['width'] = $info[0];
  138. $return['height'] = $info[1];
  139. $return['date'] = date('d.m.Y', filectime($url));
  140. $return['time'] = date('H:i:s', filectime($url));
  141. // Size
  142. $size = filesize($url)/1024;
  143. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  144. else $return['size'] = round($size, 1) . ' KB';
  145. // IPTC Metadata Fallback
  146. $return['title'] = '';
  147. $return['description'] = '';
  148. // IPTC Metadata
  149. if(isset($iptcArray['APP13'])) {
  150. $iptcInfo = iptcparse($iptcArray['APP13']);
  151. if (is_array($iptcInfo)) {
  152. $temp = $iptcInfo['2#105'][0];
  153. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  154. $temp = $iptcInfo['2#120'][0];
  155. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  156. }
  157. }
  158. // EXIF Metadata Fallback
  159. $return['orientation'] = '';
  160. $return['iso'] = '';
  161. $return['aperture'] = '';
  162. $return['make'] = '';
  163. $return['model'] = '';
  164. $return['shutter'] = '';
  165. $return['focal'] = '';
  166. $return['takeDate'] = '';
  167. $return['takeTime'] = '';
  168. // EXIF Metadata
  169. if ($info['mime']=='image/jpeg'&&function_exists('exif_read_data')&&@exif_read_data($url, 'EXIF', 0)) {
  170. $exif = exif_read_data($url, 'EXIF', 0);
  171. $temp = $exif['Orientation'];
  172. if (isset($temp)) $return['orientation'] = $temp;
  173. $temp = $exif['ISOSpeedRatings'];
  174. if (isset($temp)) $return['iso'] = $temp;
  175. $temp = $exif['COMPUTED']['ApertureFNumber'];
  176. if (isset($temp)) $return['aperture'] = $temp;
  177. $temp = $exif['Make'];
  178. if (isset($temp)) $return['make'] = $exif['Make'];
  179. $temp = $exif['Model'];
  180. if (isset($temp)) $return['model'] = $temp;
  181. $temp = $exif['ExposureTime'];
  182. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  183. $temp = $exif['FocalLength'];
  184. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  185. $temp = $exif['DateTimeOriginal'];
  186. if (isset($temp)) {
  187. $exifDate = explode(' ', $temp);
  188. $date = explode(':', $exifDate[0]);
  189. $return['takeDate'] = $date[2].'.'.$date[1].'.'.$date[0];
  190. $return['takeTime'] = $exifDate[1];
  191. }
  192. }
  193. // Security
  194. foreach(array_keys($return) as $key) $return[$key] = mysqli_real_escape_string($database, $return[$key]);
  195. return $return;
  196. }
  197. function createThumb($filename, $width = 200, $height = 200) {
  198. global $settings;
  199. $url = "../uploads/big/$filename";
  200. $info = getimagesize($url);
  201. $photoName = explode(".", $filename);
  202. $newUrl = "../uploads/thumb/$photoName[0].jpeg";
  203. $newUrl2x = "../uploads/thumb/$photoName[0]@2x.jpeg";
  204. // Set position and size
  205. $thumb = imagecreatetruecolor($width, $height);
  206. $thumb2x = imagecreatetruecolor($width*2, $height*2);
  207. if ($info[0]<$info[1]) {
  208. $newSize = $info[0];
  209. $startWidth = 0;
  210. $startHeight = $info[1]/2 - $info[0]/2;
  211. } else {
  212. $newSize = $info[1];
  213. $startWidth = $info[0]/2 - $info[1]/2;
  214. $startHeight = 0;
  215. }
  216. // Fallback for older version
  217. if ($info['mime']==='image/webp'&&floatval(phpversion())<5.5) return false;
  218. // Create new image
  219. switch($info['mime']) {
  220. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  221. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  222. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  223. case 'image/webp': $sourceImg = imagecreatefromwebp($url); break;
  224. default: return false;
  225. }
  226. imagecopyresampled($thumb,$sourceImg,0,0,$startWidth,$startHeight,$width,$height,$newSize,$newSize);
  227. imagecopyresampled($thumb2x,$sourceImg,0,0,$startWidth,$startHeight,$width*2,$height*2,$newSize,$newSize);
  228. imagejpeg($thumb,$newUrl,$settings['thumbQuality']);
  229. imagejpeg($thumb2x,$newUrl2x,$settings['thumbQuality']);
  230. return true;
  231. }
  232. function importPhoto($name, $albumID = 0) {
  233. $tmp_name = "../uploads/import/$name";
  234. $info = getimagesize($tmp_name);
  235. $size = filesize($tmp_name);
  236. $nameFile = array(array());
  237. $nameFile[0]['name'] = $name;
  238. $nameFile[0]['type'] = $info['mime'];
  239. $nameFile[0]['tmp_name'] = $tmp_name;
  240. $nameFile[0]['error'] = 0;
  241. $nameFile[0]['size'] = $size;
  242. if (upload($nameFile, $albumID)) return true;
  243. return false;
  244. }
  245. function importUrl($url, $albumID = 0) {
  246. if (strpos($url, ',')!==false) {
  247. // Multiple photos
  248. $url = explode(',', $url);
  249. foreach ($url as &$key) {
  250. $key = str_replace(' ', '%20', $key);
  251. if (@getimagesize($key)) {
  252. $pathinfo = pathinfo($key);
  253. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  254. $tmp_name = "../uploads/import/$filename";
  255. copy($key, $tmp_name);
  256. }
  257. }
  258. return importServer($albumID);
  259. } else {
  260. // One photo
  261. $url = str_replace(' ', '%20', $url);
  262. if (@getimagesize($url)) {
  263. $pathinfo = pathinfo($url);
  264. $filename = $pathinfo['filename'].".".$pathinfo['extension'];
  265. $tmp_name = "../uploads/import/$filename";
  266. copy($url, $tmp_name);
  267. return importPhoto($filename, $albumID);
  268. }
  269. }
  270. return false;
  271. }
  272. function importServer($albumID = 0) {
  273. global $database;
  274. $i = 0;
  275. $files = glob('../uploads/import/*');
  276. foreach ($files as $file) {
  277. if (@getimagesize($file)) {
  278. if (!importPhoto(basename($file), $albumID)) return false;
  279. $i++;
  280. }
  281. }
  282. if ($i===0) return "Warning: Folder empty!";
  283. return true;
  284. }
  285. ?>