attachment_thumbnail.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 GNUsocial
  22. * @author Evan Prodromou <evan@status.net>
  23. * @copyright 2008-2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. class Attachment_thumbnailAction extends Attachment_viewAction
  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. $filename = $this->filename;
  51. $filepath = $this->filepath;
  52. try {
  53. $thumbnail = $this->attachment->getThumbnail($this->thumb_w, $this->thumb_h, $this->thumb_c);
  54. $filename = $thumbnail->getFilename();
  55. $filepath = $thumbnail->getPath();
  56. } catch (UseFileAsThumbnailException $e) {
  57. // With this exception, the file exists locally $e->file;
  58. } catch (FileNotFoundException $e) {
  59. $this->clientError(_m('No such attachment'), 404);
  60. } catch (Exception $e) {
  61. if (is_null($filepath)) {
  62. $this->clientError(_m('No such thumbnail'), 404);
  63. }
  64. // Remote file
  65. }
  66. // Disable errors, to not mess with the file contents (suppress errors in case access to this
  67. // function is blocked, like in some shared hosts). Automatically reset at the end of the
  68. // script execution, and we don't want to have any more errors until then, so don't reset it
  69. @ini_set('display_errors', 0);
  70. common_send_file($filepath, image_type_to_mime_type(IMAGETYPE_WEBP), $filename, 'inline');
  71. }
  72. }