Photo.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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. # Parse
  673. if (strlen($title)>100) $title = substr($title, 0, 100);
  674. # Set title
  675. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $title, $this->photoIDs));
  676. $result = $this->database->query($query);
  677. # Call plugins
  678. $this->plugins(__METHOD__, 1, func_get_args());
  679. if (!$result) {
  680. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  681. return false;
  682. }
  683. return true;
  684. }
  685. public function setDescription($description) {
  686. # Functions sets the description of a photo
  687. # Excepts the following:
  688. # (string) $description = Description with a maximum length of 1000 chars
  689. # Returns the following:
  690. # (boolean) true = Success
  691. # (boolean) false = Failure
  692. # Check dependencies
  693. self::dependencies(isset($this->database, $this->photoIDs));
  694. # Call plugins
  695. $this->plugins(__METHOD__, 0, func_get_args());
  696. # Parse
  697. $description = htmlentities($description, ENT_COMPAT | ENT_HTML401, 'UTF-8');
  698. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  699. # Set description
  700. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN ('?')", array(LYCHEE_TABLE_PHOTOS, $description, $this->photoIDs));
  701. $result = $this->database->query($query);
  702. # Call plugins
  703. $this->plugins(__METHOD__, 1, func_get_args());
  704. if (!$result) {
  705. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  706. return false;
  707. }
  708. return true;
  709. }
  710. public function setStar() {
  711. # Functions stars a photo
  712. # Returns the following:
  713. # (boolean) true = Success
  714. # (boolean) false = Failure
  715. # Check dependencies
  716. self::dependencies(isset($this->database, $this->photoIDs));
  717. # Call plugins
  718. $this->plugins(__METHOD__, 0, func_get_args());
  719. # Init vars
  720. $error = false;
  721. # Get photos
  722. $query = Database::prepare($this->database, "SELECT id, star FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  723. $photos = $this->database->query($query);
  724. # For each photo
  725. while ($photo = $photos->fetch_object()) {
  726. # Invert star
  727. $star = ($photo->star==0 ? 1 : 0);
  728. # Set star
  729. $query = Database::prepare($this->database, "UPDATE ? SET star = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $star, $photo->id));
  730. $star = $this->database->query($query);
  731. if (!$star) $error = true;
  732. }
  733. # Call plugins
  734. $this->plugins(__METHOD__, 1, func_get_args());
  735. if ($error===true) {
  736. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  737. return false;
  738. }
  739. return true;
  740. }
  741. public function getPublic($password) {
  742. # Functions checks if photo or parent album is public
  743. # Returns the following:
  744. # (int) 0 = Photo private and parent album private
  745. # (int) 1 = Album public, but password incorrect
  746. # (int) 2 = Photo public or album public and password correct
  747. # Check dependencies
  748. self::dependencies(isset($this->database, $this->photoIDs));
  749. # Call plugins
  750. $this->plugins(__METHOD__, 0, func_get_args());
  751. # Get photo
  752. $query = Database::prepare($this->database, "SELECT public, album FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  753. $photos = $this->database->query($query);
  754. $photo = $photos->fetch_object();
  755. # Check if public
  756. if ($photo->public==='1') {
  757. # Photo public
  758. return 2;
  759. } else {
  760. # Check if album public
  761. $album = new Album($this->database, null, null, $photo->album);
  762. $agP = $album->getPublic();
  763. $acP = $album->checkPassword($password);
  764. # Album public and password correct
  765. if ($agP===true&&$acP===true) return 2;
  766. # Album public, but password incorrect
  767. if ($agP===true&&$acP===false) return 1;
  768. }
  769. # Call plugins
  770. $this->plugins(__METHOD__, 1, func_get_args());
  771. # Photo private
  772. return 0;
  773. }
  774. public function setPublic() {
  775. # Functions toggles the public property of a photo
  776. # Returns the following:
  777. # (boolean) true = Success
  778. # (boolean) false = Failure
  779. # Check dependencies
  780. self::dependencies(isset($this->database, $this->photoIDs));
  781. # Call plugins
  782. $this->plugins(__METHOD__, 0, func_get_args());
  783. # Get public
  784. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  785. $photos = $this->database->query($query);
  786. $photo = $photos->fetch_object();
  787. # Invert public
  788. $public = ($photo->public==0 ? 1 : 0);
  789. # Set public
  790. $query = Database::prepare($this->database, "UPDATE ? SET public = '?' WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $public, $this->photoIDs));
  791. $result = $this->database->query($query);
  792. # Call plugins
  793. $this->plugins(__METHOD__, 1, func_get_args());
  794. if (!$result) {
  795. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  796. return false;
  797. }
  798. return true;
  799. }
  800. function setAlbum($albumID) {
  801. # Functions sets the parent album of a photo
  802. # Returns the following:
  803. # (boolean) true = Success
  804. # (boolean) false = Failure
  805. # Check dependencies
  806. self::dependencies(isset($this->database, $this->photoIDs));
  807. # Call plugins
  808. $this->plugins(__METHOD__, 0, func_get_args());
  809. # Set album
  810. $query = Database::prepare($this->database, "UPDATE ? SET album = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->photoIDs));
  811. $result = $this->database->query($query);
  812. # Call plugins
  813. $this->plugins(__METHOD__, 1, func_get_args());
  814. if (!$result) {
  815. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  816. return false;
  817. }
  818. return true;
  819. }
  820. public function setTags($tags) {
  821. # Functions sets the tags of a photo
  822. # Excepts the following:
  823. # (string) $tags = Comma separated list of tags with a maximum length of 1000 chars
  824. # Returns the following:
  825. # (boolean) true = Success
  826. # (boolean) false = Failure
  827. # Check dependencies
  828. self::dependencies(isset($this->database, $this->photoIDs));
  829. # Call plugins
  830. $this->plugins(__METHOD__, 0, func_get_args());
  831. # Parse tags
  832. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  833. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  834. if (strlen($tags)>1000) {
  835. Log::notice($this->database, __METHOD__, __LINE__, 'Length of tags higher than 1000');
  836. return false;
  837. }
  838. # Set tags
  839. $query = Database::prepare($this->database, "UPDATE ? SET tags = '?' WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $tags, $this->photoIDs));
  840. $result = $this->database->query($query);
  841. # Call plugins
  842. $this->plugins(__METHOD__, 1, func_get_args());
  843. if (!$result) {
  844. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  845. return false;
  846. }
  847. return true;
  848. }
  849. public function duplicate() {
  850. # Functions duplicates a photo
  851. # Returns the following:
  852. # (boolean) true = Success
  853. # (boolean) false = Failure
  854. # Check dependencies
  855. self::dependencies(isset($this->database, $this->photoIDs));
  856. # Call plugins
  857. $this->plugins(__METHOD__, 0, func_get_args());
  858. # Get photos
  859. $query = Database::prepare($this->database, "SELECT id, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  860. $photos = $this->database->query($query);
  861. if (!$photos) {
  862. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  863. return false;
  864. }
  865. # For each photo
  866. while ($photo = $photos->fetch_object()) {
  867. # Generate id
  868. $id = str_replace('.', '', microtime(true));
  869. while(strlen($id)<14) $id .= 0;
  870. # Duplicate entry
  871. $values = array(LYCHEE_TABLE_PHOTOS, $id, LYCHEE_TABLE_PHOTOS, $photo->id);
  872. $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);
  873. $duplicate = $this->database->query($query);
  874. if (!$duplicate) {
  875. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  876. return false;
  877. }
  878. }
  879. return true;
  880. }
  881. public function delete() {
  882. # Functions deletes a photo with all its data and files
  883. # Returns the following:
  884. # (boolean) true = Success
  885. # (boolean) false = Failure
  886. # Check dependencies
  887. self::dependencies(isset($this->database, $this->photoIDs));
  888. # Call plugins
  889. $this->plugins(__METHOD__, 0, func_get_args());
  890. # Get photos
  891. $query = Database::prepare($this->database, "SELECT id, url, thumbUrl, checksum FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->photoIDs));
  892. $photos = $this->database->query($query);
  893. if (!$photos) {
  894. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  895. return false;
  896. }
  897. # For each photo
  898. while ($photo = $photos->fetch_object()) {
  899. # Check if other photos are referring to this images
  900. # If so, only delete the db entry
  901. if ($this->exists($photo->checksum, $photo->id)===false) {
  902. # Get retina thumb url
  903. $thumbUrl2x = explode(".", $photo->thumbUrl);
  904. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  905. # Delete big
  906. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
  907. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
  908. return false;
  909. }
  910. # Delete medium
  911. if (file_exists(LYCHEE_UPLOADS_MEDIUM . $photo->url)&&!unlink(LYCHEE_UPLOADS_MEDIUM . $photo->url)) {
  912. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/medium/');
  913. return false;
  914. }
  915. # Delete thumb
  916. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
  917. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
  918. return false;
  919. }
  920. # Delete thumb@2x
  921. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
  922. Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
  923. return false;
  924. }
  925. }
  926. # Delete db entry
  927. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id = '?'", array(LYCHEE_TABLE_PHOTOS, $photo->id));
  928. $delete = $this->database->query($query);
  929. if (!$delete) {
  930. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  931. return false;
  932. }
  933. }
  934. # Call plugins
  935. $this->plugins(__METHOD__, 1, func_get_args());
  936. return true;
  937. }
  938. }
  939. ?>