photo.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * ownCloud - gallery application
  4. *
  5. * @author Bartek Przybylski
  6. * @copyright 2012 Bartek Przybylski bart.p.pl@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class OC_Gallery_Photo {
  23. public static function create($albumId, $img){
  24. $stmt = OCP\DB::prepare('INSERT INTO *PREFIX*gallery_photos (album_id, file_path) VALUES (?, ?)');
  25. $stmt->execute(array($albumId, $img));
  26. }
  27. public static function find($albumId, $img=null){
  28. $sql = 'SELECT * FROM *PREFIX*gallery_photos WHERE album_id = ?';
  29. $args = array($albumId);
  30. if (!is_null($img)){
  31. $sql .= ' AND file_path = ?';
  32. $args[] = $img;
  33. }
  34. $stmt = OCP\DB::prepare($sql);
  35. return $stmt->execute($args);
  36. }
  37. public static function findForAlbum($owner, $album_name){
  38. $stmt = OCP\DB::prepare('SELECT *'
  39. .' FROM *PREFIX*gallery_photos photos,'
  40. .' *PREFIX*gallery_albums albums'
  41. .' WHERE albums.uid_owner = ?'
  42. .' AND albums.album_name = ?'
  43. .' AND photos.album_id = albums.album_id');
  44. return $stmt->execute(array($owner, $album_name));
  45. }
  46. public static function removeByPath($path, $album_id) {
  47. $stmt = OCP\DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE file_path LIKE ? and album_id = ?');
  48. $stmt->execute(array($path, $album_id));
  49. }
  50. public static function removeById($id) {
  51. $stmt = OCP\DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE photo_id = ?');
  52. $stmt->execute(array($id));
  53. }
  54. public static function removeByAlbumId($albumid) {
  55. $stmt = OCP\DB::prepare('DELETE FROM *PREFIX*gallery_photos WHERE album_id = ?');
  56. $stmt->execute(array($albumid));
  57. }
  58. public static function changePath($oldAlbumId, $newAlbumId, $oldpath, $newpath) {
  59. $stmt = OCP\DB::prepare("UPDATE *PREFIX*gallery_photos SET file_path = ?, album_id = ? WHERE album_id = ? and file_path = ?");
  60. $stmt->execute(array($newpath, $newAlbumId, $oldAlbumId, $oldpath));
  61. }
  62. public static function getThumbnail($image_name, $owner = null) {
  63. if (!$owner) $owner = OCP\USER::getUser();
  64. $view = OCP\Files::getStorage('gallery');
  65. $save_dir = dirname($image_name);
  66. if (!$view->is_dir($save_dir)) {
  67. $view->mkdir($save_dir);
  68. }
  69. $view->chroot($view->getRoot().'/'.$save_dir);
  70. $thumb_file = basename($image_name);
  71. if ($view->file_exists($thumb_file)) {
  72. $image = new OC_Image($view->fopen($thumb_file, 'r'));
  73. } else {
  74. $image_path = OC_Filesystem::getLocalFile($image_name);
  75. if(!file_exists($image_path)) {
  76. return null;
  77. }
  78. $image = new OC_Image($image_path);
  79. if ($image->valid()) {
  80. $image->centerCrop(200);
  81. $image->fixOrientation();
  82. $image->save($view->getLocalFile($thumb_file));
  83. }
  84. }
  85. if ($image->valid()) {
  86. return $image;
  87. }else{
  88. $image->destroy();
  89. }
  90. return null;
  91. }
  92. public static function getGalleryRoot() {
  93. return OCP\Config::getUserValue(OCP\USER::getUser(), 'gallery', 'root', '');
  94. }
  95. }