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.

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