You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace App\Image;
  3. use App\Http\Controllers\Traits\FileTrait;
  4. use App\Image\Traits\ModulateTrait;
  5. use Illuminate\Contracts\Validation\Rule;
  6. use Illuminate\Support\Facades\Validator;
  7. use Illuminate\Validation\Rule as ValidationRule;
  8. use Jcupitt\Vips\Image;
  9. use Jcupitt\Vips\Interpretation;
  10. class ImageProcessor
  11. {
  12. use ModulateTrait, FileTrait;
  13. protected $defaultW = 2000;
  14. protected $defaultH = 2000;
  15. public function process(string $filename, string $target, array $options = [], $saveOptions = ['Q' => 100])
  16. {
  17. if (array_key_exists('w', $options) && array_key_exists('h', $options) && array_key_exists('r', $options)) {
  18. unset($options['r']);
  19. }
  20. if (array_key_exists('r', $options)) {
  21. $options['r'] = explode(':', $options['r']);
  22. if (count($options['r']) != 2) {
  23. unset($options['r']);
  24. }
  25. $validator = Validator::make($options, [
  26. 'r' => ['nullable', 'array'],
  27. 'r.*' => ['nullable', 'numeric'],
  28. ]);
  29. if ($validator->fails()) {
  30. foreach ($validator->messages()->getMessages() as $field_name => $messages) {
  31. unset($options[explode('.', $field_name)[0]]);
  32. }
  33. }
  34. }
  35. if (array_key_exists('r', $options)) {
  36. if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  37. $options['w'] = $options['w'] * $options['r'][0];
  38. $options['h'] = $options['w'] * $options['r'][1];
  39. }
  40. if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
  41. $options['w'] = $options['h'] * $options['r'][0];
  42. $options['h'] = $options['h'] * $options['r'][1];
  43. }
  44. if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  45. $options['w'] = getimagesize($filename)[0] * $options['r'][0];
  46. $options['h'] = getimagesize($filename)[0] * $options['r'][1];
  47. }
  48. unset($options['r']);
  49. }
  50. $validator = Validator::make($options, [
  51. 'w' => ['numeric', 'between:1,2000'],
  52. 'h' => ['numeric', 'between:1,2000'],
  53. 'canv' => ['boolean'],
  54. 'brightness' => ['numeric', 'between:0,100'],
  55. 'saturation' => ['numeric', 'between:0,100'],
  56. 'hue' => ['numeric', 'between:0,100'],
  57. 'rotation' => ['numeric'],
  58. 'flip' => ['string', ValidationRule::in(['h', 'v', 'hv'])]
  59. ]);
  60. if ($validator->fails()) {
  61. foreach ($validator->messages()->getMessages() as $field_name => $messages) {
  62. unset($options[$field_name]);
  63. }
  64. }
  65. if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  66. $image = Image::newFromFile($filename);
  67. }
  68. if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  69. $image = Image::thumbnail($filename, $options['w'], ['height' => $this->defaultH]);
  70. }
  71. if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
  72. $image = Image::thumbnail($filename, $this->defaultW, ['height' => $options['h']]);
  73. }
  74. if (array_key_exists('w', $options) && array_key_exists('h', $options)) {
  75. if (array_key_exists('canv', $options) && ($options['canv'] == true)) {
  76. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h']]);
  77. $widthH = ($options['h'] - $image->height) / 2;
  78. $widthW = ($options['w'] - $image->width) / 2;
  79. $image = $image->embed(
  80. $widthW,
  81. $widthH,
  82. $options['w'],
  83. $options['h'],
  84. ['extend' => 'background', 'background' => 1024]
  85. );
  86. } else {
  87. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
  88. }
  89. }
  90. if (array_key_exists('brightness', $options) || array_key_exists('saturation', $options) || array_key_exists('hue', $options)) {
  91. $image = $this->brightness($image, array_key_exists('brightness', $options) ? $options['brightness'] : 1.0, array_key_exists('saturation', $options) ? $options['saturation'] : 1.0, array_key_exists('hue', $options) ? $options['hue'] : 0.0);
  92. }
  93. if (array_key_exists('rotation', $options)) {
  94. $image = $image->rotate($options['rotation']);
  95. }
  96. if (array_key_exists('flip', $options)) {
  97. if ($options['flip'] == "h") {
  98. $image = $image->fliphor();
  99. }
  100. if ($options['flip'] == "v") {
  101. $image = $image->flipver();
  102. }
  103. if ($options['flip'] == "hv") {
  104. $image = $image->fliphor();
  105. $image = $image->flipver();
  106. }
  107. }
  108. $image->writeToFile($target, $saveOptions);
  109. return new \Illuminate\Http\File($target);
  110. }
  111. public function createFakeImage($stub, $path, $saveOptions = ['Q' => 100], $options = ["w" => null, "h" => null, "r" => null, "flip" => null, "canv" => null, "rotation" => null])
  112. {
  113. return $this->process($stub, $path, $options, $saveOptions);
  114. }
  115. public function convertImage(string $filePath, string $target, array $options = ['Q' => 100]): \Illuminate\Http\File
  116. {
  117. $tmpFile = \Jcupitt\Vips\Image::newFromFile($filePath);
  118. $tmpFile->writeToFile($target, $options);
  119. return new \Illuminate\Http\File($target);
  120. }
  121. }