LocalAdapter.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/7/12 21:55
  6. * Description:
  7. */
  8. namespace common\modules\attachment\components\flysystem;
  9. use League\Flysystem\Adapter\Local;
  10. use yii\imagine\Image;
  11. class LocalAdapter extends Local
  12. {
  13. public $urlPrefix;
  14. public function __construct($root, $url, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS, array $permissions = [])
  15. {
  16. parent::__construct($root, $writeFlags, $linkHandling, $permissions);
  17. $this->urlPrefix = $url;
  18. }
  19. protected $thumbFileRule = '_{w}_{h}';
  20. protected $cropFileRule = '_{w}_{h}_{x}_{y}';
  21. public function thumbnail($path, $width, $height)
  22. {
  23. if (strpos($path, $this->urlPrefix . '/') !== false) {
  24. $path = substr($path, strlen($this->urlPrefix . '/'));
  25. }
  26. $pathinfo = pathinfo($path);
  27. $thumbFile = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . strtr($this->thumbFileRule, ['{w}' => $width, '{h}' => $height]) . '.' . $pathinfo['extension'];
  28. $thumbPath = $this->pathPrefix . $thumbFile;
  29. if (!is_file($thumbPath)) {
  30. Image::thumbnail($this->pathPrefix . $path, $width, $height)->save($thumbPath);
  31. }
  32. return $this->urlPrefix . '/' . $thumbFile;
  33. }
  34. public function crop($path, $width, $height, array $start = [0, 0])
  35. {
  36. if (strpos($path, $this->urlPrefix . '/') !== false) {
  37. $path = substr($path, strlen($this->urlPrefix . '/'));
  38. }
  39. $pathinfo = pathinfo($path);
  40. $cropFile = $pathinfo['dirname'] . '/' . $pathinfo['filename'] . strtr($this->cropFileRule, ['{w}' => $width, '{h}' => $height, '{x}' => $start[0], '{y}' => $start[1]]) . '.' . $pathinfo['extension'];
  41. $cropPath = $this->pathPrefix . $cropFile;
  42. if (!is_file($cropPath)) {
  43. Image::thumbnail($this->pathPrefix . $path, $width, $height)->save($cropPath);
  44. }
  45. Image::crop($this->pathPrefix . $path, $width, $height, $start)->save($cropPath);
  46. return $this->urlPrefix . '/' . $cropFile;
  47. }
  48. public function water()
  49. {
  50. // TODO: Implement water() method.
  51. }
  52. }