ImageThumbnail.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\ImageEncoder\Controller;
  19. use App\Core\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\GSFile;
  22. use App\Entity\AttachmentThumbnail;
  23. use App\Util\Common;
  24. use Symfony\Component\HttpFoundation\Request;
  25. class ImageThumbnail extends Controller
  26. {
  27. /**
  28. * Get a thumbnail for the attachment with id $id
  29. */
  30. public function thumbnail(Request $request, int $id)
  31. {
  32. $attachment = DB::findOneBy('attachment', ['id' => $id]);
  33. if (!is_null($attachment->getScope())) {
  34. // && ($attachment->scope | VisibilityScope::PUBLIC) != 0
  35. // $user = Common::ensureLoggedIn();
  36. assert(false, 'Attachment scope not implemented');
  37. }
  38. // TODO rate limit
  39. $max_width = Common::config('thumbnail', 'width');
  40. $max_height = Common::config('thumbnail', 'height');
  41. $width = Common::clamp($this->int('w') ?: $max_width, min: 0, max: $max_width);
  42. $height = Common::clamp($this->int('h') ?: $max_height, min: 0, max: $max_height);
  43. $crop = $this->bool('c') ?: false;
  44. $thumbnail = AttachmentThumbnail::getOrCreate(attachment: $attachment, width: $width, height: $height, crop: $crop);
  45. $filename = $thumbnail->getFilename();
  46. $path = $thumbnail->getPath();
  47. return GSFile::sendFile(filepath: $path, mimetype: $attachment->getMimetype(), output_filename: $filename, disposition: 'inline');
  48. }
  49. }