oc_photo.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * ownCloud - Addressbook
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 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. // Check if we are a user
  23. OCP\JSON::checkLoggedIn();
  24. OCP\JSON::checkAppEnabled('contacts');
  25. require_once 'loghandler.php';
  26. if(!isset($_GET['id'])) {
  27. bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
  28. }
  29. if(!isset($_GET['path'])) {
  30. bailOut(OC_Contacts_App::$l10n->t('No photo path was submitted.'));
  31. }
  32. $localpath = OC_Filesystem::getLocalFile($_GET['path']);
  33. $tmpkey = 'contact-photo-'.$_GET['id'];
  34. if(!file_exists($localpath)) {
  35. bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:').$localpath);
  36. }
  37. $image = new OC_Image();
  38. if(!$image) {
  39. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  40. }
  41. if(!$image->loadFromFile($localpath)) {
  42. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  43. }
  44. if($image->width() > 400 || $image->height() > 400) {
  45. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
  46. }
  47. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
  48. OCP\Util::writeLog('contacts',
  49. 'ajax/oc_photo.php: Couldn\'t save correct image orientation: '.$localpath,
  50. OCP\Util::DEBUG);
  51. }
  52. if(OC_Cache::set($tmpkey, $image->data(), 600)) {
  53. OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
  54. exit();
  55. } else {
  56. bailOut('Couldn\'t save temporary image: '.$tmpkey);
  57. }