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.

266 lines
10 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 processToBuffer(string $filename, 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. if (array_key_exists('q', $options)) {
  109. $saveOptions['Q'] = $options['q'];
  110. }
  111. return $image->writeToBuffer("." . app()->file->ext, $saveOptions);
  112. }
  113. public function process(string $filename, string $target, array $options = [], $saveOptions = ['Q' => 100])
  114. {
  115. if (array_key_exists('w', $options) && array_key_exists('h', $options) && array_key_exists('r', $options)) {
  116. unset($options['r']);
  117. }
  118. if (array_key_exists('r', $options)) {
  119. $options['r'] = explode(':', $options['r']);
  120. if (count($options['r']) != 2) {
  121. unset($options['r']);
  122. }
  123. $validator = Validator::make($options, [
  124. 'r' => ['nullable', 'array'],
  125. 'r.*' => ['nullable', 'numeric'],
  126. ]);
  127. if ($validator->fails()) {
  128. foreach ($validator->messages()->getMessages() as $field_name => $messages) {
  129. unset($options[explode('.', $field_name)[0]]);
  130. }
  131. }
  132. }
  133. if (array_key_exists('r', $options)) {
  134. if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  135. $options['w'] = $options['w'] * $options['r'][0];
  136. $options['h'] = $options['w'] * $options['r'][1];
  137. }
  138. if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
  139. $options['w'] = $options['h'] * $options['r'][0];
  140. $options['h'] = $options['h'] * $options['r'][1];
  141. }
  142. if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  143. $options['w'] = getimagesize($filename)[0] * $options['r'][0];
  144. $options['h'] = getimagesize($filename)[0] * $options['r'][1];
  145. }
  146. unset($options['r']);
  147. }
  148. $validator = Validator::make($options, [
  149. 'w' => ['numeric', 'between:1,2000'],
  150. 'h' => ['numeric', 'between:1,2000'],
  151. 'canv' => ['boolean'],
  152. 'brightness' => ['numeric', 'between:0,100'],
  153. 'saturation' => ['numeric', 'between:0,100'],
  154. 'hue' => ['numeric', 'between:0,100'],
  155. 'rotation' => ['numeric'],
  156. 'flip' => ['string', ValidationRule::in(['h', 'v', 'hv'])]
  157. ]);
  158. if ($validator->fails()) {
  159. foreach ($validator->messages()->getMessages() as $field_name => $messages) {
  160. unset($options[$field_name]);
  161. }
  162. }
  163. if (!array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  164. $image = Image::newFromFile($filename);
  165. }
  166. if (array_key_exists('w', $options) && !array_key_exists('h', $options)) {
  167. $image = Image::thumbnail($filename, $options['w'], ['height' => $this->defaultH]);
  168. }
  169. if (!array_key_exists('w', $options) && array_key_exists('h', $options)) {
  170. $image = Image::thumbnail($filename, $this->defaultW, ['height' => $options['h']]);
  171. }
  172. if (array_key_exists('w', $options) && array_key_exists('h', $options)) {
  173. if (array_key_exists('canv', $options) && ($options['canv'] == true)) {
  174. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h']]);
  175. $widthH = ($options['h'] - $image->height) / 2;
  176. $widthW = ($options['w'] - $image->width) / 2;
  177. $image = $image->embed(
  178. $widthW,
  179. $widthH,
  180. $options['w'],
  181. $options['h'],
  182. ['extend' => 'background', 'background' => 1024]
  183. );
  184. } else {
  185. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
  186. }
  187. }
  188. if (array_key_exists('brightness', $options) || array_key_exists('saturation', $options) || array_key_exists('hue', $options)) {
  189. $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);
  190. }
  191. if (array_key_exists('rotation', $options)) {
  192. $image = $image->rotate($options['rotation']);
  193. }
  194. if (array_key_exists('flip', $options)) {
  195. if ($options['flip'] == "h") {
  196. $image = $image->fliphor();
  197. }
  198. if ($options['flip'] == "v") {
  199. $image = $image->flipver();
  200. }
  201. if ($options['flip'] == "hv") {
  202. $image = $image->fliphor();
  203. $image = $image->flipver();
  204. }
  205. }
  206. $image->writeToFile($target, $saveOptions);
  207. return new \Illuminate\Http\File($target);
  208. }
  209. public function createFakeImage($stub, $path, $saveOptions = ['Q' => 100], $options = ["w" => null, "h" => null, "r" => null, "flip" => null, "canv" => null, "rotation" => null])
  210. {
  211. return $this->process($stub, $path, $options, $saveOptions);
  212. }
  213. public function convertImage(string $filePath, string $target, array $options = ['Q' => 100]): \Illuminate\Http\File
  214. {
  215. $tmpFile = \Jcupitt\Vips\Image::newFromFile($filePath);
  216. $tmpFile->writeToFile($target, $options);
  217. return new \Illuminate\Http\File($target);
  218. }
  219. }