crop.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Avatar crop action
  4. *
  5. */
  6. $guid = get_input('guid');
  7. $owner = get_entity($guid);
  8. if (!$owner || !($owner instanceof ElggUser) || !$owner->canEdit()) {
  9. register_error(elgg_echo('avatar:crop:fail'));
  10. forward(REFERER);
  11. }
  12. $x1 = (int) get_input('x1', 0);
  13. $y1 = (int) get_input('y1', 0);
  14. $x2 = (int) get_input('x2', 0);
  15. $y2 = (int) get_input('y2', 0);
  16. $filehandler = new ElggFile();
  17. $filehandler->owner_guid = $owner->getGUID();
  18. $filehandler->setFilename("profile/" . $owner->guid . "master" . ".jpg");
  19. $filename = $filehandler->getFilenameOnFilestore();
  20. // ensuring the avatar image exists in the first place
  21. if (!file_exists($filename)) {
  22. register_error(elgg_echo('avatar:crop:fail'));
  23. forward(REFERER);
  24. }
  25. $icon_sizes = elgg_get_config('icon_sizes');
  26. unset($icon_sizes['master']);
  27. // get the images and save their file handlers into an array
  28. // so we can do clean up if one fails.
  29. $files = array();
  30. foreach ($icon_sizes as $name => $size_info) {
  31. $resized = get_resized_image_from_existing_file($filename, $size_info['w'], $size_info['h'], $size_info['square'], $x1, $y1, $x2, $y2, $size_info['upscale']);
  32. if ($resized) {
  33. //@todo Make these actual entities. See exts #348.
  34. $file = new ElggFile();
  35. $file->owner_guid = $guid;
  36. $file->setFilename("profile/{$guid}{$name}.jpg");
  37. $file->open('write');
  38. $file->write($resized);
  39. $file->close();
  40. $files[] = $file;
  41. } else {
  42. // cleanup on fail
  43. foreach ($files as $file) {
  44. $file->delete();
  45. }
  46. register_error(elgg_echo('avatar:resize:fail'));
  47. forward(REFERER);
  48. }
  49. }
  50. $owner->icontime = time();
  51. $owner->x1 = $x1;
  52. $owner->x2 = $x2;
  53. $owner->y1 = $y1;
  54. $owner->y2 = $y2;
  55. system_message(elgg_echo('avatar:crop:success'));
  56. $view = 'river/user/default/profileiconupdate';
  57. elgg_delete_river(array('subject_guid' => $owner->guid, 'view' => $view));
  58. elgg_create_river_item(array(
  59. 'view' => $view,
  60. 'action_type' => 'update',
  61. 'subject_guid' => $owner->guid,
  62. 'object_guid' => $owner->guid,
  63. ));
  64. forward(REFERER);