Photo.php 34 KB

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