thumbnail.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2011-2012 Thomas Tanghus <thomas@tanghus.net>
  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 Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. OCP\JSON::checkLoggedIn();
  23. //OCP\User::checkLoggedIn();
  24. OCP\App::checkAppEnabled('contacts');
  25. session_write_close();
  26. function getStandardImage() {
  27. OCP\Response::enableCaching();
  28. OCP\Response::redirect(OCP\Util::imagePath('contacts', 'person.png'));
  29. }
  30. if(!extension_loaded('gd') || !function_exists('gd_info')) {
  31. OCP\Util::writeLog('contacts',
  32. 'thumbnail.php. GD module not installed', OCP\Util::DEBUG);
  33. getStandardImage();
  34. exit();
  35. }
  36. $id = $_GET['id'];
  37. $caching = null;
  38. $contact = OC_Contacts_App::getContactVCard($id);
  39. // invalid vcard
  40. if(is_null($contact)) {
  41. OCP\Util::writeLog('contacts',
  42. 'thumbnail.php. The VCard for ID ' . $id . ' is not RFC compatible',
  43. OCP\Util::ERROR);
  44. getStandardImage();
  45. exit();
  46. }
  47. OCP\Response::enableCaching($caching);
  48. OC_Contacts_App::setLastModifiedHeader($contact);
  49. $thumbnail_size = 23;
  50. // Find the photo from VCard.
  51. $image = new OC_Image();
  52. $photo = $contact->getAsString('PHOTO');
  53. if($photo) {
  54. if($image->loadFromBase64($photo)) {
  55. if($image->centerCrop()) {
  56. if($image->resize($thumbnail_size)) {
  57. $modified = OC_Contacts_App::lastModified($contact);
  58. // Force refresh if modified within the last minute.
  59. if(!is_null($modified)) {
  60. $caching = (time() - $modified->format('U') > 60) ? null : 0;
  61. }
  62. OCP\Response::enableCaching($caching);
  63. if(!is_null($modified)) {
  64. OCP\Response::setLastModifiedHeader($modified);
  65. }
  66. OCP\Response::setETagHeader(md5($photo));
  67. if($image->show()) {
  68. exit();
  69. } else {
  70. OCP\Util::writeLog('contacts',
  71. 'thumbnail.php. Couldn\'t display thumbnail for ID ' . $id,
  72. OCP\Util::ERROR);
  73. }
  74. } else {
  75. OCP\Util::writeLog('contacts',
  76. 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id,
  77. OCP\Util::ERROR);
  78. }
  79. }else{
  80. OCP\Util::writeLog('contacts',
  81. 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id,
  82. OCP\Util::ERROR);
  83. }
  84. } else {
  85. OCP\Util::writeLog('contacts',
  86. 'thumbnail.php. Couldn\'t load image string for ID ' . $id,
  87. OCP\Util::ERROR);
  88. }
  89. }
  90. getStandardImage();