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.

123 lines
4.6 KiB

3 years ago
3 years ago
3 years ago
  1. <?php
  2. namespace App\Image;
  3. use Jcupitt\Vips\Image;
  4. use Jcupitt\Vips\Interpretation;
  5. class ImageProcessor
  6. {
  7. public function process(string $filename, string $target, array $options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null],$saveOptions = ['Q' => 100])
  8. {
  9. if (!isset($options['w']) && !isset($options['h'])) {
  10. $image = Image::thumbnail($filename, getimagesize($filename)[1]);
  11. }
  12. if (isset($options['r'])) {
  13. $rArray = explode(':', $options['r']);
  14. if (isset($options['w']) && !isset($options['h'])) {
  15. $options['h'] = $options['w'] * $rArray[1];
  16. $options['w'] = $options['w'] * $rArray[0];
  17. }
  18. if (!isset($options['w']) && isset($options['h'])) {
  19. $options['h'] = $options['h'] * $rArray[1];
  20. $options['w'] = $options['h'] * $rArray[0];
  21. }
  22. if (!isset($options['w']) && !isset($options['h'])) {
  23. $options['h'] = getimagesize($filename)[0] * $rArray[1];
  24. $options['w'] = getimagesize($filename)[0] * $rArray[0];
  25. }
  26. }
  27. if (isset($options['w']) && !isset($options['h'])) {
  28. $image = Image::thumbnail($filename, $options['w'], ['height' => getimagesize($filename)[1]]);
  29. }
  30. if (!isset($options['w']) && isset($options['h'])) {
  31. $image = Image::thumbnail($filename, getimagesize($filename)[0], ['height' => $options['h']]);
  32. }
  33. if (isset($options['w']) && isset($options['h']) && !($options['canv'] == true)) {
  34. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h'], 'crop' => 'centre']);
  35. }
  36. if (isset($options['w']) && isset($options['h']) && $options['canv'] == true) {
  37. $image = Image::thumbnail($filename, $options['w'], ['height' => $options['h']]);
  38. $widthH = ($options['h'] - $image->height) / 2;
  39. $widthW = ($options['w'] - $image->width) / 2;
  40. $image = $image->embed(
  41. $widthW,
  42. $widthH,
  43. $options['w'],
  44. $options['h'],
  45. ['extend' => 'background', 'background' => 1024]
  46. );
  47. }
  48. if (isset($options['brightness']) || isset($options['saturation']) || isset($options['hue'])) {
  49. $image = $this->brightness($image, isset($options['brightness']) ? $options['brightness'] : 1.0, isset($options['saturation']) ? $options['saturation'] : 1.0, isset($options['hue']) ? $options['hue'] : 0.0);
  50. }
  51. if (isset($options['rotation'])) {
  52. $image = $image->rotate($options['rotation']);
  53. }
  54. if (isset($options['flip'])) {
  55. if ($options['flip'] == "h") {
  56. $image = $image->fliphor();
  57. }
  58. if ($options['flip'] == "v") {
  59. $image = $image->flipver();
  60. }
  61. if ($options['flip'] == "hv") {
  62. $image = $image->fliphor();
  63. $image = $image->flipver();
  64. }
  65. }
  66. $image->writeToFile($target,$saveOptions);
  67. return new \Illuminate\Http\File($target);
  68. }
  69. public function createFakeImage($stub,$path, $saveOptions = ['Q' => 100],$options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null])
  70. {
  71. $image = $this->process($stub,$path,$options,$saveOptions);
  72. }
  73. public function brightness($image, float $brightness = 1.0, float $saturation = 1.0, float $hue = 0.0)
  74. {
  75. $oldInterpretation = $image->interpretation;
  76. $hue %= 360;
  77. if ($hue < 0) {
  78. $hue = 360 + $hue;
  79. }
  80. if ($image->hasAlpha()) {
  81. $imageWithoutAlpha = $image->extract_band(0, ['n' => $image->bands - 1]);
  82. $alpha = $image->extract_band($image->bands - 1, ['n' => 1]);
  83. return $imageWithoutAlpha
  84. ->colourspace(Interpretation::LCH)
  85. ->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
  86. ->colourspace($oldInterpretation)
  87. ->bandjoin($alpha);
  88. }
  89. return $image
  90. ->colourspace(Interpretation::LCH)
  91. ->linear([$brightness, $saturation, 1.0], [0.0, 0.0, $hue])
  92. ->colourspace($oldInterpretation);
  93. }
  94. public function convertImage(string $filePath,string $target,array $options = ['Q' => 100]) : \Illuminate\Http\File
  95. {
  96. $tmpFile = \Jcupitt\Vips\Image::newFromFile($filePath);
  97. $tmpFile->writeToFile($target,$options);
  98. return new \Illuminate\Http\File($target);
  99. }
  100. }