Photo.php 34 KB

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