MediaTransformer.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace App\Transformer\Api\Mastodon\v1;
  3. use App\Media;
  4. use League\Fractal;
  5. class MediaTransformer extends Fractal\TransformerAbstract
  6. {
  7. public function transform(Media $media)
  8. {
  9. $res = [
  10. 'id' => (string) $media->id,
  11. 'type' => lcfirst($media->activityVerb()),
  12. 'url' => $media->url(),
  13. 'preview_url' => $media->thumbnailUrl(),
  14. 'remote_url' => $media->remote_url,
  15. 'text_url' => $media->url(),
  16. 'description' => $media->caption,
  17. 'blurhash' => $media->blurhash ?? 'U4Rfzst8?bt7ogayj[j[~pfQ9Goe%Mj[WBay'
  18. ];
  19. if($media->width && $media->height) {
  20. $res['meta'] = [
  21. 'focus' => [
  22. 'x' => 0,
  23. 'y' => 0
  24. ],
  25. 'original' => [
  26. 'width' => $media->width,
  27. 'height' => $media->height,
  28. 'size' => "{$media->width}x{$media->height}",
  29. 'aspect' => $media->width / $media->height
  30. ]
  31. ];
  32. }
  33. return $res;
  34. }
  35. }