Photo.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. <?php
  2. ###
  3. # @name Photo Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Photo extends Module {
  9. private $database = null;
  10. private $settings = null;
  11. private $photoIDs = null;
  12. public function __construct($database, $plugins, $settings, $photoIDs) {
  13. # Init vars
  14. $this->database = $database;
  15. $this->plugins = $plugins;
  16. $this->settings = $settings;
  17. $this->photoIDs = $photoIDs;
  18. return true;
  19. }
  20. public function add($files, $albumID, $description = '', $tags = '') {
  21. if (!isset($this->database)) return false;
  22. # Call plugins
  23. $this->plugins(__METHOD__, 0, func_get_args());
  24. switch($albumID) {
  25. case 's':
  26. # s for public (share)
  27. $public = 1;
  28. $star = 0;
  29. $albumID = 0;
  30. break;
  31. case 'f':
  32. # f for starred (fav)
  33. $star = 1;
  34. $public = 0;
  35. $albumID = 0;
  36. break;
  37. default:
  38. $star = 0;
  39. $public = 0;
  40. break;
  41. }
  42. foreach ($files as $file) {
  43. if ($file['type']!=='image/jpeg'&&
  44. $file['type']!=='image/png'&&
  45. $file['type']!=='image/gif')
  46. continue;
  47. $id = str_replace('.', '', microtime(true));
  48. while(strlen($id)<14) $id .= 0;
  49. $tmp_name = $file['tmp_name'];
  50. $extension = array_reverse(explode('.', $file['name']));
  51. $extension = $extension[0];
  52. $photo_name = md5($id) . ".$extension";
  53. $path = LYCHEE_UPLOADS_BIG . $photo_name;
  54. # Import if not uploaded via web
  55. if (!is_uploaded_file($tmp_name)) {
  56. if (!@copy($tmp_name, $path)) exit('Error: Could not copy photo to uploads!');
  57. else @unlink($tmp_name);
  58. } else {
  59. if (!@move_uploaded_file($tmp_name, $path)) exit('Error: Could not move photo to uploads!');
  60. }
  61. # Read infos
  62. $info = $this->getInfo($path);
  63. # Use title of file if IPTC title missing
  64. if ($info['title']==='') $info['title'] = mysqli_real_escape_string($this->database, substr(basename($file['name'], ".$extension"), 0, 30));
  65. # Use description parameter if set
  66. if ($description==='') $description = $info['description'];
  67. # Set orientation based on EXIF data
  68. if ($file['type']==='image/jpeg'&&isset($info['orientation'])&&$info['orientation']!==''&&isset($info['width'])&&isset($info['height'])) {
  69. if (!$this->adjustFile($path, $info)) exit('Error: Could not adjust photo!');
  70. }
  71. # Set original date
  72. if ($info['takestamp']!=='') @touch($path, $info['takestamp']);
  73. # Create Thumb
  74. if (!$this->createThumb($path, $photo_name)) exit('Error: Could not create thumbnail for photo!');
  75. # Save to DB
  76. $query = "INSERT INTO lychee_photos (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star)
  77. VALUES (
  78. '" . $id . "',
  79. '" . $info['title'] . "',
  80. '" . $photo_name . "',
  81. '" . $description . "',
  82. '" . $tags . "',
  83. '" . $info['type'] . "',
  84. '" . $info['width'] . "',
  85. '" . $info['height'] . "',
  86. '" . $info['size'] . "',
  87. '" . $info['iso'] . "',
  88. '" . $info['aperture'] . "',
  89. '" . $info['make'] . "',
  90. '" . $info['model'] . "',
  91. '" . $info['shutter'] . "',
  92. '" . $info['focal'] . "',
  93. '" . $info['takestamp'] . "',
  94. '" . md5($id) . ".jpeg',
  95. '" . $albumID . "',
  96. '" . $public . "',
  97. '" . $star . "');";
  98. $result = $this->database->query($query);
  99. if (!$result) exit('Error: Could not save photo in database!');
  100. }
  101. # Call plugins
  102. $this->plugins(__METHOD__, 1, func_get_args());
  103. return true;
  104. }
  105. private function createThumb($url, $filename, $width = 200, $height = 200) {
  106. if (!isset($this->settings, $url, $filename)) return false;
  107. # Call plugins
  108. $this->plugins(__METHOD__, 0, func_get_args());
  109. $info = getimagesize($url);
  110. $photoName = explode(".", $filename);
  111. $newUrl = LYCHEE_UPLOADS_THUMB . $photoName[0] . '.jpeg';
  112. $newUrl2x = LYCHEE_UPLOADS_THUMB . $photoName[0] . '@2x.jpeg';
  113. # create thumbnails with Imagick
  114. if(extension_loaded('imagick')) {
  115. # Read image
  116. $thumb = new Imagick();
  117. $thumb->readImage($url);
  118. $thumb->setImageCompressionQuality($this->settings['thumbQuality']);
  119. $thumb->setImageFormat('jpeg');
  120. # Copy image for 2nd thumb version
  121. $thumb2x = clone $thumb;
  122. # Create 1st version
  123. $thumb->cropThumbnailImage($width, $height);
  124. $thumb->writeImage($newUrl);
  125. $thumb->clear();
  126. $thumb->destroy();
  127. # Create 2nd version
  128. $thumb2x->cropThumbnailImage($width*2, $height*2);
  129. $thumb2x->writeImage($newUrl2x);
  130. $thumb2x->clear();
  131. $thumb2x->destroy();
  132. } else {
  133. # Set position and size
  134. $thumb = imagecreatetruecolor($width, $height);
  135. $thumb2x = imagecreatetruecolor($width*2, $height*2);
  136. if ($info[0]<$info[1]) {
  137. $newSize = $info[0];
  138. $startWidth = 0;
  139. $startHeight = $info[1]/2 - $info[0]/2;
  140. } else {
  141. $newSize = $info[1];
  142. $startWidth = $info[0]/2 - $info[1]/2;
  143. $startHeight = 0;
  144. }
  145. # Fallback for older version
  146. if ($info['mime']==='image/webp'&&floatval(phpversion())<5.5) return false;
  147. # Create new image
  148. switch($info['mime']) {
  149. case 'image/jpeg': $sourceImg = imagecreatefromjpeg($url); break;
  150. case 'image/png': $sourceImg = imagecreatefrompng($url); break;
  151. case 'image/gif': $sourceImg = imagecreatefromgif($url); break;
  152. case 'image/webp': $sourceImg = imagecreatefromwebp($url); break;
  153. default: return false; break;
  154. }
  155. # Create thumb
  156. imagecopyresampled($thumb, $sourceImg, 0, 0, $startWidth, $startHeight, $width, $height, $newSize, $newSize);
  157. imagejpeg($thumb, $newUrl, $this->settings['thumbQuality']);
  158. imagedestroy($thumb);
  159. # Create retina thumb
  160. imagecopyresampled($thumb2x, $sourceImg, 0, 0, $startWidth, $startHeight, $width*2, $height*2, $newSize, $newSize);
  161. imagejpeg($thumb2x, $newUrl2x, $this->settings['thumbQuality']);
  162. imagedestroy($thumb2x);
  163. # Free memory
  164. imagedestroy($sourceImg);
  165. }
  166. # Call plugins
  167. $this->plugins(__METHOD__, 1, func_get_args());
  168. return true;
  169. }
  170. private function adjustFile($path, $info) {
  171. if (!isset($path, $info)) return false;
  172. # Call plugins
  173. $this->plugins(__METHOD__, 0, func_get_args());
  174. if (extension_loaded('imagick')) {
  175. $rotateImage = 0;
  176. switch ($info['orientation']) {
  177. case 3:
  178. $rotateImage = 180;
  179. $imageOrientation = 1;
  180. break;
  181. case 6:
  182. $rotateImage = 90;
  183. $imageOrientation = 1;
  184. break;
  185. case 8:
  186. $rotateImage = 270;
  187. $imageOrientation = 1;
  188. break;
  189. }
  190. if ($rotateImage!==0) {
  191. $image = new Imagick();
  192. $image->readImage($path);
  193. $image->rotateImage(new ImagickPixel(), $rotateImage);
  194. $image->setImageOrientation($imageOrientation);
  195. $image->writeImage($path);
  196. $image->clear();
  197. $image->destroy();
  198. }
  199. } else {
  200. $newWidth = $info['width'];
  201. $newHeight = $info['height'];
  202. $process = false;
  203. $sourceImg = imagecreatefromjpeg($path);
  204. switch ($info['orientation']) {
  205. case 2:
  206. # mirror
  207. # not yet implemented
  208. break;
  209. case 3:
  210. $process = true;
  211. $sourceImg = imagerotate($sourceImg, -180, 0);
  212. break;
  213. case 4:
  214. # rotate 180 and mirror
  215. # not yet implemented
  216. break;
  217. case 5:
  218. # rotate 90 and mirror
  219. # not yet implemented
  220. break;
  221. case 6:
  222. $process = true;
  223. $sourceImg = imagerotate($sourceImg, -90, 0);
  224. $newWidth = $info['height'];
  225. $newHeight = $info['width'];
  226. break;
  227. case 7:
  228. # rotate -90 and mirror
  229. # not yet implemented
  230. break;
  231. case 8:
  232. $process = true;
  233. $sourceImg = imagerotate($sourceImg, 90, 0);
  234. $newWidth = $info['height'];
  235. $newHeight = $info['width'];
  236. break;
  237. }
  238. # Need to adjust photo?
  239. if ($process===true) {
  240. # Recreate photo
  241. $newSourceImg = imagecreatetruecolor($newWidth, $newHeight);
  242. imagecopyresampled($newSourceImg, $sourceImg, 0, 0, 0, 0, $newWidth, $newHeight, $newWidth, $newHeight);
  243. imagejpeg($newSourceImg, $path, 100);
  244. # Free memory
  245. imagedestroy($sourceImg);
  246. imagedestroy($newSourceImg);
  247. }
  248. }
  249. # Call plugins
  250. $this->plugins(__METHOD__, 1, func_get_args());
  251. return true;
  252. }
  253. public function get($albumID) {
  254. if (!isset($this->database, $this->photoIDs)) return false;
  255. # Call plugins
  256. $this->plugins(__METHOD__, 0, func_get_args());
  257. # Get photo
  258. $photos = $this->database->query("SELECT * FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  259. $photo = $photos->fetch_assoc();
  260. # Parse photo
  261. $photo['sysdate'] = date('d M. Y', substr($photo['id'], 0, -4));
  262. if (strlen($photo['takestamp'])>1) $photo['takedate'] = date('d M. Y', $photo['takestamp']);
  263. if ($albumID!='false') {
  264. if ($photo['album']!=0) {
  265. # Get album
  266. $albums = $this->database->query("SELECT public FROM lychee_albums WHERE id = '" . $photo['album'] . " LIMIT 1';");
  267. $album = $albums->fetch_assoc();
  268. # Parse album
  269. $photo['public'] = ($album['public']=='1' ? '2' : $photo['public']);
  270. }
  271. $photo['original_album'] = $photo['album'];
  272. $photo['album'] = $albumID;
  273. }
  274. # Call plugins
  275. $this->plugins(__METHOD__, 1, func_get_args());
  276. return $photo;
  277. }
  278. private function getInfo($url) {
  279. if (!isset($this->database, $url)) return false;
  280. # Call plugins
  281. $this->plugins(__METHOD__, 0, func_get_args());
  282. $iptcArray = array();
  283. $info = getimagesize($url, $iptcArray);
  284. # General information
  285. $return['type'] = $info['mime'];
  286. $return['width'] = $info[0];
  287. $return['height'] = $info[1];
  288. # Size
  289. $size = filesize($url)/1024;
  290. if ($size>=1024) $return['size'] = round($size/1024, 1) . ' MB';
  291. else $return['size'] = round($size, 1) . ' KB';
  292. # IPTC Metadata Fallback
  293. $return['title'] = '';
  294. $return['description'] = '';
  295. # IPTC Metadata
  296. if(isset($iptcArray['APP13'])) {
  297. $iptcInfo = iptcparse($iptcArray['APP13']);
  298. if (is_array($iptcInfo)) {
  299. $temp = @$iptcInfo['2#105'][0];
  300. if (isset($temp)&&strlen($temp)>0) $return['title'] = $temp;
  301. $temp = @$iptcInfo['2#120'][0];
  302. if (isset($temp)&&strlen($temp)>0) $return['description'] = $temp;
  303. }
  304. }
  305. # EXIF Metadata Fallback
  306. $return['orientation'] = '';
  307. $return['iso'] = '';
  308. $return['aperture'] = '';
  309. $return['make'] = '';
  310. $return['model'] = '';
  311. $return['shutter'] = '';
  312. $return['focal'] = '';
  313. $return['takestamp'] = '';
  314. # Read EXIF
  315. if ($info['mime']=='image/jpeg') $exif = @exif_read_data($url, 'EXIF', 0);
  316. else $exif = false;
  317. # EXIF Metadata
  318. if ($exif!==false) {
  319. if (isset($exif['Orientation'])) $return['orientation'] = $exif['Orientation'];
  320. else if (isset($exif['IFD0']['Orientation'])) $return['orientation'] = $exif['IFD0']['Orientation'];
  321. $temp = @$exif['ISOSpeedRatings'];
  322. if (isset($temp)) $return['iso'] = $temp;
  323. $temp = @$exif['COMPUTED']['ApertureFNumber'];
  324. if (isset($temp)) $return['aperture'] = $temp;
  325. $temp = @$exif['Make'];
  326. if (isset($temp)) $return['make'] = $exif['Make'];
  327. $temp = @$exif['Model'];
  328. if (isset($temp)) $return['model'] = $temp;
  329. $temp = @$exif['ExposureTime'];
  330. if (isset($temp)) $return['shutter'] = $exif['ExposureTime'] . ' Sec.';
  331. $temp = @$exif['FocalLength'];
  332. if (isset($temp)) $return['focal'] = ($temp/1) . ' mm';
  333. $temp = @$exif['DateTimeOriginal'];
  334. if (isset($temp)) $return['takestamp'] = strtotime($temp);
  335. }
  336. # Security
  337. foreach(array_keys($return) as $key) $return[$key] = mysqli_real_escape_string($this->database, $return[$key]);
  338. # Call plugins
  339. $this->plugins(__METHOD__, 1, func_get_args());
  340. return $return;
  341. }
  342. public function getArchive() {
  343. if (!isset($this->database, $this->photoIDs)) return false;
  344. # Call plugins
  345. $this->plugins(__METHOD__, 0, func_get_args());
  346. # Get photo
  347. $photos = $this->database->query("SELECT title, url FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  348. $photo = $photos->fetch_object();
  349. # Get extension
  350. $extension = array_reverse(explode('.', $photo->url));
  351. # Parse title
  352. if ($photo->title=='') $photo->title = 'Untitled';
  353. # Set headers
  354. header("Content-Type: application/octet-stream");
  355. header("Content-Disposition: attachment; filename=\"$photo->title.$extension[0]\"");
  356. header("Content-Length: " . filesize(LYCHEE_UPLOADS_BIG . $photo->url));
  357. # Send file
  358. readfile(LYCHEE_UPLOADS_BIG . $photo->url);
  359. # Call plugins
  360. $this->plugins(__METHOD__, 1, func_get_args());
  361. return true;
  362. }
  363. function setTitle($title) {
  364. if (!isset($this->database, $this->photoIDs)) return false;
  365. # Call plugins
  366. $this->plugins(__METHOD__, 0, func_get_args());
  367. # Parse
  368. if (strlen($title)>50) $title = substr($title, 0, 50);
  369. # Set title
  370. $result = $this->database->query("UPDATE lychee_photos SET title = '$title' WHERE id IN ($this->photoIDs);");
  371. # Call plugins
  372. $this->plugins(__METHOD__, 1, func_get_args());
  373. if (!$result) return false;
  374. return true;
  375. }
  376. function setDescription($description) {
  377. if (!isset($this->database, $this->photoIDs)) return false;
  378. # Call plugins
  379. $this->plugins(__METHOD__, 0, func_get_args());
  380. # Parse
  381. $description = htmlentities($description);
  382. if (strlen($description)>1000) $description = substr($description, 0, 1000);
  383. # Set description
  384. $result = $this->database->query("UPDATE lychee_photos SET description = '$description' WHERE id IN ('$this->photoIDs');");
  385. # Call plugins
  386. $this->plugins(__METHOD__, 1, func_get_args());
  387. if (!$result) return false;
  388. return true;
  389. }
  390. public function setStar() {
  391. if (!isset($this->database, $this->photoIDs)) return false;
  392. # Call plugins
  393. $this->plugins(__METHOD__, 0, func_get_args());
  394. # Init vars
  395. $error = false;
  396. # Get photos
  397. $photos = $this->database->query("SELECT id, star FROM lychee_photos WHERE id IN ($this->photoIDs);");
  398. # For each photo
  399. while ($photo = $photos->fetch_object()) {
  400. # Invert star
  401. $star = ($photo->star==0 ? 1 : 0);
  402. # Set star
  403. $star = $this->database->query("UPDATE lychee_photos SET star = '$star' WHERE id = '$photo->id';");
  404. if (!$star) $error = true;
  405. }
  406. # Call plugins
  407. $this->plugins(__METHOD__, 1, func_get_args());
  408. if ($error) return false;
  409. return true;
  410. }
  411. function getPublic($password) {
  412. if (!isset($this->database, $this->photoIDs)) return false;
  413. # Call plugins
  414. $this->plugins(__METHOD__, 0, func_get_args());
  415. # Get photo
  416. $photos = $this->database->query("SELECT public, album FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  417. $photo = $photos->fetch_object();
  418. # Check if public
  419. if ($photo->public==1) return true;
  420. else {
  421. $album = new Album($this->database, null, null, $photo->album);
  422. $acP = $album->checkPassword($password);
  423. $agP = $album->getPublic();
  424. if ($acP===true&&$agP===true) return true;
  425. }
  426. # Call plugins
  427. $this->plugins(__METHOD__, 1, func_get_args());
  428. return false;
  429. }
  430. public function setPublic() {
  431. if (!isset($this->database, $this->photoIDs)) return false;
  432. # Call plugins
  433. $this->plugins(__METHOD__, 0, func_get_args());
  434. # Get public
  435. $photos = $this->database->query("SELECT public FROM lychee_photos WHERE id = '$this->photoIDs' LIMIT 1;");
  436. $photo = $photos->fetch_object();
  437. # Invert public
  438. $public = ($photo->public==0 ? 1 : 0);
  439. # Set public
  440. $result = $this->database->query("UPDATE lychee_photos SET public = '$public' WHERE id = '$this->photoIDs';");
  441. # Call plugins
  442. $this->plugins(__METHOD__, 1, func_get_args());
  443. if (!$result) return false;
  444. return true;
  445. }
  446. function setAlbum($albumID) {
  447. if (!isset($this->database, $this->photoIDs)) return false;
  448. # Call plugins
  449. $this->plugins(__METHOD__, 0, func_get_args());
  450. # Set album
  451. $result = $this->database->query("UPDATE lychee_photos SET album = '$albumID' WHERE id IN ($this->photoIDs);");
  452. # Call plugins
  453. $this->plugins(__METHOD__, 1, func_get_args());
  454. if (!$result) return false;
  455. return true;
  456. }
  457. public function setTags($tags) {
  458. if (!isset($this->database, $this->photoIDs)) return false;
  459. # Call plugins
  460. $this->plugins(__METHOD__, 0, func_get_args());
  461. # Parse tags
  462. $tags = preg_replace('/(\ ,\ )|(\ ,)|(,\ )|(,{1,}\ {0,})|(,$|^,)/', ',', $tags);
  463. $tags = preg_replace('/,$|^,|(\ ){0,}$/', '', $tags);
  464. if (strlen($tags)>1000) return false;
  465. # Set tags
  466. $result = $this->database->query("UPDATE lychee_photos SET tags = '$tags' WHERE id IN ($this->photoIDs);");
  467. # Call plugins
  468. $this->plugins(__METHOD__, 1, func_get_args());
  469. if (!$result) return false;
  470. return true;
  471. }
  472. public function delete() {
  473. if (!isset($this->database, $this->photoIDs)) return false;
  474. # Call plugins
  475. $this->plugins(__METHOD__, 0, func_get_args());
  476. # Get photos
  477. $photos = $this->database->query("SELECT id, url, thumbUrl FROM lychee_photos WHERE id IN ($this->photoIDs);");
  478. # For each photo
  479. while ($photo = $photos->fetch_object()) {
  480. # Get retina thumb url
  481. $thumbUrl2x = explode(".", $photo->thumbUrl);
  482. $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
  483. # Delete files
  484. if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) return false;
  485. if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) return false;
  486. if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) return false;
  487. # Delete db entry
  488. $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");
  489. if (!$delete) return false;
  490. }
  491. # Call plugins
  492. $this->plugins(__METHOD__, 1, func_get_args());
  493. if (!$photos) return false;
  494. return true;
  495. }
  496. }
  497. ?>