oc_photo.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. function bailOut($msg) {
  26. OCP\JSON::error(array('data' => array('message' => $msg)));
  27. OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::ERROR);
  28. exit();
  29. }
  30. if(!isset($_GET['id'])) {
  31. bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
  32. }
  33. if(!isset($_GET['path'])) {
  34. bailOut(OC_Contacts_App::$l10n->t('No photo path was submitted.'));
  35. }
  36. $localpath = OC_Filesystem::getLocalFile($_GET['path']);
  37. $tmpkey = 'contact-photo-'.$_GET['id'];
  38. if(!file_exists($localpath)) {
  39. bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:').$localpath);
  40. }
  41. $image = new OC_Image();
  42. if(!$image) {
  43. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  44. }
  45. if(!$image->loadFromFile($localpath)) {
  46. bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
  47. }
  48. if($image->width() > 400 || $image->height() > 400) {
  49. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
  50. }
  51. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
  52. OCP\Util::writeLog('contacts','ajax/oc_photo.php: Couldn\'t save correct image orientation: '.$localpath, OCP\Util::DEBUG);
  53. }
  54. if(OC_Cache::set($tmpkey, $image->data(), 600)) {
  55. OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
  56. exit();
  57. } else {
  58. bailOut('Couldn\'t save temporary image: '.$tmpkey);
  59. }
  60. ?>