Photo.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. ###
  3. # @name Photo Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Photo extends Module {
  9. private $database = null;
  10. private $settings = null;
  11. private $photoIDs = null;
  12. public function __construct($database, $plugins, $settings, $photoIDs) {
  13. # Init vars
  14. $this->database = $database;
  15. $this->plugins = $plugins;
  16. $this->settings = $settings;
  17. $this->photoIDs = $photoIDs;
  18. return true;
  19. }
  20. public function add($files, $albumID, $description = '', $tags = '') {
  21. if (!isset($this->database)) return false;
  22. # Call plugins
  23. $this->plugins(__METHOD__, 0, func_get_args());
  24. switch($albumID) {
  25. case 's':
  26. # s for public (share)
  27. $public = 1;
  28. $star = 0;
  29. $albumID = 0;
  30. break;
  31. case 'f':
  32. # f for starred (fav)
  33. $star = 1;
  34. $public = 0;
  35. $albumID = 0;
  36. break;
  37. default:
  38. $star = 0;
  39. $public = 0;
  40. break;
  41. }
  42. foreach ($files as $file) {
  43. if ($file['type']!=='image/jpeg'&&
  44. $file['type']!=='image/png'&&
  45. $file['type']!=='image/gif')
  46. continue;
  47. $id = str_replace('.', '', microtime(true));
  48. while(strlen($id)<14) $id .= 0;
  49. $tmp_name = $file['tmp_name'];
  50. $extension = array_reverse(explode('.', $file['name']));
  51. $extension = $extension[0];
  52. $photo_name = md5($id) . ".$extension";
  53. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  54. # Import if not uploaded via web
  55. if (!is_uploaded_file($tmp_name)) {
  56. if (!@copy($tmp_name, $path)) exit('Error: Could not copy photo to uploads!');
  57. else @unlink($tmp_name);
  58. } else {
  59. if (!@move_uploaded_file($tmp_name, $path)) exit('Error: Could not move photo to uploads!');
  60. }
  61. # Read infos
  62. $info = $this->getInfo($path);
  63. # Use title of file if IPTC title missing
  64. if ($info['title']==='') $info['title'] = mysqli_real_escape_string($this->database, substr(basename($file['name'], ".$extension"), 0, 30));
  65. # Use description parameter if set
  66. if ($description==='') $description = $info['description'];
  67. # Set orientation based on EXIF data
  68. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) {
  69. if (!$this->adjustFile($path, $info)) exit('Error: Could not adjust photo!');
  70. }
  71. # Set original date
  72. if ($info['takestamp']!=='') @touch($path, $info['takestamp']);
  73. # Create Thumb
  74. if (!$this->createThumb($path, $photo_name)) exit('Error: Could not create thumbnail for photo!');
  75. # Save to DB
  76. $query = "INSERT INTO lychee_photos (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star)
  77. VALUES (
  78. '" . $id . "',
  79. '" . $info['title'] . "',
  80. '" . $photo_name . "',
  81. '" . $description . "',
  82. '" . $tags . "',
  83. '" . $info['type'] . "',
  84. '" . $info['width'] . "',
  85. '" . $info['height'] . "',
  86. '" . $info['size'] . "',
  87. '" . $info['iso'] . "',
  88. '" . $info['aperture'] . "',
  89. '" . $info['make'] . "',
  90. '" . $info['model'] . "',
  91. '" . $info['shutter'] . "',
  92. '" . $info['focal'] . "',
  93. '" . $info['takestamp'] . "',
  94. '" . md5($id) . ".jpeg',
  95. '" . $albumID . "',
  96. '" . $public . "',
  97. '" . $star . "');";
  98. $result = $this->database->query($query);
  99. if (!$result) exit('Error: Could not save photo in database!');
  100. }
  101. # Call plugins
  102. $this->plugins(__METHOD__, 1, func_get_args());
  103. return true;
  104. }
  105. private function createThumb($url, $filename, $width = 200, $height = 200) {
  106. if (!isset($this->settings, $url, $filename)) return false;
  107. # Call plugins
  108. $this->plugins(__METHOD__, 0, func_get_args());
  109. $info = getimagesize($url);
  110. $photoName = explode(".", $filename);
  111. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  112. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  113. # create thumbnails with Imagick
  114. if(extension_loaded('imagick')) {
  115. # Read image
  116. $thumb = new Imagick();
  117. $thumb->readImage($url);
  118. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  119. $thumb->setImageFormat('jpeg');
  120. # Copy image for 2nd thumb version
  121. $thumb2x = clone $thumb;
  122. # Create 1st version
  123. $thumb->cropThumbnailImage($width, $height);
  124. $thumb->writeImage($newUrl);
  125. # Create 2nd version
  126. $thumb2x->cropThumbnailImage($width*2, $height*2);
  127. $thumb2x->writeImage($newUrl2x);
  128. # Close thumb
  129. $thumb->clear();
  130. $thumb->destroy();
  131. # Close thumb2
  132. $thumb2x->clear();
  133. $thumb2x->destroy();
  134. } else {
  135. # Set position and size
  136. $thumb = imagecreatetruecolor($width, $height);
  137. $thumb2x = imagecreatetruecolor($width*2, $height*2);
  138. if ($info[0]<$info[1]) {
  139. $newSize = $info[0];
  140. $startWidth = 0;
  141. $startHeight = $info[1]/2 - $info[0]/2;
  142. } else {
  143. $newSize = $info[1];
  144. $startWidth = $info[0]/2 - $info[1]/2;
  145. $startHeight = 0;
  146. }
  147. # Fallback for older version
  148. if ($info['mime']==='image/webp'&&floatval(phpversion())<5.5) return false;
  149. # Create new image
  150. switch($info['mime']) {
  151. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  152. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  153. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  154. case 'image/webp': $sourceImg = imagecreatefromwebp($url); break;
  155. default: return false;
  156. }
  157. imagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
  158. imagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
  159. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  160. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  161. }
  162. # Call plugins
  163. $this->plugins(__METHOD__, 1, func_get_args());
  164. return true;
  165. }
  166. private function adjustFile($path, $info) {
  167. if (!isset($path, $info)) return false;
  168. # Call plugins
  169. $this->plugins(__METHOD__, 0, func_get_args());
  170. if (extension_loaded('imagick')) {
  171. $rotateImage = 0;
  172. switch ($info['orientation']) {
  173. case 3:
  174. $rotateImage = 180;
  175. $imageOrientation = 1;
  176. break;
  177. case 6:
  178. $rotateImage = 90;
  179. $imageOrientation = 1;
  180. break;
  181. case 8:
  182. $rotateImage = 270;
  183. $imageOrientation = 1;
  184. break;
  185. }
  186. if ($rotateImage) {
  187. $image = new Imagick();
  188. $image->readImage($path);
  189. $image->rotateImage(new ImagickPixel(), $rotateImage);
  190. $image->setImageOrientation($imageOrientation);
  191. $image->writeImage($path);
  192. $image->clear();
  193. $image->destroy();
  194. }
  195. } else {
  196. $newWidth = $info['width'];
  197. $newHeight = $info['height'];
  198. $sourceImg = imagecreatefromjpeg($path);
  199. switch ($info['orientation']) {
  200. case 2:
  201. # mirror
  202. # not yet implemented
  203. break;
  204. case 3:
  205. $sourceImg = imagerotate($sourceImg, -180, 0);
  206. break;
  207. case 4:
  208. # rotate 180 and mirror
  209. # not yet implemented
  210. break;
  211. case 5:
  212. # rotate 90 and mirror
  213. # not yet implemented
  214. break;
  215. case 6:
  216. $sourceImg = imagerotate($sourceImg, -90, 0);
  217. $newWidth = $info['height'];
  218. $newHeight = $info['width'];
  219. break;
  220. case 7:
  221. # rotate -90 and mirror
  222. # not yet implemented
  223. break;
  224. case 8:
  225. $sourceImg = imagerotate($sourceImg, 90, 0);
  226. $newWidth = $info['height'];
  227. $newHeight = $info['width'];
  228. break;
  229. }
  230. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  231. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  232. imagejpeg($newSourceImg, $path, 100);
  233. }
  234. # Call plugins
  235. $this->plugins(__METHOD__, 1, func_get_args());
  236. return true;
  237. }
  238. public function get($albumID) {
  239. if (!isset($this->database, $this->photoIDs)) return false;
  240. # Call plugins
  241. $this->plugins(__METHOD__, 0, func_get_args());
  242. # Get photo
  243. $photos = $this->database->query("SELECT * FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  244. $photo = $photos->fetch_assoc();
  245. # Parse photo
  246. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  247. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  248. if ($albumID!='false') {
  249. if ($photo['album']!=0) {
  250. # Get album
  251. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '" . $photo['album'] . " LIMIT 1';");
  252. $album = $albums->fetch_assoc();
  253. # Parse album
  254. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  255. }
  256. $photo['original_album'] = $photo['album'];
  257. $photo['album'] = $albumID;
  258. }
  259. # Call plugins
  260. $this->plugins(__METHOD__, 1, func_get_args());
  261. return $photo;
  262. }
  263. private function getInfo($url) {
  264. if (!isset($this->database, $url)) return false;
  265. # Call plugins
  266. $this->plugins(__METHOD__, 0, func_get_args());
  267. $iptcArray = array();
  268. $info = getimagesize($url, $iptcArray);
  269. # General information
  270. $return['type'] = $info['mime'];
  271. $return['width'] = $info[0];
  272. $return['height'] = $info[1];
  273. # Size
  274. $size = filesize($url)/1024;
  275. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  276. else $return['size'] = round($size, 1) . ' KB';
  277. # IPTC Metadata Fallback
  278. $return['title'] = '';
  279. $return['description'] = '';
  280. # IPTC Metadata
  281. if(isset($iptcArray['APP13'])) {
  282. $iptcInfo = iptcparse($iptcArray['APP13']);
  283. if (is_array($iptcInfo)) {
  284. $temp = @$iptcInfo['2#105'][0];
  285. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  286. $temp = @$iptcInfo['2#120'][0];
  287. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  288. }
  289. }
  290. # EXIF Metadata Fallback
  291. $return['orientation'] = '';
  292. $return['iso'] = '';
  293. $return['aperture'] = '';
  294. $return['make'] = '';
  295. $return['model'] = '';
  296. $return['shutter'] = '';
  297. $return['focal'] = '';
  298. $return['takestamp'] = '';
  299. # Read EXIF
  300. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  301. else $exif = false;
  302. # EXIF Metadata
  303. if ($exif!==false) {
  304. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  305. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  306. $temp = @$exif['ISOSpeedRatings'];
  307. if (isset($temp)) $return['iso'] = $temp;
  308. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  309. if (isset($temp)) $return['aperture'] = $temp;
  310. $temp = @$exif['Make'];
  311. if (isset($temp)) $return['make'] = $exif['Make'];
  312. $temp = @$exif['Model'];
  313. if (isset($temp)) $return['model'] = $temp;
  314. $temp = @$exif['ExposureTime'];
  315. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  316. $temp = @$exif['FocalLength'];
  317. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  318. $temp = @$exif['DateTimeOriginal'];
  319. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  320. }
  321. # Security
  322. foreach(array_keys($return) as $key) $return[$key] = mysqli_real_escape_string($this->database, $return[$key]);
  323. # Call plugins
  324. $this->plugins(__METHOD__, 1, func_get_args());
  325. return $return;
  326. }
  327. public function getArchive() {
  328. if (!isset($this->database, $this->photoIDs)) return false;
  329. # Call plugins
  330. $this->plugins(__METHOD__, 0, func_get_args());
  331. # Get photo
  332. $photos = $this->database->query("SELECT title, url FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  333. $photo = $photos->fetch_object();
  334. # Get extension
  335. $extension = array_reverse(explode('.', $photo->url));
  336. # Parse title
  337. if ($photo->title=='') $photo->title = 'Untitled';
  338. # Set headers
  339. header("Content-Type: application/octet-stream");
  340. header("Content-Disposition: attachment; filename=\"$photo->title.$extension[0]\"");
  341. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  342. # Send file
  343. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  344. # Call plugins
  345. $this->plugins(__METHOD__, 1, func_get_args());
  346. return true;
  347. }
  348. function setTitle($title) {
  349. if (!isset($this->database, $this->photoIDs)) return false;
  350. # Call plugins
  351. $this->plugins(__METHOD__, 0, func_get_args());
  352. # Parse
  353. if (strlen($title)>50) $title = substr($title, 0, 50);
  354. # Set title
  355. $result = $this->database->query("UPDATE lychee_photos SET title = '$title' WHERE id IN ($this->photoIDs);");
  356. # Call plugins
  357. $this->plugins(__METHOD__, 1, func_get_args());
  358. if (!$result) return false;
  359. return true;
  360. }
  361. function setDescription($description) {
  362. if (!isset($this->database, $this->photoIDs)) return false;
  363. # Call plugins
  364. $this->plugins(__METHOD__, 0, func_get_args());
  365. # Parse
  366. $description = htmlentities($description);
  367. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  368. # Set description
  369. $result = $this->database->query("UPDATE lychee_photos SET description = '$description' WHERE id IN ('$this->photoIDs');");
  370. # Call plugins
  371. $this->plugins(__METHOD__, 1, func_get_args());
  372. if (!$result) return false;
  373. return true;
  374. }
  375. public function setStar() {
  376. if (!isset($this->database, $this->photoIDs)) return false;
  377. # Call plugins
  378. $this->plugins(__METHOD__, 0, func_get_args());
  379. # Init vars
  380. $error = false;
  381. # Get photos
  382. $photos = $this->database->query("SELECT id, star FROM lychee_photos WHERE id IN ($this->photoIDs);");
  383. # For each photo
  384. while ($photo = $photos->fetch_object()) {
  385. # Invert star
  386. $star = ($photo->star==0 ? 1 : 0);
  387. # Set star
  388. $star = $this->database->query("UPDATE lychee_photos SET star = '$star' WHERE id = '$photo->id';");
  389. if (!$star) $error = true;
  390. }
  391. # Call plugins
  392. $this->plugins(__METHOD__, 1, func_get_args());
  393. if ($error) return false;
  394. return true;
  395. }
  396. function getPublic($password) {
  397. if (!isset($this->database, $this->photoIDs)) return false;
  398. # Call plugins
  399. $this->plugins(__METHOD__, 0, func_get_args());
  400. # Get photo
  401. $photos = $this->database->query("SELECT public, album FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  402. $photo = $photos->fetch_object();
  403. # Check if public
  404. if ($photo->public==1) return true;
  405. else {
  406. $album = new Album($this->database, null, null, $photo->album);
  407. $acP = $album->checkPassword($password);
  408. $agP = $album->getPublic();
  409. if ($acP===true&&$agP===true) return true;
  410. }
  411. # Call plugins
  412. $this->plugins(__METHOD__, 1, func_get_args());
  413. return false;
  414. }
  415. public function setPublic() {
  416. if (!isset($this->database, $this->photoIDs)) return false;
  417. # Call plugins
  418. $this->plugins(__METHOD__, 0, func_get_args());
  419. # Get public
  420. $photos = $this->database->query("SELECT public FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  421. $photo = $photos->fetch_object();
  422. # Invert public
  423. $public = ($photo->public==0 ? 1 : 0);
  424. # Set public
  425. $result = $this->database->query("UPDATE lychee_photos SET public = '$public' WHERE id = '$this->photoIDs';");
  426. # Call plugins
  427. $this->plugins(__METHOD__, 1, func_get_args());
  428. if (!$result) return false;
  429. return true;
  430. }
  431. function setAlbum($albumID) {
  432. if (!isset($this->database, $this->photoIDs)) return false;
  433. # Call plugins
  434. $this->plugins(__METHOD__, 0, func_get_args());
  435. # Set album
  436. $result = $this->database->query("UPDATE lychee_photos SET album = '$albumID' WHERE id IN ($this->photoIDs);");
  437. # Call plugins
  438. $this->plugins(__METHOD__, 1, func_get_args());
  439. if (!$result) return false;
  440. return true;
  441. }
  442. public function setTags($tags) {
  443. if (!isset($this->database, $this->photoIDs)) return false;
  444. # Call plugins
  445. $this->plugins(__METHOD__, 0, func_get_args());
  446. # Parse tags
  447. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  448. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  449. if (strlen($tags)>1000) return false;
  450. # Set tags
  451. $result = $this->database->query("UPDATE lychee_photos SET tags = '$tags' WHERE id IN ($this->photoIDs);");
  452. # Call plugins
  453. $this->plugins(__METHOD__, 1, func_get_args());
  454. if (!$result) return false;
  455. return true;
  456. }
  457. public function delete() {
  458. if (!isset($this->database, $this->photoIDs)) return false;
  459. # Call plugins
  460. $this->plugins(__METHOD__, 0, func_get_args());
  461. # Get photos
  462. $photos = $this->database->query("SELECT id, url, thumbUrl FROM lychee_photos WHERE id IN ($this->photoIDs);");
  463. # For each photo
  464. while ($photo = $photos->fetch_object()) {
  465. # Get retina thumb url
  466. $thumbUrl2x = explode(".", $photo->thumbUrl);
  467. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  468. # Delete files
  469. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) return false;
  470. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) return false;
  471. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) return false;
  472. # Delete db entry
  473. $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");
  474. if (!$delete) return false;
  475. }
  476. # Call plugins
  477. $this->plugins(__METHOD__, 1, func_get_args());
  478. if (!$photos) return false;
  479. return true;
  480. }
  481. }
  482. ?>