Photo.php 32 KB

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