Resizable.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Dcat\Admin\Traits;
  3. use Illuminate\Support\Str;
  4. trait Resizable
  5. {
  6. /**
  7. * Method for returning specific thumbnail for model.
  8. *
  9. * @param string $type
  10. * @param string $attribute
  11. *
  12. * @return string|null
  13. */
  14. public function thumbnail($type, $attribute = 'image', $disk = null)
  15. {
  16. // Return empty string if the field not found
  17. if (! isset($this->attributes[$attribute])) {
  18. return '';
  19. }
  20. // We take image from posts field
  21. $image = $this->attributes[$attribute];
  22. $thumbnail = $this->getThumbnailPath($image, $type);
  23. return $thumbnail;
  24. }
  25. /**
  26. * Generate thumbnail URL.
  27. *
  28. * @param $image
  29. * @param $type
  30. *
  31. * @return string
  32. */
  33. public function getThumbnailPath($image, $type)
  34. {
  35. // We need to get extension type ( .jpeg , .png ...)
  36. $ext = pathinfo($image, PATHINFO_EXTENSION);
  37. // We remove extension from file name so we can append thumbnail type
  38. $name = Str::replaceLast('.'.$ext, '', $image);
  39. // We merge original name + type + extension
  40. return $name.'-'.$type.'.'.$ext;
  41. }
  42. }