attachment_thumbnail.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Show notice attachments
  19. *
  20. * @category Personal
  21. * @package StatusNet
  22. * @author Evan Prodromou <evan@status.net>
  23. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  24. * @link http://status.net/
  25. */
  26. class Attachment_thumbnailAction extends AttachmentAction
  27. {
  28. protected $thumb_w = null; // max width
  29. protected $thumb_h = null; // max height
  30. protected $thumb_c = null; // crop?
  31. protected function doPreparation()
  32. {
  33. parent::doPreparation();
  34. $this->thumb_w = $this->int('w');
  35. $this->thumb_h = $this->int('h');
  36. $this->thumb_c = $this->boolean('c');
  37. }
  38. /**
  39. * Show an inline representation of an attachment of the size
  40. * requested in the GET variables (read in the constructor). Tries
  41. * to send the most appropriate file with the correct size and
  42. * headers or displays an error if it's not possible.
  43. * @throws ClientException
  44. * @throws ReflectionException
  45. * @throws ServerException
  46. */
  47. public function showPage(): void
  48. {
  49. // Returns a File_thumbnail object or throws exception if not available
  50. try {
  51. $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
  52. $file = $thumbnail->getFile();
  53. } catch (UseFileAsThumbnailException $e) {
  54. // With this exception, the file exists locally
  55. $file = $e->file;
  56. } catch (FileNotFoundException $e) {
  57. $this->clientError(_m('No such attachment'), 404);
  58. }
  59. // Disable errors, to not mess with the file contents (suppress errors in case access to this
  60. // function is blocked, like in some shared hosts). Automatically reset at the end of the
  61. // script execution, and we don't want to have any more errors until then, so don't reset it
  62. @ini_set('display_errors', 0);
  63. common_send_file($this->filepath, $this->mimetype, $this->filename, 'inline');
  64. }
  65. }