Photo.php 33 KB

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