Photo.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. <?php
  2. ###
  3. # @name Photo Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Photo extends Module {
  8. private $database = null;
  9. private $settings = null;
  10. private $photoIDs = null;
  11. private $allowedTypes = array(
  12. IMAGETYPE_JPEG,
  13. IMAGETYPE_GIF,
  14. IMAGETYPE_PNG
  15. );
  16. private $validExtensions = array(
  17. '.jpg',
  18. '.jpeg',
  19. '.png',
  20. '.gif'
  21. );
  22. public function __construct($database, $plugins, $settings, $photoIDs) {
  23. # Init vars
  24. $this->database = $database;
  25. $this->plugins = $plugins;
  26. $this->settings = $settings;
  27. $this->photoIDs = $photoIDs;
  28. return true;
  29. }
  30. public function add($files, $albumID, $description = '', $tags = '') {
  31. # Check dependencies
  32. self::dependencies(isset($this->database));
  33. # Check permissions
  34. if (hasPermissions(LYCHEE_UPLOADS)===false||
  35. hasPermissions(LYCHEE_UPLOADS_BIG)===false||
  36. hasPermissions(LYCHEE_UPLOADS_THUMB)===false) {
  37. Log::error($this->database, __METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable');
  38. exit('Error: An upload-folder is missing or not readable and writable!');
  39. }
  40. # Call plugins
  41. $this->plugins(__METHOD__, 0, func_get_args());
  42. switch($albumID) {
  43. case 's':
  44. # s for public (share)
  45. $public = 1;
  46. $star = 0;
  47. $albumID = 0;
  48. break;
  49. case 'f':
  50. # f for starred (fav)
  51. $star = 1;
  52. $public = 0;
  53. $albumID = 0;
  54. break;
  55. case 'r':
  56. # r for recent
  57. $public = 0;
  58. $star = 0;
  59. $albumID = 0;
  60. break;
  61. default:
  62. $star = 0;
  63. $public = 0;
  64. break;
  65. }
  66. foreach ($files as $file) {
  67. # Verify extension
  68. $extension = getExtension($file['name']);
  69. if (!in_array(strtolower($extension), $this->validExtensions, true)) continue;
  70. # Verify image
  71. $type = @exif_imagetype($file['tmp_name']);
  72. if (!in_array($type, $this->allowedTypes, true)) continue;
  73. # Generate id
  74. $id = str_replace('.', '', microtime(true));
  75. while(strlen($id)<14) $id .= 0;
  76. # Set paths
  77. $tmp_name = $file['tmp_name'];
  78. $photo_name = md5($id) . $extension;
  79. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  80. # Calculate checksum
  81. $checksum = sha1_file($tmp_name);
  82. if ($checksum===false) {
  83. Log::error($this->database, __METHOD__, __LINE__, 'Could not calculate checksum for photo');
  84. exit('Error: Could not calculate checksum for photo!');
  85. }
  86. # Check if image exists based on checksum
  87. if ($checksum===false) {
  88. $checksum = '';
  89. $exists = false;
  90. } else {
  91. $exists = $this->exists($checksum);
  92. if ($exists!==false) {
  93. $photo_name = $exists['photo_name'];
  94. $path = $exists['path'];
  95. $path_thumb = $exists['path_thumb'];
  96. $medium = ($exists['medium']==='1' ? 1 : 0);
  97. $exists = true;
  98. }
  99. }
  100. if ($exists===false) {
  101. # Import if not uploaded via web
  102. if (!is_uploaded_file($tmp_name)) {
  103. if (!@copy($tmp_name, $path)) {
  104. Log::error($this->database, __METHOD__, __LINE__, 'Could not copy photo to uploads');
  105. exit('Error: Could not copy photo to uploads!');
  106. } else @unlink($tmp_name);
  107. } else {
  108. if (!@move_uploaded_file($tmp_name, $path)) {
  109. Log::error($this->database, __METHOD__, __LINE__, 'Could not move photo to uploads');
  110. exit('Error: Could not move photo to uploads!');
  111. }
  112. }
  113. }
  114. # Read infos
  115. $info = $this->getInfo($path);
  116. # Use title of file if IPTC title missing
  117. if ($info['title']==='') $info['title'] = substr(basename($file['name'], $extension), 0, 30);
  118. # Use description parameter if set
  119. if ($description==='') $description = $info['description'];
  120. if ($exists===false) {
  121. # Set orientation based on EXIF data
  122. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!=='') {
  123. $adjustFile = $this->adjustFile($path, $info);
  124. if ($adjustFile!==false) $info = $adjustFile;
  125. else Log::notice($this->database, __METHOD__, __LINE__, 'Skipped adjustment of photo (' . $info['title'] . ')');
  126. }
  127. # Set original date
  128. if ($info['takestamp']!==''&&$info['takestamp']!==0) @touch($path, $info['takestamp']);
  129. # Create Thumb
  130. if (!$this->createThumb($path, $photo_name, $info['type'], $info['width'], $info['height'])) {
  131. Log::error($this->database, __METHOD__, __LINE__, 'Could not create thumbnail for photo');
  132. exit('Error: Could not create thumbnail for photo!');
  133. }
  134. # Create Medium
  135. if ($this->createMedium($path, $photo_name, $info['width'], $info['height'])) $medium = 1;
  136. else $medium = 0;
  137. # Set thumb url
  138. $path_thumb = md5($id) . '.jpeg';
  139. }
  140. # Save to DB
  141. $values = array(LYCHEE_TABLE_PHOTOS, $id, $info['title'], $photo_name, $description, $tags, $info['type'], $info['width'], $info['height'], $info['size'], $info['iso'], $info['aperture'], $info['make'], $info['model'], $info['shutter'], $info['focal'], $info['takestamp'], $path_thumb, $albumID, $public, $star, $checksum, $medium);
  142. $query = Database::prepare($this->database, "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum, medium) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')", $values);
  143. $result = $this->database->query($query);
  144. if (!$result) {
  145. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  146. exit('Error: Could not save photo in database!');
  147. }
  148. }
  149. # Call plugins
  150. $this->plugins(__METHOD__, 1, func_get_args());
  151. return true;
  152. }
  153. private function exists($checksum, $photoID = null) {
  154. # Check dependencies
  155. self::dependencies(isset($this->database, $checksum));
  156. # Exclude $photoID from select when $photoID is set
  157. if (isset($photoID)) $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' AND id <> '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum, $photoID));
  158. else $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, medium FROM ? WHERE checksum = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $checksum));
  159. $result = $this->database->query($query);
  160. if (!$result) {
  161. Log::error($this->database, __METHOD__, __LINE__, 'Could not check for existing photos with the same checksum');
  162. return false;
  163. }
  164. if ($result->num_rows===1) {
  165. $result = $result->fetch_object();
  166. $return = array(
  167. 'photo_name' => $result->url,
  168. 'path' => LYCHEE_UPLOADS_BIG . $result->url,
  169. 'path_thumb' => $result->thumbUrl,
  170. 'medium' => $result->medium
  171. );
  172. return $return;
  173. }
  174. return false;
  175. }
  176. private function createThumb($url, $filename, $type, $width, $height) {
  177. # Check dependencies
  178. self::dependencies(isset($this->database, $this->settings, $url, $filename, $type, $width, $height));
  179. # Call plugins
  180. $this->plugins(__METHOD__, 0, func_get_args());
  181. # Size of the thumbnail
  182. $newWidth = 200;
  183. $newHeight = 200;
  184. $photoName = explode('.', $filename);
  185. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  186. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  187. # Create thumbnails with Imagick
  188. if(extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  189. # Read image
  190. $thumb = new Imagick();
  191. $thumb->readImage($url);
  192. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  193. $thumb->setImageFormat('jpeg');
  194. # Copy image for 2nd thumb version
  195. $thumb2x = clone $thumb;
  196. # Create 1st version
  197. $thumb->cropThumbnailImage($newWidth, $newHeight);
  198. $thumb->writeImage($newUrl);
  199. $thumb->clear();
  200. $thumb->destroy();
  201. # Create 2nd version
  202. $thumb2x->cropThumbnailImage($newWidth*2, $newHeight*2);
  203. $thumb2x->writeImage($newUrl2x);
  204. $thumb2x->clear();
  205. $thumb2x->destroy();
  206. } else {
  207. # Create image
  208. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  209. $thumb2x = imagecreatetruecolor($newWidth*2, $newHeight*2);
  210. # Set position
  211. if ($width<$height) {
  212. $newSize = $width;
  213. $startWidth = 0;
  214. $startHeight = $height/2 - $width/2;
  215. } else {
  216. $newSize = $height;
  217. $startWidth = $width/2 - $height/2;
  218. $startHeight = 0;
  219. }
  220. # Create new image
  221. switch($type) {
  222. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  223. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  224. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  225. default: Log::error($this->database, __METHOD__, __LINE__, 'Type of photo is not supported');
  226. return false;
  227. break;
  228. }
  229. # Create thumb
  230. fastimagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth, $newHeight, $newSize, $newSize);
  231. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  232. imagedestroy($thumb);
  233. # Create retina thumb
  234. fastimagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $newWidth*2, $newHeight*2, $newSize, $newSize);
  235. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  236. imagedestroy($thumb2x);
  237. # Free memory
  238. imagedestroy($sourceImg);
  239. }
  240. # Call plugins
  241. $this->plugins(__METHOD__, 1, func_get_args());
  242. return true;
  243. }
  244. private function createMedium($url, $filename, $width, $height) {
  245. # Function creates a smaller version of a photo when its size is bigger than a preset size
  246. # Excepts the following:
  247. # (string) $url = Path to the photo-file
  248. # (string) $filename = Name of the photo-file
  249. # (int) $width = Width of the photo
  250. # (int) $height = Height of the photo
  251. # Returns the following
  252. # (boolean) true = Success
  253. # (boolean) false = Failure
  254. # Check dependencies
  255. self::dependencies(isset($this->database, $this->settings, $url, $filename, $width, $height));
  256. # Call plugins
  257. $this->plugins(__METHOD__, 0, func_get_args());
  258. # Set to true when creation of medium-photo failed
  259. $error = false;
  260. # Size of the medium-photo
  261. # When changing these values,
  262. # also change the size detection in the front-end
  263. $newWidth = 1920;
  264. $newHeight = 1080;
  265. # Check permissions
  266. if (hasPermissions(LYCHEE_UPLOADS_MEDIUM)===false) {
  267. # Permissions are missing
  268. Log::notice($this->database, __METHOD__, __LINE__, 'Skipped creation of medium-photo, because uploads/medium/ is missing or not readable and writable.');
  269. $error = true;
  270. }
  271. # Is photo big enough?
  272. # Is medium activated?
  273. # Is Imagick installed and activated?
  274. if (($error===false)&&
  275. ($width>$newWidth||$height>$newHeight)&&
  276. ($this->settings['medium']==='1')&&
  277. (extension_loaded('imagick')&&$this->settings['imagick']==='1')) {
  278. $newUrl = LYCHEE_UPLOADS_MEDIUM . $filename;
  279. # Read image
  280. $medium = new Imagick();
  281. $medium->readImage($url);
  282. # Adjust image
  283. $medium->scaleImage($newWidth, $newHeight, true);
  284. # Save image
  285. try { $medium->writeImage($newUrl); }
  286. catch (ImagickException $err) {
  287. Log::notice($this->database, __METHOD__, __LINE__, 'Could not save medium-photo: ' . $err->getMessage());
  288. $error = true;
  289. }
  290. $medium->clear();
  291. $medium->destroy();
  292. } else {
  293. # Photo too small or
  294. # Medium is deactivated or
  295. # Imagick not installed
  296. $error = true;
  297. }
  298. # Call plugins
  299. $this->plugins(__METHOD__, 1, func_get_args());
  300. if ($error===true) return false;
  301. return true;
  302. }
  303. public function adjustFile($path, $info) {
  304. # Function rotates and flips a photo based on its EXIF orientation
  305. # Excepts the following:
  306. # (string) $path = Path to the photo-file
  307. # (array) $info = ['orientation', 'width', 'height']
  308. # Returns the following
  309. # (array) $info = ['orientation', 'width', 'height'] = Success
  310. # (boolean) false = Failure
  311. # Check dependencies
  312. self::dependencies(isset($path, $info));
  313. # Call plugins
  314. $this->plugins(__METHOD__, 0, func_get_args());
  315. $swapSize = false;
  316. if (extension_loaded('imagick')&&$this->settings['imagick']==='1') {
  317. $rotateImage = 0;
  318. switch ($info['orientation']) {
  319. case 3:
  320. $rotateImage = 180;
  321. break;
  322. case 6:
  323. $rotateImage = 90;
  324. $swapSize = true;
  325. break;
  326. case 8:
  327. $rotateImage = 270;
  328. $swapSize = true;
  329. break;
  330. default:
  331. return false;
  332. break;
  333. }
  334. if ($rotateImage!==0) {
  335. $image = new Imagick();
  336. $image->readImage($path);
  337. $image->rotateImage(new ImagickPixel(), $rotateImage);
  338. $image->setImageOrientation(1);
  339. $image->writeImage($path);
  340. $image->clear();
  341. $image->destroy();
  342. }
  343. } else {
  344. $newWidth = $info['width'];
  345. $newHeight = $info['height'];
  346. $sourceImg = imagecreatefromjpeg($path);
  347. switch ($info['orientation']) {
  348. case 2:
  349. # mirror
  350. # not yet implemented
  351. return false;
  352. break;
  353. case 3:
  354. $process = true;
  355. $sourceImg = imagerotate($sourceImg, -180, 0);
  356. break;
  357. case 4:
  358. # rotate 180 and mirror
  359. # not yet implemented
  360. return false;
  361. break;
  362. case 5:
  363. # rotate 90 and mirror
  364. # not yet implemented
  365. return false;
  366. break;
  367. case 6:
  368. $process = true;
  369. $sourceImg = imagerotate($sourceImg, -90, 0);
  370. $newWidth = $info['height'];
  371. $newHeight = $info['width'];
  372. $swapSize = true;
  373. break;
  374. case 7:
  375. # rotate -90 and mirror
  376. # not yet implemented
  377. return false;
  378. break;
  379. case 8:
  380. $process = true;
  381. $sourceImg = imagerotate($sourceImg, 90, 0);
  382. $newWidth = $info['height'];
  383. $newHeight = $info['width'];
  384. $swapSize = true;
  385. break;
  386. default:
  387. return false;
  388. break;
  389. }
  390. # Recreate photo
  391. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  392. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  393. imagejpeg($newSourceImg, $path, 100);
  394. # Free memory
  395. imagedestroy($sourceImg);
  396. imagedestroy($newSourceImg);
  397. }
  398. # Call plugins
  399. $this->plugins(__METHOD__, 1, func_get_args());
  400. # SwapSize should be true when the image has been rotated
  401. # Return new dimensions in this case
  402. if ($swapSize===true) {
  403. $swapSize = $info['width'];
  404. $info['width'] = $info['height'];
  405. $info['height'] = $swapSize;
  406. }
  407. return $info;
  408. }
  409. public static function prepareData($data) {
  410. # Function turns photo-attributes into a front-end friendly format. Note that some attributes remain unchanged.
  411. # Excepts the following:
  412. # (array) $data = ['id', 'title', 'tags', 'public', 'star', 'album', 'thumbUrl', 'takestamp', 'url']
  413. # Returns the following:
  414. # (array) $photo
  415. # Check dependencies
  416. self::dependencies(isset($data));
  417. # Init
  418. $photo = null;
  419. # Set unchanged attributes
  420. $photo['id'] = $data['id'];
  421. $photo['title'] = $data['title'];
  422. $photo['tags'] = $data['tags'];
  423. $photo['public'] = $data['public'];
  424. $photo['star'] = $data['star'];
  425. $photo['album'] = $data['album'];
  426. # Parse urls
  427. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $data['thumbUrl'];
  428. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $data['url'];
  429. # Use takestamp as sysdate when possible
  430. if (isset($data['takestamp'])&&$data['takestamp']!=='0') {
  431. # Use takestamp
  432. $photo['cameraDate'] = '1';
  433. $photo['sysdate'] = date('d F Y', $data['takestamp']);
  434. } else {
  435. # Use sysstamp from the id
  436. $photo['cameraDate'] = '0';
  437. $photo['sysdate'] = date('d F Y', substr($data['id'], 0, -4));
  438. }
  439. return $photo;
  440. }
  441. public function get($albumID) {
  442. # Functions returns data of a photo
  443. # Excepts the following:
  444. # (string) $albumID = Album which is currently visible to the user
  445. # Returns the following:
  446. # (array) $photo
  447. # Check dependencies
  448. self::dependencies(isset($this->database, $this->photoIDs));
  449. # Call plugins
  450. $this->plugins(__METHOD__, 0, func_get_args());
  451. # Get photo
  452. $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  453. $photos = $this->database->query($query);
  454. $photo = $photos->fetch_assoc();
  455. # Parse photo
  456. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  457. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  458. # Parse medium
  459. if ($photo['medium']==='1') $photo['medium'] = LYCHEE_URL_UPLOADS_MEDIUM . $photo['url'];
  460. else $photo['medium'] = '';
  461. # Parse paths
  462. $photo['url'] = LYCHEE_URL_UPLOADS_BIG . $photo['url'];
  463. $photo['thumbUrl'] = LYCHEE_URL_UPLOADS_THUMB . $photo['thumbUrl'];
  464. if ($albumID!='false') {
  465. # Only show photo as public when parent album is public
  466. # Check if parent album is not 'Unsorted'
  467. if ($photo['album']!=='0') {
  468. # Get album
  469. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $photo['album']));
  470. $albums = $this->database->query($query);
  471. $album = $albums->fetch_assoc();
  472. # Parse album
  473. $photo['public'] = ($album['public']==='1' ? '2' : $photo['public']);
  474. }
  475. $photo['original_album'] = $photo['album'];
  476. $photo['album'] = $albumID;
  477. }
  478. # Call plugins
  479. $this->plugins(__METHOD__, 1, func_get_args());
  480. return $photo;
  481. }
  482. public function getInfo($url) {
  483. # Functions returns information and metadata of a photo
  484. # Excepts the following:
  485. # (string) $url = Path to photo-file
  486. # Returns the following:
  487. # (array) $return
  488. # Check dependencies
  489. self::dependencies(isset($this->database, $url));
  490. # Call plugins
  491. $this->plugins(__METHOD__, 0, func_get_args());
  492. $iptcArray = array();
  493. $info = getimagesize($url, $iptcArray);
  494. # General information
  495. $return['type'] = $info['mime'];
  496. $return['width'] = $info[0];
  497. $return['height'] = $info[1];
  498. # Size
  499. $size = filesize($url)/1024;
  500. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  501. else $return['size'] = round($size, 1) . ' KB';
  502. # IPTC Metadata Fallback
  503. $return['title'] = '';
  504. $return['description'] = '';
  505. # IPTC Metadata
  506. if(isset($iptcArray['APP13'])) {
  507. $iptcInfo = iptcparse($iptcArray['APP13']);
  508. if (is_array($iptcInfo)) {
  509. $temp = @$iptcInfo['2#105'][0];
  510. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  511. $temp = @$iptcInfo['2#120'][0];
  512. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  513. $temp = @$iptcInfo['2#005'][0];
  514. if (isset($temp)&&strlen($temp)>0&&$return['title']==='') $return['title'] = $temp;
  515. }
  516. }
  517. # EXIF Metadata Fallback
  518. $return['orientation'] = '';
  519. $return['iso'] = '';
  520. $return['aperture'] = '';
  521. $return['make'] = '';
  522. $return['model'] = '';
  523. $return['shutter'] = '';
  524. $return['focal'] = '';
  525. $return['takestamp'] = 0;
  526. # Read EXIF
  527. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  528. else $exif = false;
  529. # EXIF Metadata
  530. if ($exif!==false) {
  531. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  532. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  533. $temp = @$exif['ISOSpeedRatings'];
  534. if (isset($temp)) $return['iso'] = $temp;
  535. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  536. if (isset($temp)) $return['aperture'] = $temp;
  537. $temp = @$exif['Make'];
  538. if (isset($temp)) $return['make'] = trim($temp);
  539. $temp = @$exif['Model'];
  540. if (isset($temp)) $return['model'] = trim($temp);
  541. $temp = @$exif['ExposureTime'];
  542. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' s';
  543. $temp = @$exif['FocalLength'];
  544. if (isset($temp)) {
  545. if (strpos($temp, '/')!==FALSE) {
  546. $temp = explode('/', $temp, 2);
  547. $temp = $temp[0] / $temp[1];
  548. $temp = round($temp, 1);
  549. $return['focal'] = $temp . ' mm';
  550. }
  551. $return['focal'] = $temp . ' mm';
  552. }
  553. $temp = @$exif['DateTimeOriginal'];
  554. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  555. }
  556. # Call plugins
  557. $this->plugins(__METHOD__, 1, func_get_args());
  558. return $return;
  559. }
  560. public function getArchive() {
  561. # Functions starts a download of a photo
  562. # Returns the following:
  563. # (boolean + output) true = Success
  564. # (boolean) false = Failure
  565. # Check dependencies
  566. self::dependencies(isset($this->database, $this->photoIDs));
  567. # Call plugins
  568. $this->plugins(__METHOD__, 0, func_get_args());
  569. # Get photo
  570. $query = Database::prepare($this->database, "SELECT title, url FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  571. $photos = $this->database->query($query);
  572. $photo = $photos->fetch_object();
  573. # Error in database query
  574. if (!$photos) {
  575. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  576. return false;
  577. }
  578. # Photo not found
  579. if ($photo===null) {
  580. Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
  581. return false;
  582. }
  583. # Get extension
  584. $extension = getExtension($photo->url);
  585. if ($extension===false) {
  586. Log::error($this->database, __METHOD__, __LINE__, 'Invalid photo extension');
  587. return false;
  588. }
  589. # Illicit chars
  590. $badChars = array_merge(
  591. array_map('chr', range(0,31)),
  592. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  593. );
  594. # Parse title
  595. if ($photo->title=='') $photo->title = 'Untitled';
  596. # Escape title
  597. $photo->title = str_replace($badChars, '', $photo->title);
  598. # Set headers
  599. header("Content-Type: application/octet-stream");
  600. header("Content-Disposition: attachment; filename=\"" . $photo->title . $extension . "\"");
  601. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  602. # Send file
  603. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  604. # Call plugins
  605. $this->plugins(__METHOD__, 1, func_get_args());
  606. return true;
  607. }
  608. public function setTitle($title) {
  609. # Functions sets the title of a photo
  610. # Excepts the following:
  611. # (string) $title = Title with a maximum length of 50 chars
  612. # Returns the following:
  613. # (boolean) true = Success
  614. # (boolean) false = Failure
  615. # Check dependencies
  616. self::dependencies(isset($this->database, $this->photoIDs));
  617. # Call plugins
  618. $this->plugins(__METHOD__, 0, func_get_args());
  619. # Parse
  620. if (strlen($title)>50) $title = substr($title, 0, 50);
  621. # Set title
  622. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  623. $result = $this->database->query($query);
  624. # Call plugins
  625. $this->plugins(__METHOD__, 1, func_get_args());
  626. if (!$result) {
  627. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  628. return false;
  629. }
  630. return true;
  631. }
  632. public function setDescription($description) {
  633. # Functions sets the description of a photo
  634. # Excepts the following:
  635. # (string) $description = Description with a maximum length of 1000 chars
  636. # Returns the following:
  637. # (boolean) true = Success
  638. # (boolean) false = Failure
  639. # Check dependencies
  640. self::dependencies(isset($this->database, $this->photoIDs));
  641. # Call plugins
  642. $this->plugins(__METHOD__, 0, func_get_args());
  643. # Parse
  644. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  645. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  646. # Set description
  647. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  648. $result = $this->database->query($query);
  649. # Call plugins
  650. $this->plugins(__METHOD__, 1, func_get_args());
  651. if (!$result) {
  652. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  653. return false;
  654. }
  655. return true;
  656. }
  657. public function setStar() {
  658. # Functions stars a photo
  659. # Returns the following:
  660. # (boolean) true = Success
  661. # (boolean) false = Failure
  662. # Check dependencies
  663. self::dependencies(isset($this->database, $this->photoIDs));
  664. # Call plugins
  665. $this->plugins(__METHOD__, 0, func_get_args());
  666. # Init vars
  667. $error = false;
  668. # Get photos
  669. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  670. $photos = $this->database->query($query);
  671. # For each photo
  672. while ($photo = $photos->fetch_object()) {
  673. # Invert star
  674. $star = ($photo->star==0 ? 1 : 0);
  675. # Set star
  676. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  677. $star = $this->database->query($query);
  678. if (!$star) $error = true;
  679. }
  680. # Call plugins
  681. $this->plugins(__METHOD__, 1, func_get_args());
  682. if ($error===true) {
  683. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  684. return false;
  685. }
  686. return true;
  687. }
  688. public function getPublic($password) {
  689. # Functions checks if photo or parent album is public
  690. # Returns the following:
  691. # (int) 0 = Photo private and parent album private
  692. # (int) 1 = Album public, but password incorrect
  693. # (int) 2 = Photo public or album public and password correct
  694. # Check dependencies
  695. self::dependencies(isset($this->database, $this->photoIDs));
  696. # Call plugins
  697. $this->plugins(__METHOD__, 0, func_get_args());
  698. # Get photo
  699. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  700. $photos = $this->database->query($query);
  701. $photo = $photos->fetch_object();
  702. # Check if public
  703. if ($photo->public==='1') {
  704. # Photo public
  705. return 2;
  706. } else {
  707. # Check if album public
  708. $album = new Album($this->database, null, null, $photo->album);
  709. $agP = $album->getPublic();
  710. $acP = $album->checkPassword($password);
  711. # Album public and password correct
  712. if ($agP===true&&$acP===true) return 2;
  713. # Album public, but password incorrect
  714. if ($agP===true&&$acP===false) return 1;
  715. }
  716. # Call plugins
  717. $this->plugins(__METHOD__, 1, func_get_args());
  718. # Photo private
  719. return 0;
  720. }
  721. public function setPublic() {
  722. # Functions toggles the public property of a photo
  723. # Returns the following:
  724. # (boolean) true = Success
  725. # (boolean) false = Failure
  726. # Check dependencies
  727. self::dependencies(isset($this->database, $this->photoIDs));
  728. # Call plugins
  729. $this->plugins(__METHOD__, 0, func_get_args());
  730. # Get public
  731. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  732. $photos = $this->database->query($query);
  733. $photo = $photos->fetch_object();
  734. # Invert public
  735. $public = ($photo->public==0 ? 1 : 0);
  736. # Set public
  737. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  738. $result = $this->database->query($query);
  739. # Call plugins
  740. $this->plugins(__METHOD__, 1, func_get_args());
  741. if (!$result) {
  742. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  743. return false;
  744. }
  745. return true;
  746. }
  747. function setAlbum($albumID) {
  748. # Functions sets the parent album of a photo
  749. # Returns the following:
  750. # (boolean) true = Success
  751. # (boolean) false = Failure
  752. # Check dependencies
  753. self::dependencies(isset($this->database, $this->photoIDs));
  754. # Call plugins
  755. $this->plugins(__METHOD__, 0, func_get_args());
  756. # Set album
  757. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  758. $result = $this->database->query($query);
  759. # Call plugins
  760. $this->plugins(__METHOD__, 1, func_get_args());
  761. if (!$result) {
  762. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  763. return false;
  764. }
  765. return true;
  766. }
  767. public function setTags($tags) {
  768. # Functions sets the tags of a photo
  769. # Excepts the following:
  770. # (string) $tags = Comma separated list of tags with a maximum length of 1000 chars
  771. # Returns the following:
  772. # (boolean) true = Success
  773. # (boolean) false = Failure
  774. # Check dependencies
  775. self::dependencies(isset($this->database, $this->photoIDs));
  776. # Call plugins
  777. $this->plugins(__METHOD__, 0, func_get_args());
  778. # Parse tags
  779. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  780. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  781. if (strlen($tags)>1000) {
  782. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  783. return false;
  784. }
  785. # Set tags
  786. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  787. $result = $this->database->query($query);
  788. # Call plugins
  789. $this->plugins(__METHOD__, 1, func_get_args());
  790. if (!$result) {
  791. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  792. return false;
  793. }
  794. return true;
  795. }
  796. public function duplicate() {
  797. # Functions duplicates a photo
  798. # Returns the following:
  799. # (boolean) true = Success
  800. # (boolean) false = Failure
  801. # Check dependencies
  802. self::dependencies(isset($this->database, $this->photoIDs));
  803. # Call plugins
  804. $this->plugins(__METHOD__, 0, func_get_args());
  805. # Get photos
  806. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  807. $photos = $this->database->query($query);
  808. if (!$photos) {
  809. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  810. return false;
  811. }
  812. # For each photo
  813. while ($photo = $photos->fetch_object()) {
  814. # Generate id
  815. $id = str_replace('.', '', microtime(true));
  816. while(strlen($id)<14) $id .= 0;
  817. # Duplicate entry
  818. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  819. $query = Database::prepare($this->database, "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum) SELECT '?' AS id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum FROM ? WHERE id = '?'", $values);
  820. $duplicate = $this->database->query($query);
  821. if (!$duplicate) {
  822. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  823. return false;
  824. }
  825. }
  826. return true;
  827. }
  828. public function delete() {
  829. # Functions deletes a photo with all its data and files
  830. # Returns the following:
  831. # (boolean) true = Success
  832. # (boolean) false = Failure
  833. # Check dependencies
  834. self::dependencies(isset($this->database, $this->photoIDs));
  835. # Call plugins
  836. $this->plugins(__METHOD__, 0, func_get_args());
  837. # Get photos
  838. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  839. $photos = $this->database->query($query);
  840. if (!$photos) {
  841. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  842. return false;
  843. }
  844. # For each photo
  845. while ($photo = $photos->fetch_object()) {
  846. # Check if other photos are referring to this images
  847. # If so, only delete the db entry
  848. if ($this->exists($photo->checksum, $photo->id)===false) {
  849. # Get retina thumb url
  850. $thumbUrl2x = explode(".", $photo->thumbUrl);
  851. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  852. # Delete big
  853. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  854. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  855. return false;
  856. }
  857. # Delete medium
  858. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  859. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  860. return false;
  861. }
  862. # Delete thumb
  863. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  864. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  865. return false;
  866. }
  867. # Delete thumb@2x
  868. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  869. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  870. return false;
  871. }
  872. }
  873. # Delete db entry
  874. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  875. $delete = $this->database->query($query);
  876. if (!$delete) {
  877. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  878. return false;
  879. }
  880. }
  881. # Call plugins
  882. $this->plugins(__METHOD__, 1, func_get_args());
  883. return true;
  884. }
  885. }
  886. ?>